@@ -56,7 +56,44 @@ of ``mypkg``.
5656
5757Note that using this scheme your test files must have **unique names **, because
5858``pytest `` will import them as *top-level * modules since there are no packages
59- to derive a full package name from.
59+ to derive a full package name from. In other words, the test files in the example above will
60+ be imported as ``test_app `` and ``test_view `` top-level modules by adding ``tests/ `` to
61+ ``sys.path ``.
62+
63+ If you need to have test modules with the same name, you might add ``__init__.py `` files to your
64+ ``tests `` folder and subfolders, changing them to packages::
65+
66+ mypkg/
67+ ...
68+ tests/
69+ __init__.py
70+ foo/
71+ __init__.py
72+ test_view.py
73+ bar/
74+ __init__.py
75+ test_view.py
76+
77+ Now pytest will load the modules as ``tests.foo.test_view `` and ``tests.bar.test_view ``, allowing
78+ you to have modules with the same name. But now this introduces a subtle problem: in order to load
79+ the test modules from the ``tests `` directory, pytest prepends the root of the repository to
80+ ``sys.path ``, which adds the side-effect that now ``mypkg `` is also importable.
81+ This is problematic if you are using a tool like `tox `_ to test your package in a virtual environment,
82+ because you want to test the *installed * version of your package, not the local code from the repository.
83+
84+ There are a couple of solutions to this:
85+
86+ * Do not add the ``tests/__init__.py `` file. This will cause your test modules to be loaded as
87+ ``foo.test_view `` and ``bar.test_view ``, which is usually fine and avoids having the root of
88+ the repository being added to the ``PYTHONPATH ``.
89+
90+ * Add ``addopts=--import-mode=append `` to your ``pytest.ini `` file. This will cause ``pytest `` to
91+ load your modules by *appending * the root directory instead of prepending.
92+
93+ * Alternatively, consider using the ``src `` layout explained in detail in this excellent
94+ `blog post by Ionel Cristian Mărieș <https://blog.ionelmc.ro/2014/05/25/python-packaging/#the-structure >`_
95+ which prevents a lot of the pitfalls described here.
96+
6097
6198Tests as part of application code
6299^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
0 commit comments