Skip to content

Commit 81ad185

Browse files
authored
Merge pull request #2595 from nicoddemus/docs-rootdir-pythonpath
Clarify PYTHONPATH changes and ``rootdir`` roles
2 parents 0aa2480 + 3d24485 commit 81ad185

File tree

9 files changed

+106
-17
lines changed

9 files changed

+106
-17
lines changed

_pytest/config.py

-1
Original file line numberDiff line numberDiff line change
@@ -1079,7 +1079,6 @@ def _preparse(self, args, addopts=True):
10791079
self.pluginmanager.load_setuptools_entrypoints('pytest11')
10801080
self.pluginmanager.consider_env()
10811081
self.known_args_namespace = ns = self._parser.parse_known_args(args, namespace=self.option.copy())
1082-
confcutdir = self.known_args_namespace.confcutdir
10831082
if self.known_args_namespace.confcutdir is None and self.inifile:
10841083
confcutdir = py.path.local(self.inifile).dirname
10851084
self.known_args_namespace.confcutdir = confcutdir

doc/en/cache.rst

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
.. _cache:
2+
13
Cache: working with cross-testrun state
24
=======================================
35

doc/en/contents.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@ Full pytest documentation
3131
plugins
3232
writing_plugins
3333

34-
example/index
3534
goodpractices
35+
pythonpath
3636
customize
37+
example/index
3738
bash-completion
3839

3940
backwards-compatibility

doc/en/customize.rst

+22-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
Basic test configuration
2-
===================================
1+
Configuration
2+
=============
33

44
Command line options and configuration file settings
55
-----------------------------------------------------------------
@@ -15,17 +15,31 @@ which were registered by installed plugins.
1515
.. _rootdir:
1616
.. _inifiles:
1717

18-
initialization: determining rootdir and inifile
18+
Initialization: determining rootdir and inifile
1919
-----------------------------------------------
2020

2121
.. versionadded:: 2.7
2222

23-
pytest determines a "rootdir" for each test run which depends on
23+
pytest determines a ``rootdir`` for each test run which depends on
2424
the command line arguments (specified test files, paths) and on
25-
the existence of inifiles. The determined rootdir and ini-file are
26-
printed as part of the pytest header. The rootdir is used for constructing
27-
"nodeids" during collection and may also be used by plugins to store
28-
project/testrun-specific information.
25+
the existence of *ini-files*. The determined ``rootdir`` and *ini-file* are
26+
printed as part of the pytest header during startup.
27+
28+
Here's a summary what ``pytest`` uses ``rootdir`` for:
29+
30+
* Construct *nodeids* during collection; each test is assigned
31+
a unique *nodeid* which is rooted at the ``rootdir`` and takes in account full path,
32+
class name, function name and parametrization (if any).
33+
34+
* Is used by plugins as a stable location to store project/test run specific information;
35+
for example, the internal :ref:`cache <cache>` plugin creates a ``.cache`` subdirectory
36+
in ``rootdir`` to store its cross-test run state.
37+
38+
Important to emphasize that ``rootdir`` is **NOT** used to modify ``sys.path``/``PYTHONPATH`` or
39+
influence how modules are imported. See :ref:`pythonpath` for more details.
40+
41+
Finding the ``rootdir``
42+
~~~~~~~~~~~~~~~~~~~~~~~
2943

3044
Here is the algorithm which finds the rootdir from ``args``:
3145

doc/en/example/index.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11

22
.. _examples:
33

4-
Usages and Examples
5-
===========================================
4+
Examples and customization tricks
5+
=================================
66

77
Here is a (growing) list of examples. :ref:`Contact <contact>` us if you
88
need more examples or have questions. Also take a look at the

doc/en/nose.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Supported nose Idioms
2626
* setup and teardown at module/class/method level
2727
* SkipTest exceptions and markers
2828
* setup/teardown decorators
29-
* ``yield``-based tests and their setup
29+
* ``yield``-based tests and their setup (considered deprecated as of pytest 3.0)
3030
* ``__test__`` attribute on modules/classes/functions
3131
* general usage of nose utilities
3232

doc/en/pythonpath.rst

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
.. _pythonpath:
2+
3+
pytest import mechanisms and ``sys.path``/``PYTHONPATH``
4+
========================================================
5+
6+
Here's a list of scenarios where pytest may need to change ``sys.path`` in order
7+
to import test modules or ``conftest.py`` files.
8+
9+
Test modules / ``conftest.py`` files inside packages
10+
----------------------------------------------------
11+
12+
Consider this file and directory layout::
13+
14+
root/
15+
|- foo/
16+
|- __init__.py
17+
|- conftest.py
18+
|- bar/
19+
|- __init__.py
20+
|- tests/
21+
|- __init__.py
22+
|- test_foo.py
23+
24+
25+
When executing::
26+
27+
pytest root/
28+
29+
30+
31+
pytest will find ``foo/bar/tests/test_foo.py`` and realize it is part of a package given that
32+
there's an ``__init__.py`` file in the same folder. It will then search upwards until it can find the
33+
last folder which still contains an ``__init__.py`` file in order to find the package *root* (in
34+
this case ``foo/``). To load the module, it will insert ``root/`` to the front of
35+
``sys.path`` (if not there already) in order to load
36+
``test_foo.py`` as the *module* ``foo.bar.tests.test_foo``.
37+
38+
The same logic applies to the ``conftest.py`` file: it will be imported as ``foo.conftest`` module.
39+
40+
Preserving the full package name is important when tests live in a package to avoid problems
41+
and allow test modules to have duplicated names. This is also discussed in details in
42+
:ref:`test discovery`.
43+
44+
Standalone test modules / ``conftest.py`` files
45+
-----------------------------------------------
46+
47+
Consider this file and directory layout::
48+
49+
root/
50+
|- foo/
51+
|- conftest.py
52+
|- bar/
53+
|- tests/
54+
|- test_foo.py
55+
56+
57+
When executing::
58+
59+
pytest root/
60+
61+
pytest will find ``foo/bar/tests/test_foo.py`` and realize it is NOT part of a package given that
62+
there's no ``__init__.py`` file in the same folder. It will then add ``root/foo/bar/tests`` to
63+
``sys.path`` in order to import ``test_foo.py`` as the *module* ``test_foo``. The same is done
64+
with the ``conftest.py`` file by adding ``root/foo`` to ``sys.path`` to import it as ``conftest``.
65+
66+
For this reason this layout cannot have test modules with the same name, as they all will be
67+
imported in the global import namespace.
68+
69+
This is also discussed in details in :ref:`test discovery`.
70+
71+

doc/en/usage.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ You can invoke testing through the Python interpreter from the command line::
1717
python -m pytest [...]
1818

1919
This is almost equivalent to invoking the command line script ``pytest [...]``
20-
directly, except that python will also add the current directory to ``sys.path``.
20+
directly, except that Python will also add the current directory to ``sys.path``.
2121

2222
Possible exit codes
2323
--------------------------------------------------------------

doc/en/writing_plugins.rst

+5-3
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ Plugin discovery order at tool startup
4949

5050
Note that pytest does not find ``conftest.py`` files in deeper nested
5151
sub directories at tool startup. It is usually a good idea to keep
52-
your conftest.py file in the top level test or project root directory.
52+
your ``conftest.py`` file in the top level test or project root directory.
5353

5454
* by recursively loading all plugins specified by the
5555
``pytest_plugins`` variable in ``conftest.py`` files
@@ -94,10 +94,12 @@ Here is how you might run it::
9494
If you have ``conftest.py`` files which do not reside in a
9595
python package directory (i.e. one containing an ``__init__.py``) then
9696
"import conftest" can be ambiguous because there might be other
97-
``conftest.py`` files as well on your PYTHONPATH or ``sys.path``.
97+
``conftest.py`` files as well on your ``PYTHONPATH`` or ``sys.path``.
9898
It is thus good practice for projects to either put ``conftest.py``
9999
under a package scope or to never import anything from a
100-
conftest.py file.
100+
``conftest.py`` file.
101+
102+
See also: :ref:`pythonpath`.
101103

102104

103105
Writing your own plugin

0 commit comments

Comments
 (0)