From f5d2edc1fc90553f1ad0e2fef7ffa8f3251acb1c Mon Sep 17 00:00:00 2001 From: Daniel Valenzuela Date: Fri, 18 Nov 2022 09:20:38 -0300 Subject: [PATCH] [7.2.x] issue-10457/show test name when skipping from fixture --- AUTHORS | 1 + changelog/10457.bugfix.rst | 1 + src/_pytest/fixtures.py | 5 +++++ testing/test_skipping.py | 21 +++++++++++++++++++++ 4 files changed, 28 insertions(+) create mode 100644 changelog/10457.bugfix.rst diff --git a/AUTHORS b/AUTHORS index 7da1f8a0c57..37272c44840 100644 --- a/AUTHORS +++ b/AUTHORS @@ -88,6 +88,7 @@ Daniel Grana Daniel Hahler Daniel Nuri Daniel Sánchez Castelló +Daniel Valenzuela Zenteno Daniel Wandschneider Daniele Procida Danielle Jenkins diff --git a/changelog/10457.bugfix.rst b/changelog/10457.bugfix.rst new file mode 100644 index 00000000000..26522e9f0e1 --- /dev/null +++ b/changelog/10457.bugfix.rst @@ -0,0 +1 @@ +If a test is skipped from inside a fixture, the test summary now shows the test location instead of the fixture location. diff --git a/src/_pytest/fixtures.py b/src/_pytest/fixtures.py index d79895c262b..7ef261b969a 100644 --- a/src/_pytest/fixtures.py +++ b/src/_pytest/fixtures.py @@ -58,6 +58,7 @@ from _pytest.mark import ParameterSet from _pytest.mark.structures import MarkDecorator from _pytest.outcomes import fail +from _pytest.outcomes import skip from _pytest.outcomes import TEST_OUTCOME from _pytest.pathlib import absolutepath from _pytest.pathlib import bestrelpath @@ -1129,6 +1130,10 @@ def pytest_fixture_setup( except TEST_OUTCOME: exc_info = sys.exc_info() assert exc_info[0] is not None + if isinstance( + exc_info[1], skip.Exception + ) and not fixturefunc.__name__.startswith("xunit_setup"): + exc_info[1]._use_item_location = True # type: ignore[attr-defined] fixturedef.cached_result = (None, my_cache_key, exc_info) raise fixturedef.cached_result = (result, my_cache_key, None) diff --git a/testing/test_skipping.py b/testing/test_skipping.py index 6415480ef4f..892ed85476b 100644 --- a/testing/test_skipping.py +++ b/testing/test_skipping.py @@ -1439,6 +1439,27 @@ def test_pass(): ) +def test_skip_from_fixture(pytester: Pytester) -> None: + pytester.makepyfile( + **{ + "tests/test_1.py": """ + import pytest + def test_pass(arg): + pass + @pytest.fixture + def arg(): + condition = True + if condition: + pytest.skip("Fixture conditional skip") + """, + } + ) + result = pytester.runpytest("-rs", "tests/test_1.py", "--rootdir=tests") + result.stdout.fnmatch_lines( + ["SKIPPED [[]1[]] tests/test_1.py:2: Fixture conditional skip"] + ) + + def test_skip_using_reason_works_ok(pytester: Pytester) -> None: p = pytester.makepyfile( """