conftest solution
The least invasive solution is to add an empty file called conftest.py to conftest.py repo/ :
$ touch repo/conftest.py
It. There is no need to write your own code to distort sys.path or remember to drag PYTHONPATH together or put __init__.py in directories where it does not belong.
Project catalog subsequently:
repo ├── conftest.py ├── app.py ├── settings.py ├── models.py └── tests └── test_app.py
explanation
pytest looks for pytest modules in conftest tests for collecting custom hooks and fixtures, and to import custom objects from them, pytest adds the parent directory conftest.py to sys.path .
Other project structures
If you have a different project structure, put conftest.py in the root directory of the package (the one that contains the packages but not the package itself, therefore does not contain __init__.py ), for example:
repo ├── conftest.py ├── spam │ ├── __init__.py │ ├── bacon.py │ └── egg.py ├── eggs │ ├── __init__.py │ └── sausage.py └── tests ├── test_bacon.py └── test_egg.py
src location
Although this approach can be used with the src layout (put conftest.py in conftest.py src ):
repo ├── src │ ├── conftest.py │ ├── spam │ │ ├── __init__.py │ │ ├── bacon.py │ │ └── egg.py │ └── eggs │ ├── __init__.py │ └── sausage.py └── tests ├── test_bacon.py └── test_egg.py
PYTHONPATH in mind that adding src to PYTHONPATH reduces the value and benefits of the src layout! You will end up testing the code from the repository, not the installed package. If you need to do this, you probably do not need src dir at all.
Where to go from here
Of course, conftest modules are not just files that help in detecting source code; this is the place where all the project-specific improvements to the pytest platform and the tuning of your test suite take place. pytest lot of information about conftest modules scattered across all documents ; start with conftest.py : local plugins for each directory
In addition, SO has a great question on conftest modules: what is the use of conftest.py files in py.test?
hoefling May 30 '18 at 17:42 2018-05-30 17:42
source share