pytest import mechanisms and sys.path/PYTHONPATH (original) (raw)

Toggle table of contents sidebar

Import modes

pytest as a testing framework needs to import test modules and conftest.py files for execution.

Importing files in Python is a non-trivial process, so aspects of the import process can be controlled through the --import-mode command-line flag, which can assume these values:

Note

Initially we intended to make importlib the default in future releases, however it is clear now that it has its own set of drawbacks so the default will remain prepend for the foreseeable future.

Note

By default, pytest will not attempt to resolve namespace packages automatically, but that can be changed via the consider_namespace_packages configuration variable.

prepend and append import modes scenarios

Here’s a list of scenarios when using prepend or append import modes where pytest needs to change sys.path in order to import test modules or conftest.py files, and the issues users might encounter because of that.

Test modules / conftest.py files inside packages

Consider this file and directory layout:

root/ |- foo/ |- init.py |- conftest.py |- bar/ |- init.py |- tests/ |- init.py |- test_foo.py

When executing:

pytest will find foo/bar/tests/test_foo.py and realize it is part of a package given that there’s an __init__.py file in the same folder. It will then search upwards until it can find the last folder which still contains an __init__.py file in order to find the package root (in this case foo/). To load the module, it will insert root/ to the front ofsys.path (if not there already) in order to loadtest_foo.py as the module foo.bar.tests.test_foo.

The same logic applies to the conftest.py file: it will be imported as foo.conftest module.

Preserving the full package name is important when tests live in a package to avoid problems and allow test modules to have duplicated names. This is also discussed in details inConventions for Python test discovery.

Standalone test modules / conftest.py files

Consider this file and directory layout:

root/ |- foo/ |- conftest.py |- bar/ |- tests/ |- test_foo.py

When executing:

pytest will find foo/bar/tests/test_foo.py and realize it is NOT part of a package given that there’s no __init__.py file in the same folder. It will then add root/foo/bar/tests tosys.path in order to import test_foo.py as the module test_foo. The same is done with the conftest.py file by adding root/foo to sys.path to import it as conftest.

For this reason this layout cannot have test modules with the same name, as they all will be imported in the global import namespace.

This is also discussed in details in Conventions for Python test discovery.

Invoking pytest versus python -m pytest

Running pytest with pytest [...] instead of python -m pytest [...] yields nearly equivalent behaviour, except that the latter will add the current directory to sys.path, which is standard python behavior.

See also Calling pytest through python -m pytest.