Skip to content

Commit

Permalink
[8.0.x] Improve error message when using @pytest.fixture twice (#11958)
Browse files Browse the repository at this point in the history
Co-authored-by: Florian Bruhin <me@the-compiler.org>
  • Loading branch information
github-actions[bot] and The-Compiler authored Feb 9, 2024
1 parent 23b91d1 commit e6f6be3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/_pytest/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -1196,7 +1196,7 @@ def __call__(self, function: FixtureFunction) -> FixtureFunction:

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"):
Expand Down
21 changes: 21 additions & 0 deletions testing/python/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -4352,6 +4352,27 @@ def fix():
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(
Expand Down

0 comments on commit e6f6be3

Please sign in to comment.