Skip to content

Commit 3c2f90b

Browse files
authored
Merge pull request #10513 from pytest-dev/backport-10482-to-7.2.x
[7.2.x] issue-10457/show test name when skipping from fixture
2 parents 47d6adf + f5d2edc commit 3c2f90b

File tree

4 files changed

+28
-0
lines changed

4 files changed

+28
-0
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ Daniel Grana
8888
Daniel Hahler
8989
Daniel Nuri
9090
Daniel Sánchez Castelló
91+
Daniel Valenzuela Zenteno
9192
Daniel Wandschneider
9293
Daniele Procida
9394
Danielle Jenkins

changelog/10457.bugfix.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 a fixture, the test summary now shows the test location instead of the fixture location.

src/_pytest/fixtures.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
from _pytest.mark import ParameterSet
5959
from _pytest.mark.structures import MarkDecorator
6060
from _pytest.outcomes import fail
61+
from _pytest.outcomes import skip
6162
from _pytest.outcomes import TEST_OUTCOME
6263
from _pytest.pathlib import absolutepath
6364
from _pytest.pathlib import bestrelpath
@@ -1129,6 +1130,10 @@ def pytest_fixture_setup(
11291130
except TEST_OUTCOME:
11301131
exc_info = sys.exc_info()
11311132
assert exc_info[0] is not None
1133+
if isinstance(
1134+
exc_info[1], skip.Exception
1135+
) and not fixturefunc.__name__.startswith("xunit_setup"):
1136+
exc_info[1]._use_item_location = True # type: ignore[attr-defined]
11321137
fixturedef.cached_result = (None, my_cache_key, exc_info)
11331138
raise
11341139
fixturedef.cached_result = (result, my_cache_key, None)

testing/test_skipping.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1439,6 +1439,27 @@ def test_pass():
14391439
)
14401440

14411441

1442+
def test_skip_from_fixture(pytester: Pytester) -> None:
1443+
pytester.makepyfile(
1444+
**{
1445+
"tests/test_1.py": """
1446+
import pytest
1447+
def test_pass(arg):
1448+
pass
1449+
@pytest.fixture
1450+
def arg():
1451+
condition = True
1452+
if condition:
1453+
pytest.skip("Fixture conditional skip")
1454+
""",
1455+
}
1456+
)
1457+
result = pytester.runpytest("-rs", "tests/test_1.py", "--rootdir=tests")
1458+
result.stdout.fnmatch_lines(
1459+
["SKIPPED [[]1[]] tests/test_1.py:2: Fixture conditional skip"]
1460+
)
1461+
1462+
14421463
def test_skip_using_reason_works_ok(pytester: Pytester) -> None:
14431464
p = pytester.makepyfile(
14441465
"""

0 commit comments

Comments
 (0)