Skip to content

Commit eb8592c

Browse files
committed
update default and add docs
1 parent a6ee0bc commit eb8592c

File tree

4 files changed

+37
-9
lines changed

4 files changed

+37
-9
lines changed

changelog/12749.feature.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1-
Add :confval:`collect_imported_tests`, when enabled (default is disabled) will make sure to not consider classes/functions which are imported by a test file and contains Test/test_*/*_test.
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.
2+
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).
24

35
-- by :user:`FreerGit`

doc/en/reference/reference.rst

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1839,9 +1839,17 @@ passed multiple times. The expected format is ``name=value``. For example::
18391839
pytest testing doc
18401840
18411841
1842-
.. confval:: tmp_path_retention_count
1842+
.. confval:: collect_imported_tests
1843+
1844+
Setting this to `false` will make pytest collect classes/functions from test
1845+
files only if they are defined in that file (as opposed to imported there).
1846+
1847+
.. code-block:: ini
18431848
1849+
[pytest]
1850+
collect_imported_tests = false
18441851
1852+
.. confval:: tmp_path_retention_count
18451853

18461854
How many sessions should we keep the `tmp_path` directories,
18471855
according to `tmp_path_retention_policy`.

src/_pytest/main.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def pytest_addoption(parser: Parser) -> None:
8282
"collect_imported_tests",
8383
"Whether to collect tests in imported modules outside `testpaths`",
8484
type="bool",
85-
default=False,
85+
default=True,
8686
)
8787
group = parser.getgroup("general", "Running and selection options")
8888
group._addoption(
@@ -973,7 +973,7 @@ def genitems(self, node: nodes.Item | nodes.Collector) -> Iterator[nodes.Item]:
973973
self.trace("genitems", node)
974974
if isinstance(node, nodes.Item):
975975
node.ihook.pytest_itemcollected(item=node)
976-
if self.config.getini("collect_imported_tests"):
976+
if not self.config.getini("collect_imported_tests"):
977977
if isinstance(node.parent, Module) and isinstance(node, Function):
978978
if inspect.isfunction(node._getobj()):
979979
fn_defined_at = node._getobj().__module__
@@ -988,7 +988,7 @@ def genitems(self, node: nodes.Item | nodes.Collector) -> Iterator[nodes.Item]:
988988
handle_dupes = not (keepduplicates and isinstance(node, nodes.File))
989989
rep, duplicate = self._collect_one_node(node, handle_dupes)
990990

991-
if self.config.getini("collect_imported_tests"):
991+
if not self.config.getini("collect_imported_tests"):
992992
for subnode in rep.result:
993993
if isinstance(subnode, Class) and isinstance(
994994
subnode.parent, Module

testing/test_collect_imports.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def test_collect_imports_disabled(pytester: Pytester) -> None:
5555
collect_imported_tests = false
5656
""")
5757

58-
run_import_class_test(pytester, errors=1)
58+
run_import_class_test(pytester, passed=1)
5959

6060

6161
def test_collect_imports_default(pytester: Pytester) -> None:
@@ -74,7 +74,7 @@ def test_collect_imports_enabled(pytester: Pytester) -> None:
7474
collect_imported_tests = true
7575
""")
7676

77-
run_import_class_test(pytester, passed=1)
77+
run_import_class_test(pytester, errors=1)
7878

7979

8080
def run_import_functions_test(
@@ -137,14 +137,32 @@ def test_collect_function_imports_enabled(pytester: Pytester) -> None:
137137
collect_imported_tests = true
138138
""")
139139

140-
run_import_functions_test(pytester, passed=1, errors=0, failed=0)
140+
run_import_functions_test(pytester, passed=2, errors=0, failed=1)
141141

142142

143143
def test_collect_function_imports_disabled(pytester: Pytester) -> None:
144144
pytester.makeini("""
145145
[pytest]
146-
testpaths = "tests"
146+
# testpaths = "tests"
147147
collect_imported_tests = false
148148
""")
149149

150+
run_import_functions_test(pytester, passed=1, errors=0, failed=0)
151+
152+
153+
def test_behaviour_without_testpaths_set_and_false(pytester: Pytester) -> None:
154+
pytester.makeini("""
155+
[pytest]
156+
collect_imported_tests = false
157+
""")
158+
159+
run_import_functions_test(pytester, passed=1, errors=0, failed=0)
160+
161+
162+
def test_behaviour_without_testpaths_set_and_true(pytester: Pytester) -> None:
163+
pytester.makeini("""
164+
[pytest]
165+
collect_imported_tests = true
166+
""")
167+
150168
run_import_functions_test(pytester, passed=2, errors=0, failed=1)

0 commit comments

Comments
 (0)