You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This introduces --import-mode=importlib, which uses fine-grained facilities
from importlib to import test modules and conftest files, bypassing
the need to change sys.path and sys.modules as side-effect of that.
I've also opened pytest-dev#7245 to gather feedback on the new import mode.
New ``--import-mode=importlib`` option that uses `importlib <https://docs.python.org/3/library/importlib.html>`__ to import test modules.
2
+
3
+
Traditionally pytest used ``__import__`` while changing ``sys.path`` to import test modules (which
4
+
also changes ``sys.modules`` as a side-effect), which works but has a number of drawbacks, like requiring test modules
5
+
that don't live in packages to have unique names (as they need to reside under a unique name in ``sys.modules``).
6
+
7
+
``--import-mode=importlib`` uses more fine grained import mechanisms from ``importlib`` which don't
8
+
require pytest to change ``sys.path`` or ``sys.modules`` at all, eliminating much of the drawbacks
9
+
of the previous mode.
10
+
11
+
We intend to make ``--import-mode=importlib`` the default in future versions, so users are encouraged
12
+
to try the new mode and provide feedback (both positive or negative) in issue `#7245 <https://github.com/pytest-dev/pytest/issues/7245>`__.
13
+
14
+
You can read more about this option in `the documentation <https://docs.pytest.org/en/latest/pythonpath.html#import-modes>`__.
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.
6
+
.. _`import-modes`:
7
+
8
+
Import modes
9
+
------------
10
+
11
+
pytest as a testing framework needs to import test moduels and ``conftest.py`` files for execution.
12
+
13
+
Importing files in Python (at least until recently) is a non-trivial processes, often requiring
14
+
changing `sys.path <https://docs.python.org/3/library/sys.html#sys.path>`__. Some aspects of the
15
+
import process can be controlled through the ``--import-mode`` command-line flag, which can assume
16
+
these values:
17
+
18
+
* ``prepend`` (default): the directory path containing each module will be inserted into the *beginning*
19
+
of ``sys.path`` if not already there, and then imported with the `__import__ <https://docs.python.org/3/library/functions.html#__import__>`__ builtin.
20
+
21
+
This requires test module names to be unique when the test directory tree is not arranged in
22
+
packages, because the modules will put in ``sys.modules`` after importing.
23
+
24
+
This is the classic mechanism, dating back from the time Python 2 was still supported.
25
+
26
+
* ``append``: the directory containing each module is appended to the end of ``sys.path`` if not already
27
+
there, and imported with ``__import__``.
28
+
29
+
This better allows to run test modules against installed versions of a package even if the
30
+
package under test has the same import root. For example:
31
+
32
+
::
33
+
34
+
testing/__init__.py
35
+
testing/test_pkg_under_test.py
36
+
pkg_under_test/
37
+
38
+
the tests will run against the installed version
39
+
of ``pkg_under_test`` when ``--import-mode=append`` is used whereas
40
+
with ``prepend`` they would pick up the local version. This kind of confusion is why
41
+
we advocate for using :ref:`src <src-layout>` layouts.
42
+
43
+
Same as ``prepend``, requires test module names to be unique when the test directory tree is
44
+
not arranged in packages, because the modules will put in ``sys.modules`` after importing.
45
+
46
+
* ``importlib``: new in pytest-6.0, this mode uses `importlib <https://docs.python.org/3/library/importlib.html>`__ to import test modules. This gives full control over the import process, and doesn't require
47
+
changing ``sys.path`` or ``sys.modules`` at all.
48
+
49
+
For this reason this doesn't require test module names to be unique at all.
50
+
51
+
We intend to make ``importlib`` the default in future releases.
52
+
53
+
``prepend`` and ``append`` import modes scenarios
54
+
-------------------------------------------------
55
+
56
+
Here's a list of scenarios when using ``prepend`` or ``append`` import modes where pytest needs to
57
+
change ``sys.path`` in order to import test modules or ``conftest.py`` files, and the issues users
58
+
might encounter because of that.
8
59
9
60
Test modules / ``conftest.py`` files inside packages
0 commit comments