-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2595 from nicoddemus/docs-rootdir-pythonpath
Clarify PYTHONPATH changes and ``rootdir`` roles
- Loading branch information
Showing
9 changed files
with
106 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
.. _cache: | ||
|
||
Cache: working with cross-testrun state | ||
======================================= | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
.. _pythonpath: | ||
|
||
pytest import mechanisms and ``sys.path``/``PYTHONPATH`` | ||
======================================================== | ||
|
||
Here's a list of scenarios where pytest may need to change ``sys.path`` in order | ||
to import test modules or ``conftest.py`` files. | ||
|
||
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 root/ | ||
|
||
|
||
|
||
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 of | ||
``sys.path`` (if not there already) in order to load | ||
``test_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 in | ||
:ref:`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 root/ | ||
|
||
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`` to | ||
``sys.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 :ref:`test discovery`. | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters