Skip to content

Commit 2867e8e

Browse files
committed
python: change pytest pkg/__init__.py to only collect the __init__.py Module
Previously it would collect the entire package, but this is not what users expect. Refs pytest-dev#3749 Fixes pytest-dev#8976 Fixes pytest-dev#9263 Fixes pytest-dev#9313
1 parent b5ff089 commit 2867e8e

File tree

8 files changed

+39
-14
lines changed

8 files changed

+39
-14
lines changed

changelog/8976.breaking.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Running `pytest pkg/__init__.py` now collects the `pkg/__init__.py` file (module) only.
2+
Previously, it collected the entire `pkg` package, including other test files in the directory, but excluding tests in the `__init__.py` file itself
3+
(unless :confval:`python_files` was changed to allow `__init__.py` file).
4+
5+
To collect the entire package, specify just the directory: `pytest pkg`.

src/_pytest/python.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -739,7 +739,9 @@ def collect(self) -> Iterable[Union[nodes.Item, nodes.Collector]]:
739739
this_path = self.path.parent
740740

741741
# Always collect the __init__ first.
742-
if path_matches_patterns(self.path, self.config.getini("python_files")):
742+
if self.session.isinitpath(self.path) or path_matches_patterns(
743+
self.path, self.config.getini("python_files")
744+
):
743745
yield Module.from_parent(self, path=self.path)
744746

745747
pkg_prefixes: Set[Path] = set()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def test_init():
2+
pass
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
def test():
1+
def test_foo():
22
pass

testing/python/collect.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1407,10 +1407,15 @@ def test_package_collection_infinite_recursion(pytester: Pytester) -> None:
14071407

14081408

14091409
def test_package_collection_init_given_as_argument(pytester: Pytester) -> None:
1410-
"""Regression test for #3749"""
1410+
"""Regression test for #3749, #8976, #9263, #9313.
1411+
1412+
Specifying an __init__.py file directly should collect only the __init__.py
1413+
Module, not the entire package.
1414+
"""
14111415
p = pytester.copy_example("collect/package_init_given_as_arg")
1412-
result = pytester.runpytest(p / "pkg" / "__init__.py")
1413-
result.stdout.fnmatch_lines(["*1 passed*"])
1416+
items, hookrecorder = pytester.inline_genitems(p / "pkg" / "__init__.py")
1417+
assert len(items) == 1
1418+
assert items[0].name == "test_init"
14141419

14151420

14161421
def test_package_with_modules(pytester: Pytester) -> None:

testing/test_collection.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1362,19 +1362,27 @@ def test_collect_pkg_init_and_file_in_args(pytester: Pytester) -> None:
13621362
p = subdir.joinpath("test_file.py")
13631363
p.write_text("def test_file(): pass")
13641364

1365-
# NOTE: without "-o python_files=*.py" this collects test_file.py twice.
1366-
# This changed/broke with "Add package scoped fixtures #2283" (2b1410895)
1367-
# initially (causing a RecursionError).
1368-
result = pytester.runpytest("-v", str(init), str(p))
1365+
# Just the package directory, the __init__.py module is filtered out.
1366+
result = pytester.runpytest("-v", subdir)
13691367
result.stdout.fnmatch_lines(
13701368
[
13711369
"sub/test_file.py::test_file PASSED*",
1370+
"*1 passed in*",
1371+
]
1372+
)
1373+
1374+
# But it's included if specified directly.
1375+
result = pytester.runpytest("-v", init, p)
1376+
result.stdout.fnmatch_lines(
1377+
[
1378+
"sub/__init__.py::test_init PASSED*",
13721379
"sub/test_file.py::test_file PASSED*",
13731380
"*2 passed in*",
13741381
]
13751382
)
13761383

1377-
result = pytester.runpytest("-v", "-o", "python_files=*.py", str(init), str(p))
1384+
# Or if the pattern allows it.
1385+
result = pytester.runpytest("-v", "-o", "python_files=*.py", subdir)
13781386
result.stdout.fnmatch_lines(
13791387
[
13801388
"sub/__init__.py::test_init PASSED*",
@@ -1389,10 +1397,13 @@ def test_collect_pkg_init_only(pytester: Pytester) -> None:
13891397
init = subdir.joinpath("__init__.py")
13901398
init.write_text("def test_init(): pass")
13911399

1392-
result = pytester.runpytest(str(init))
1400+
result = pytester.runpytest(subdir)
13931401
result.stdout.fnmatch_lines(["*no tests ran in*"])
13941402

1395-
result = pytester.runpytest("-v", "-o", "python_files=*.py", str(init))
1403+
result = pytester.runpytest("-v", init)
1404+
result.stdout.fnmatch_lines(["sub/__init__.py::test_init PASSED*", "*1 passed in*"])
1405+
1406+
result = pytester.runpytest("-v", "-o", "python_files=*.py", subdir)
13961407
result.stdout.fnmatch_lines(["sub/__init__.py::test_init PASSED*", "*1 passed in*"])
13971408

13981409

testing/test_doctest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def my_func():
132132
""",
133133
}
134134
)
135-
reprec = pytester.inline_run(p, "--doctest-modules", "--import-mode=importlib")
135+
reprec = pytester.inline_run("--doctest-modules", "--import-mode=importlib")
136136
reprec.assertoutcome(passed=1)
137137

138138
def test_new_pattern(self, pytester: Pytester):

testing/test_nose.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ def test_it():
494494
pass
495495
""",
496496
)
497-
result = pytester.runpytest(p, "-p", "nose")
497+
result = pytester.runpytest(p.parent, "-p", "nose")
498498
assert result.ret == 0
499499

500500

0 commit comments

Comments
 (0)