From e6f6be3bc9e876f1853fdea68ec49cfc1c4c246d Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Fri, 9 Feb 2024 14:34:45 +0000 Subject: [PATCH] [8.0.x] Improve error message when using @pytest.fixture twice (#11958) Co-authored-by: Florian Bruhin --- src/_pytest/fixtures.py | 2 +- testing/python/fixtures.py | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/_pytest/fixtures.py b/src/_pytest/fixtures.py index dabdf0e42..206fd084a 100644 --- a/src/_pytest/fixtures.py +++ b/src/_pytest/fixtures.py @@ -1196,7 +1196,7 @@ class FixtureFunctionMarker: if getattr(function, "_pytestfixturefunction", False): raise ValueError( - "fixture is being applied more than once to the same function" + f"@pytest.fixture is being applied more than once to the same function {function.__name__!r}" ) if hasattr(function, "pytestmark"): diff --git a/testing/python/fixtures.py b/testing/python/fixtures.py index 5512b602d..37c09a9bd 100644 --- a/testing/python/fixtures.py +++ b/testing/python/fixtures.py @@ -4352,6 +4352,27 @@ def test_call_fixture_function_error(): assert fix() == 1 +def test_fixture_double_decorator(pytester: Pytester) -> None: + """Check if an error is raised when using @pytest.fixture twice.""" + pytester.makepyfile( + """ + import pytest + + @pytest.fixture + @pytest.fixture + def fixt(): + pass + """ + ) + result = pytester.runpytest() + result.assert_outcomes(errors=1) + result.stdout.fnmatch_lines( + [ + "E * ValueError: @pytest.fixture is being applied more than once to the same function 'fixt'" + ] + ) + + def test_fixture_param_shadowing(pytester: Pytester) -> None: """Parametrized arguments would be shadowed if a fixture with the same name also exists (#5036)""" pytester.makepyfile(