Skip to content

Commit

Permalink
Merge pull request #3798 from jonozzz/fix-3751
Browse files Browse the repository at this point in the history
Fix #3751
  • Loading branch information
RonnyPfannschmidt committed Aug 16, 2018
2 parents 64faa41 + 27b5435 commit 939a792
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/_pytest/main.py
Expand Up @@ -505,8 +505,9 @@ def _collect(self, arg):
root = self._node_cache[pkginit]
else:
col = root._collectfile(pkginit)
if col and isinstance(col, Package):
root = col[0]
if col:
if isinstance(col[0], Package):
root = col[0]
self._node_cache[root.fspath] = root

# If it's a directory argument, recurse and look for any Subpackages.
Expand Down
11 changes: 9 additions & 2 deletions testing/test_collection.py
Expand Up @@ -638,16 +638,23 @@ def test_global_file(self, testdir, tmpdir):
assert col.config is config

def test_pkgfile(self, testdir):
"""Verify nesting when a module is within a package.
The parent chain should match: Module<x.py> -> Package<subdir> -> Session.
Session's parent should always be None.
"""
tmpdir = testdir.tmpdir
subdir = tmpdir.join("subdir")
x = subdir.ensure("x.py")
subdir.ensure("__init__.py")
with subdir.as_cwd():
config = testdir.parseconfigure(x)
col = testdir.getnode(config, x)
assert isinstance(col, pytest.Module)
assert col.name == "x.py"
assert col.parent.parent is None
assert isinstance(col, pytest.Module)
assert isinstance(col.parent, pytest.Package)
assert isinstance(col.parent.parent, pytest.Session)
# session is batman (has no parents)
assert col.parent.parent.parent is None
for col in col.listchain():
assert col.config is config

Expand Down

0 comments on commit 939a792

Please sign in to comment.