diff --git a/_pytest/fixtures.py b/_pytest/fixtures.py index 6190dea01..fa16fea64 100644 --- a/_pytest/fixtures.py +++ b/_pytest/fixtures.py @@ -851,6 +851,11 @@ class FixtureFunctionMarker(object): if isclass(function): raise ValueError( "class fixtures not supported (may be in the future)") + + if getattr(function, "_pytestfixturefunction", False): + raise ValueError( + "fixture is being applied more than once to the same function") + function._pytestfixturefunction = self return function diff --git a/changelog/2334.feature b/changelog/2334.feature new file mode 100644 index 000000000..5af168526 --- /dev/null +++ b/changelog/2334.feature @@ -0,0 +1 @@ +Now when ``@pytest.fixture`` is applied more than once to the same function a ``ValueError`` is raised. This buggy behavior would cause surprising problems and if was working for a test suite it was mostly by accident. diff --git a/testing/python/fixture.py b/testing/python/fixture.py index c558ea3cf..2ba890d05 100644 --- a/testing/python/fixture.py +++ b/testing/python/fixture.py @@ -2992,6 +2992,14 @@ class TestShowFixtures(object): Hi from test module ''') + def test_fixture_disallow_twice(self): + """Test that applying @pytest.fixture twice generates an error (#2334).""" + with pytest.raises(ValueError): + @pytest.fixture + @pytest.fixture + def foo(): + pass + @pytest.mark.parametrize('flavor', ['fixture', 'yield_fixture']) class TestContextManagerFixtureFuncs(object):