i am currently working on a project, where i need to run tests inside a different file structure like this:
/my_project
├── ...my python code
/given_proj
├── /package
│ ├── init.py
│ └── main.py
└── /tests
└── test_main.py
Current Approach
From inside my project i want to execute the tests within the given project.
My current approach is using unittest.TextTestRunner like this:
unittest.TextTestRunner().run(unittest.defaultTestLoader.discover('../given_proj/tests'))
.
Problem with the current approach
Of course the test file wants to import from main.py
like this from package.main import my_function
. However when i run my code, the tests fail to run because the “package” module cannot be found:
...given_projteststest_main.py", line 2, in <module>
from package.main import my_function
ModuleNotFoundError: No module named 'package'
When i run the tests with python -m unittest discover -s tests
from the command line in the directory of the given_proj
they run fine.
What i tried
I tried changing the working directory to given_proj
with os.chdir('../given_proj')
however it produces the same result.
What i kinda tried, was importing the module manually with importlib.import_module()
. There i am not sure if i did it wrong or it doesnt work either.
My question
How do i make it, that the tests get run, as if i would run it from the actual directory they are supposed to run?