Skip to content

Commit ca04769

Browse files
Merge pull request #3751 from nicoddemus/collect-file-bug
Workaround for #3742
2 parents 7e92930 + 8c9efd8 commit ca04769

File tree

6 files changed

+21
-2
lines changed

6 files changed

+21
-2
lines changed

src/_pytest/main.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,8 @@ def collect(self):
482482
self.trace.root.indent -= 1
483483

484484
def _collect(self, arg):
485+
from _pytest.python import Package
486+
485487
names = self._parsearg(arg)
486488
argpath = names.pop(0)
487489
paths = []
@@ -503,7 +505,7 @@ def _collect(self, arg):
503505
root = self._node_cache[pkginit]
504506
else:
505507
col = root._collectfile(pkginit)
506-
if col:
508+
if col and isinstance(col, Package):
507509
root = col[0]
508510
self._node_cache[root.fspath] = root
509511

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import pytest
2+
3+
4+
class CustomItem(pytest.Item, pytest.File):
5+
def runtest(self):
6+
pass
7+
8+
9+
def pytest_collect_file(path, parent):
10+
return CustomItem(path, parent)

testing/example_scripts/fixtures/custom_item/foo/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def test():
2+
pass

testing/python/fixture.py

+5
Original file line numberDiff line numberDiff line change
@@ -1618,6 +1618,11 @@ def test_package(one):
16181618
reprec = testdir.inline_run()
16191619
reprec.assertoutcome(passed=2)
16201620

1621+
def test_collect_custom_items(self, testdir):
1622+
testdir.copy_example("fixtures/custom_item")
1623+
result = testdir.runpytest("foo")
1624+
result.stdout.fnmatch_lines("*passed*")
1625+
16211626

16221627
class TestAutouseDiscovery(object):
16231628
@pytest.fixture

testing/test_collection.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,7 @@ def test_pkgfile(self, testdir):
647647
col = testdir.getnode(config, x)
648648
assert isinstance(col, pytest.Module)
649649
assert col.name == "x.py"
650-
assert col.parent.parent.parent is None
650+
assert col.parent.parent is None
651651
for col in col.listchain():
652652
assert col.config is config
653653

0 commit comments

Comments
 (0)