I am working in a codebase with the following folder structure:
lambdas
|-lambda1
|-core
|-infrastructure
...
|-lambda2
|-core
|-infrastructure
...
...
tests
|-unit
|-lambda1
...tests
|-lambda2
...tests
...
Since the lambdas are all deployed independently, imports within each lambda are absolute and from the root, like so:
import core.domain.user
My issue arises when I am running tests with PyTest. When the tests for lambda1 are ran, sys.modules creates an entry for ‘core’ and maps it to the core module in lambda1:
{
...
'core': <module 'core' from '/Users/me/project/lambdas/lambda1/core/__init__.py'>
...
}
Then, when I want to import ‘core’ again when the lambda2 tests run, I get errors since the wrong core module is imported.
I have tried deleting this entry in conftest.py but that didn’t solve the issue, I think since sys.path will also grab the wrong ‘core’ module depending on its order. I have tried altering sys.path in every test file and that seems to help but feels too much like a hack.
I think that moving the tests to be in the same directory as their lambda would help, but I want to explore options that don’t alter the folder structure first.
Any ideas?
Samuel O’Connell is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.