'Testing project using pytest with tests from external "tests" directory
I have a problem with that testing. I tried to find anything but all guides are about using pycharm's abilities. So, I have a structure like this:
project1
|-module1.py
tests
|-test1.py
module1.py:
def foo(a, b):
return a + b + 1
test_module1.py:
from project1.module1 import foo
def test_foo():
assert foo(1, 1) == 3
While I stay in root directory (where this structure is located), how can i run testing with pytest from terminal. I really tried my best, but always was getting different errors. Most of them look like ModuleNotFoundError: No module named 'project1'.
Thanks!!!
Solution 1:[1]
You need to add an empty __init__.py into each sub directory.
project1
|-__init__.py
|-module1.py
tests
|-__init__.py
|-test1.py
Solution 2:[2]
You need to add project1 path into your PYTHONPATH
OR
use inline command: PYTHONPATH=project1 pytest tests/
This thread might help clarify your doubt: https://stackoverflow.com/a/15317038/2022338
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Teejay Bruno |
| Solution 2 |
