From 2867e8e41bdeb605e91cb6727891f799cadcd0c6 Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Sat, 27 May 2023 14:42:19 +0300 Subject: [PATCH] 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 #3749 Fixes #8976 Fixes #9263 Fixes #9313 --- changelog/8976.breaking.rst | 5 ++++ src/_pytest/python.py | 4 ++- .../package_init_given_as_arg/pkg/__init__.py | 2 ++ .../package_init_given_as_arg/pkg/test_foo.py | 2 +- testing/python/collect.py | 11 +++++--- testing/test_collection.py | 25 +++++++++++++------ testing/test_doctest.py | 2 +- testing/test_nose.py | 2 +- 8 files changed, 39 insertions(+), 14 deletions(-) create mode 100644 changelog/8976.breaking.rst diff --git a/changelog/8976.breaking.rst b/changelog/8976.breaking.rst new file mode 100644 index 00000000000..bd9a63982b9 --- /dev/null +++ b/changelog/8976.breaking.rst @@ -0,0 +1,5 @@ +Running `pytest pkg/__init__.py` now collects the `pkg/__init__.py` file (module) only. +Previously, it collected the entire `pkg` package, including other test files in the directory, but excluding tests in the `__init__.py` file itself +(unless :confval:`python_files` was changed to allow `__init__.py` file). + +To collect the entire package, specify just the directory: `pytest pkg`. diff --git a/src/_pytest/python.py b/src/_pytest/python.py index 3b1253cf4b7..6f26f08c6e2 100644 --- a/src/_pytest/python.py +++ b/src/_pytest/python.py @@ -739,7 +739,9 @@ def collect(self) -> Iterable[Union[nodes.Item, nodes.Collector]]: this_path = self.path.parent # Always collect the __init__ first. - if path_matches_patterns(self.path, self.config.getini("python_files")): + if self.session.isinitpath(self.path) or path_matches_patterns( + self.path, self.config.getini("python_files") + ): yield Module.from_parent(self, path=self.path) pkg_prefixes: Set[Path] = set() diff --git a/testing/example_scripts/collect/package_init_given_as_arg/pkg/__init__.py b/testing/example_scripts/collect/package_init_given_as_arg/pkg/__init__.py index e69de29bb2d..9cd366295e7 100644 --- a/testing/example_scripts/collect/package_init_given_as_arg/pkg/__init__.py +++ b/testing/example_scripts/collect/package_init_given_as_arg/pkg/__init__.py @@ -0,0 +1,2 @@ +def test_init(): + pass diff --git a/testing/example_scripts/collect/package_init_given_as_arg/pkg/test_foo.py b/testing/example_scripts/collect/package_init_given_as_arg/pkg/test_foo.py index f174823854e..8f2d73cfa4f 100644 --- a/testing/example_scripts/collect/package_init_given_as_arg/pkg/test_foo.py +++ b/testing/example_scripts/collect/package_init_given_as_arg/pkg/test_foo.py @@ -1,2 +1,2 @@ -def test(): +def test_foo(): pass diff --git a/testing/python/collect.py b/testing/python/collect.py index 41845517bf0..5a104186e63 100644 --- a/testing/python/collect.py +++ b/testing/python/collect.py @@ -1407,10 +1407,15 @@ def test_package_collection_infinite_recursion(pytester: Pytester) -> None: def test_package_collection_init_given_as_argument(pytester: Pytester) -> None: - """Regression test for #3749""" + """Regression test for #3749, #8976, #9263, #9313. + + Specifying an __init__.py file directly should collect only the __init__.py + Module, not the entire package. + """ p = pytester.copy_example("collect/package_init_given_as_arg") - result = pytester.runpytest(p / "pkg" / "__init__.py") - result.stdout.fnmatch_lines(["*1 passed*"]) + items, hookrecorder = pytester.inline_genitems(p / "pkg" / "__init__.py") + assert len(items) == 1 + assert items[0].name == "test_init" def test_package_with_modules(pytester: Pytester) -> None: diff --git a/testing/test_collection.py b/testing/test_collection.py index bbcb358b6ff..2958bbddc90 100644 --- a/testing/test_collection.py +++ b/testing/test_collection.py @@ -1362,19 +1362,27 @@ def test_collect_pkg_init_and_file_in_args(pytester: Pytester) -> None: p = subdir.joinpath("test_file.py") p.write_text("def test_file(): pass") - # NOTE: without "-o python_files=*.py" this collects test_file.py twice. - # This changed/broke with "Add package scoped fixtures #2283" (2b1410895) - # initially (causing a RecursionError). - result = pytester.runpytest("-v", str(init), str(p)) + # Just the package directory, the __init__.py module is filtered out. + result = pytester.runpytest("-v", subdir) result.stdout.fnmatch_lines( [ "sub/test_file.py::test_file PASSED*", + "*1 passed in*", + ] + ) + + # But it's included if specified directly. + result = pytester.runpytest("-v", init, p) + result.stdout.fnmatch_lines( + [ + "sub/__init__.py::test_init PASSED*", "sub/test_file.py::test_file PASSED*", "*2 passed in*", ] ) - result = pytester.runpytest("-v", "-o", "python_files=*.py", str(init), str(p)) + # Or if the pattern allows it. + result = pytester.runpytest("-v", "-o", "python_files=*.py", subdir) result.stdout.fnmatch_lines( [ "sub/__init__.py::test_init PASSED*", @@ -1389,10 +1397,13 @@ def test_collect_pkg_init_only(pytester: Pytester) -> None: init = subdir.joinpath("__init__.py") init.write_text("def test_init(): pass") - result = pytester.runpytest(str(init)) + result = pytester.runpytest(subdir) result.stdout.fnmatch_lines(["*no tests ran in*"]) - result = pytester.runpytest("-v", "-o", "python_files=*.py", str(init)) + result = pytester.runpytest("-v", init) + result.stdout.fnmatch_lines(["sub/__init__.py::test_init PASSED*", "*1 passed in*"]) + + result = pytester.runpytest("-v", "-o", "python_files=*.py", subdir) result.stdout.fnmatch_lines(["sub/__init__.py::test_init PASSED*", "*1 passed in*"]) diff --git a/testing/test_doctest.py b/testing/test_doctest.py index d2944fa2bcf..9ec1a84d667 100644 --- a/testing/test_doctest.py +++ b/testing/test_doctest.py @@ -132,7 +132,7 @@ def my_func(): """, } ) - reprec = pytester.inline_run(p, "--doctest-modules", "--import-mode=importlib") + reprec = pytester.inline_run("--doctest-modules", "--import-mode=importlib") reprec.assertoutcome(passed=1) def test_new_pattern(self, pytester: Pytester): diff --git a/testing/test_nose.py b/testing/test_nose.py index e838e79ddd5..0347ab90df3 100644 --- a/testing/test_nose.py +++ b/testing/test_nose.py @@ -494,7 +494,7 @@ def test_it(): pass """, ) - result = pytester.runpytest(p, "-p", "nose") + result = pytester.runpytest(p.parent, "-p", "nose") assert result.ret == 0