Skip to content

Commit e6f6be3

Browse files
[8.0.x] Improve error message when using @pytest.fixture twice (#11958)
Co-authored-by: Florian Bruhin <[email protected]>
1 parent 23b91d1 commit e6f6be3

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

Diff for: src/_pytest/fixtures.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1196,7 +1196,7 @@ def __call__(self, function: FixtureFunction) -> FixtureFunction:
11961196

11971197
if getattr(function, "_pytestfixturefunction", False):
11981198
raise ValueError(
1199-
"fixture is being applied more than once to the same function"
1199+
f"@pytest.fixture is being applied more than once to the same function {function.__name__!r}"
12001200
)
12011201

12021202
if hasattr(function, "pytestmark"):

Diff for: testing/python/fixtures.py

+21
Original file line numberDiff line numberDiff line change
@@ -4352,6 +4352,27 @@ def fix():
43524352
assert fix() == 1
43534353

43544354

4355+
def test_fixture_double_decorator(pytester: Pytester) -> None:
4356+
"""Check if an error is raised when using @pytest.fixture twice."""
4357+
pytester.makepyfile(
4358+
"""
4359+
import pytest
4360+
4361+
@pytest.fixture
4362+
@pytest.fixture
4363+
def fixt():
4364+
pass
4365+
"""
4366+
)
4367+
result = pytester.runpytest()
4368+
result.assert_outcomes(errors=1)
4369+
result.stdout.fnmatch_lines(
4370+
[
4371+
"E * ValueError: @pytest.fixture is being applied more than once to the same function 'fixt'"
4372+
]
4373+
)
4374+
4375+
43554376
def test_fixture_param_shadowing(pytester: Pytester) -> None:
43564377
"""Parametrized arguments would be shadowed if a fixture with the same name also exists (#5036)"""
43574378
pytester.makepyfile(

0 commit comments

Comments
 (0)