Skip to content

Commit 85288b5

Browse files
authored
fix bug with nonskipped first test in package (pytest-dev#5831)
fix bug with nonskipped first test in package
2 parents 5f9db8a + 5cefcb2 commit 85288b5

File tree

4 files changed

+43
-6
lines changed

4 files changed

+43
-6
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ Daniel Hahler
7070
Daniel Nuri
7171
Daniel Wandschneider
7272
Danielle Jenkins
73+
Daniil Galiev
7374
Dave Hunt
7475
David Díaz-Barquero
7576
David Mohr

changelog/5830.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix fail skipping the first test in package marked as ``skip``

src/_pytest/python.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -251,18 +251,21 @@ class PyobjMixin(PyobjContext):
251251
@property
252252
def obj(self):
253253
"""Underlying Python object."""
254+
self._mount_obj_if_needed()
255+
return self._obj
256+
257+
@obj.setter
258+
def obj(self, value):
259+
self._obj = value
260+
261+
def _mount_obj_if_needed(self):
254262
obj = getattr(self, "_obj", None)
255263
if obj is None:
256264
self._obj = obj = self._getobj()
257265
# XXX evil hack
258266
# used to avoid Instance collector marker duplication
259267
if self._ALLOW_MARKERS:
260-
self.own_markers.extend(get_unpacked_marks(self.obj))
261-
return obj
262-
263-
@obj.setter
264-
def obj(self, value):
265-
self._obj = value
268+
self.own_markers.extend(get_unpacked_marks(obj))
266269

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

435+
def __init__(self, fspath, parent=None, config=None, session=None, nodeid=None):
436+
if fspath.basename == "__init__.py":
437+
self._ALLOW_MARKERS = False
438+
439+
nodes.FSCollector.__init__(
440+
self, fspath, parent=parent, config=config, session=session, nodeid=nodeid
441+
)
442+
432443
def _getobj(self):
433444
return self._importtestmodule()
434445

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

630641
def collect(self):
642+
self._mount_obj_if_needed()
631643
this_path = self.fspath.dirpath()
632644
init_module = this_path.join("__init__.py")
633645
if init_module.check(file=1) and path_matches_patterns(

testing/test_skipping.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,3 +1162,26 @@ def test_importorskip():
11621162
match="^could not import 'doesnotexist': No module named .*",
11631163
):
11641164
pytest.importorskip("doesnotexist")
1165+
1166+
1167+
def test_skip_package(testdir):
1168+
testdir.makepyfile(
1169+
__init__="""
1170+
import pytest
1171+
pytestmark = pytest.mark.skip
1172+
"""
1173+
)
1174+
1175+
testdir.makepyfile(
1176+
"""
1177+
import pytest
1178+
def test_skip1():
1179+
assert 0
1180+
def test_skip2():
1181+
assert 0
1182+
"""
1183+
)
1184+
1185+
result = testdir.inline_run()
1186+
_, skipped, _ = result.listoutcomes()
1187+
assert len(skipped) == 2

0 commit comments

Comments
 (0)