Skip to content

Commit 5ca8159

Browse files
authored
Merge pull request #4744 from blueyed/fix-4592-collectfile
Fix handling of collect_ignore from parent conftest
2 parents ea73246 + 913a2da commit 5ca8159

File tree

4 files changed

+22
-5
lines changed

4 files changed

+22
-5
lines changed

changelog/4592.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix handling of ``collect_ignore`` via parent ``conftest.py``.

src/_pytest/main.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,7 @@ def filter_(f):
607607
yield y
608608

609609
def _collectfile(self, path, handle_dupes=True):
610+
assert path.isfile()
610611
ihook = self.gethookproxy(path)
611612
if not self.isinitpath(path):
612613
if ihook.pytest_ignore_collect(path=path, config=self.config):

src/_pytest/python.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,7 @@ def gethookproxy(self, fspath):
599599
return proxy
600600

601601
def _collectfile(self, path, handle_dupes=True):
602+
assert path.isfile()
602603
ihook = self.gethookproxy(path)
603604
if not self.isinitpath(path):
604605
if ihook.pytest_ignore_collect(path=path, config=self.config):
@@ -642,11 +643,12 @@ def collect(self):
642643
):
643644
continue
644645

645-
if path.isdir() and path.join("__init__.py").check(file=1):
646-
pkg_prefixes.add(path)
647-
648-
for x in self._collectfile(path):
649-
yield x
646+
if path.isdir():
647+
if path.join("__init__.py").check(file=1):
648+
pkg_prefixes.add(path)
649+
else:
650+
for x in self._collectfile(path):
651+
yield x
650652

651653

652654
def _get_xunit_setup_teardown(holder, attr_name, param_obj=None):

testing/test_collection.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1144,3 +1144,16 @@ def test_nodeid(request):
11441144
]
11451145
)
11461146
assert result.ret == 0
1147+
1148+
1149+
def test_collectignore_via_conftest(testdir, monkeypatch):
1150+
"""collect_ignore in parent conftest skips importing child (issue #4592)."""
1151+
tests = testdir.mkpydir("tests")
1152+
tests.ensure("conftest.py").write("collect_ignore = ['ignore_me']")
1153+
1154+
ignore_me = tests.mkdir("ignore_me")
1155+
ignore_me.ensure("__init__.py")
1156+
ignore_me.ensure("conftest.py").write("assert 0, 'should_not_be_called'")
1157+
1158+
result = testdir.runpytest()
1159+
assert result.ret == EXIT_NOTESTSCOLLECTED

0 commit comments

Comments
 (0)