You never know…
Small update to my previous post (link) about pytest. With help of Oleg Eterevsky I found there is a better way to have tests in separate directory, without manging import path.
Answer is simple: Just add __init__.py to the tests directory.
Directory structure
/app/
/app/app
/app/app/mymodule.py
/app/tests
/app/tests/__init_.py <- zero byte python magic
/app/tests/test_mymodule.py
After that tests in the ‘tests’ directory can use normal import:
from app import mymodule
It works like magic. Still, to have proper code coverage reports I need to specify filename to track. This done in the old way:
#!/usr/bin/python
import os
import inspect
import sys
import pytest@pytest.fixture
def config():
from dibctl import config
return configdef test_fake(config):
assert configif __name__ == "__main__":
ourfilename = os.path.abspath(inspect.getfile(inspect.currentframe()))
currentdir = os.path.dirname(ourfilename)
parentdir = os.path.dirname(currentdir)
file_to_test = os.path.join(
parentdir,
os.path.basename(parentdir),
os.path.basename(ourfilename).replace("test_", '')
)
pytest.main([
"-vv",
"--cov", file_to_test,
"--cov-report", "term-missing"
] + sys.argv)
But this is much nicer than original version, because all than noise around paths related only to coverage reporting, not the tests itself.