Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incorrect discovery of non-test __init__.py files. #6197

Merged
merged 2 commits into from
Nov 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/6194.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix incorrect discovery of non-test ``__init__.py`` files.
1 change: 1 addition & 0 deletions changelog/6197.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Revert "The first test in a package (``__init__.py``) marked with ``@pytest.mark.skip`` is now correctly skipped.".
24 changes: 6 additions & 18 deletions src/_pytest/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,21 +251,18 @@ class PyobjMixin(PyobjContext):
@property
def obj(self):
"""Underlying Python object."""
self._mount_obj_if_needed()
return self._obj

@obj.setter
def obj(self, value):
self._obj = value

def _mount_obj_if_needed(self):
obj = getattr(self, "_obj", None)
if obj is None:
self._obj = obj = self._getobj()
# XXX evil hack
# used to avoid Instance collector marker duplication
if self._ALLOW_MARKERS:
self.own_markers.extend(get_unpacked_marks(obj))
self.own_markers.extend(get_unpacked_marks(self.obj))
return obj

@obj.setter
def obj(self, value):
self._obj = value

def _getobj(self):
"""Gets the underlying Python object. May be overwritten by subclasses."""
Expand Down Expand Up @@ -432,14 +429,6 @@ def _genfunctions(self, name, funcobj):
class Module(nodes.File, PyCollector):
""" Collector for test classes and functions. """

def __init__(self, fspath, parent=None, config=None, session=None, nodeid=None):
if fspath.basename == "__init__.py":
self._ALLOW_MARKERS = False

nodes.FSCollector.__init__(
self, fspath, parent=parent, config=config, session=session, nodeid=nodeid
)

def _getobj(self):
return self._importtestmodule()

Expand Down Expand Up @@ -639,7 +628,6 @@ def isinitpath(self, path):
return path in self.session._initialpaths

def collect(self):
self._mount_obj_if_needed()
this_path = self.fspath.dirpath()
init_module = this_path.join("__init__.py")
if init_module.check(file=1) and path_matches_patterns(
Expand Down
21 changes: 21 additions & 0 deletions testing/test_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -1257,3 +1257,24 @@ def test_collector_respects_tbstyle(testdir):
"*= 1 error in *",
]
)


def test_does_not_eagerly_collect_packages(testdir):
testdir.makepyfile("def test(): pass")
pydir = testdir.mkpydir("foopkg")
pydir.join("__init__.py").write("assert False")
result = testdir.runpytest()
assert result.ret == ExitCode.OK


def test_does_not_put_src_on_path(testdir):
# `src` is not on sys.path so it should not be importable
testdir.tmpdir.join("src/nope/__init__.py").ensure()
testdir.makepyfile(
"import pytest\n"
"def test():\n"
" with pytest.raises(ImportError):\n"
" import nope\n"
)
result = testdir.runpytest()
assert result.ret == ExitCode.OK
asottile marked this conversation as resolved.
Show resolved Hide resolved
23 changes: 0 additions & 23 deletions testing/test_skipping.py
Original file line number Diff line number Diff line change
Expand Up @@ -1162,26 +1162,3 @@ def test_importorskip():
match="^could not import 'doesnotexist': No module named .*",
):
pytest.importorskip("doesnotexist")


def test_skip_package(testdir):
testdir.makepyfile(
__init__="""
import pytest
pytestmark = pytest.mark.skip
"""
)

testdir.makepyfile(
"""
import pytest
def test_skip1():
assert 0
def test_skip2():
assert 0
"""
)

result = testdir.inline_run()
_, skipped, _ = result.listoutcomes()
assert len(skipped) == 2