From cfd16d0dac1c935952b892bfbc225978c9b25de3 Mon Sep 17 00:00:00 2001 From: ApaDoctor Date: Thu, 5 Oct 2017 11:45:35 +0300 Subject: [PATCH 1/3] provide error fixture applied to the same func provide error fixture applied to the same func provide error fixture applied to the same func --- _pytest/fixtures.py | 4 ++++ changelog/2334.feature | 1 + 2 files changed, 5 insertions(+) create mode 100644 changelog/2334.feature diff --git a/_pytest/fixtures.py b/_pytest/fixtures.py index 64d21b9f6..b9d1070ce 100644 --- a/_pytest/fixtures.py +++ b/_pytest/fixtures.py @@ -827,6 +827,10 @@ class FixtureFunctionMarker: 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..5aec67994 --- /dev/null +++ b/changelog/2334.feature @@ -0,0 +1 @@ +Now when @pytest.fixture is being applied to the same function ValueError is raised. \ No newline at end of file From a7a39f136450e4608a61e0d77336ffd21328a915 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Wed, 18 Oct 2017 19:26:10 -0200 Subject: [PATCH 2/3] Update CHANGELOG formatting --- changelog/2334.feature | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog/2334.feature b/changelog/2334.feature index 5aec67994..5af168526 100644 --- a/changelog/2334.feature +++ b/changelog/2334.feature @@ -1 +1 @@ -Now when @pytest.fixture is being applied to the same function ValueError is raised. \ No newline at end of file +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. From 03850cf962e9c3c9b8d05a63a2fde0ba8ec0f7fa Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Mon, 23 Apr 2018 22:17:46 -0300 Subject: [PATCH 3/3] Add test for applying fixture twice Fix #2334 --- testing/python/fixture.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/testing/python/fixture.py b/testing/python/fixture.py index 1e58a5550..26cde4c93 100644 --- a/testing/python/fixture.py +++ b/testing/python/fixture.py @@ -2812,6 +2812,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):