Skip to content

Commit e2ec64e

Browse files
committed
Improve docs
1 parent 6ccb5e7 commit e2ec64e

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

changelog/12749.feature.rst

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
1-
New :confval:`collect_imported_tests`: when enabled (the default) pytest will collect classes/functions in test modules even if they are imported from another file.
1+
pytest traditionally collects classes/functions in the test module namespace even if they are imported from another file.
22

3-
Setting this to False will make pytest collect classes/functions from test files only if they are defined in that file (as opposed to imported there).
3+
For example:
4+
5+
.. code-block:: python
6+
7+
# contents of src/domain.py
8+
class Testament: ...
9+
10+
11+
# contents of tests/test_testament.py
12+
from domain import Testament
13+
14+
15+
def test_testament(): ...
16+
17+
In this scenario with the default options, pytest will collect the class `Testament` from `tests/test_testament.py` because it starts with `Test`, even though in this case it is a production class being imported in the test module namespace.
18+
19+
This behavior can now be prevented by setting the new :confval:`collect_imported_tests` configuration option to ``false``, which will make pytest collect classes/functions from test files **only** if they are defined in that file.
420

521
-- by :user:`FreerGit`

doc/en/reference/reference.rst

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1306,7 +1306,7 @@ passed multiple times. The expected format is ``name=value``. For example::
13061306
.. versionadded:: 8.4
13071307

13081308
Setting this to ``false`` will make pytest collect classes/functions from test
1309-
files only if they are defined in that file (as opposed to imported there).
1309+
files **only** if they are defined in that file (as opposed to imported there).
13101310

13111311
.. code-block:: ini
13121312
@@ -1315,6 +1315,26 @@ passed multiple times. The expected format is ``name=value``. For example::
13151315
13161316
Default: ``true``
13171317

1318+
pytest traditionally collects classes/functions in the test module namespace even if they are imported from another file.
1319+
1320+
For example:
1321+
1322+
.. code-block:: python
1323+
1324+
# contents of src/domain.py
1325+
class Testament: ...
1326+
1327+
1328+
# contents of tests/test_testament.py
1329+
from domain import Testament
1330+
1331+
1332+
def test_testament(): ...
1333+
1334+
In this scenario, with the default options, pytest will collect the class `Testament` from `tests/test_testament.py` because it starts with `Test`, even though in this case it is a production class being imported in the test module namespace.
1335+
1336+
Set ``collected_imported_tests`` to ``false`` in the configuration file prevents that.
1337+
13181338
.. confval:: consider_namespace_packages
13191339

13201340
Controls if pytest should attempt to identify `namespace packages <https://packaging.python.org/en/latest/guides/packaging-namespace-packages>`__

0 commit comments

Comments
 (0)