Skip to content

Commit 29010d2

Browse files
authored
Merge pull request #11217 from bluetech/fixtures-skip-xunit-loc
fixtures: show test as skip location if skipped from an xunit setup function
2 parents de1f6f5 + c5262b0 commit 29010d2

File tree

3 files changed

+16
-13
lines changed

3 files changed

+16
-13
lines changed

changelog/11216.improvement.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
If a test is skipped from inside an :ref:`xunit setup fixture <classic xunit>`, the test summary now shows the test location instead of the fixture location.

src/_pytest/fixtures.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,9 +1162,10 @@ def pytest_fixture_setup(
11621162
try:
11631163
result = call_fixture_func(fixturefunc, request, kwargs)
11641164
except TEST_OUTCOME as e:
1165-
if isinstance(e, skip.Exception) and not fixturefunc.__name__.startswith(
1166-
"xunit_setup"
1167-
):
1165+
if isinstance(e, skip.Exception):
1166+
# The test requested a fixture which caused a skip.
1167+
# Don't show the fixture as the skip location, as then the user
1168+
# wouldn't know which test skipped.
11681169
e._use_item_location = True
11691170
fixturedef.cached_result = (None, my_cache_key, e)
11701171
raise

testing/test_skipping.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -989,33 +989,34 @@ def test_skipped_reasons_functional(pytester: Pytester) -> None:
989989
pytester.makepyfile(
990990
test_one="""
991991
import pytest
992-
from conftest import doskip
992+
from helpers import doskip
993993
994-
def setup_function(func):
995-
doskip()
994+
def setup_function(func): # LINE 4
995+
doskip("setup function")
996996
997997
def test_func():
998998
pass
999999
1000-
class TestClass(object):
1000+
class TestClass:
10011001
def test_method(self):
1002-
doskip()
1002+
doskip("test method")
10031003
1004-
@pytest.mark.skip("via_decorator")
1004+
@pytest.mark.skip("via_decorator") # LINE 14
10051005
def test_deco(self):
10061006
assert 0
10071007
""",
1008-
conftest="""
1008+
helpers="""
10091009
import pytest, sys
1010-
def doskip():
1010+
def doskip(reason):
10111011
assert sys._getframe().f_lineno == 3
1012-
pytest.skip('test')
1012+
pytest.skip(reason) # LINE 4
10131013
""",
10141014
)
10151015
result = pytester.runpytest("-rs")
10161016
result.stdout.fnmatch_lines_random(
10171017
[
1018-
"SKIPPED [[]2[]] conftest.py:4: test",
1018+
"SKIPPED [[]1[]] test_one.py:7: setup function",
1019+
"SKIPPED [[]1[]] helpers.py:4: test method",
10191020
"SKIPPED [[]1[]] test_one.py:14: via_decorator",
10201021
]
10211022
)

0 commit comments

Comments
 (0)