I have a unit test I'm running with a particular fixture. The fixture is pretty simple and looks like so:
@pytest.fixture
def patch_s3():
# Create empty bucket
bucket = s3_resource().create_bucket(Bucket=BUCKET)
bucket.objects.all().delete()
yield
bucket.objects.all().delete()
and the test isn't all that complicated either. Here is the signature for the test:
def test_task_asg(patch_s3, tmp_path, monkeypatch)
However, when I run pytest, I get the following error:
tests/test_task_asg.py:313: in <module>
@pytest.fixture
home/ubuntu/miniconda/envs/pipeline/lib/python3.7/site-packages/_pytest/fixtures.py:1150: in fixture
fixture_function
home/ubuntu/miniconda/envs/pipeline/lib/python3.7/site-packages/_pytest/fixtures.py:1011: in __call__
"fixture is being applied more than once to the same function"
E ValueError: fixture is being applied more than once to the same function
This is weird to me, as it seems to imply I'm somehow including patch_s3
more than once in test_task_asg
. What could be causing this?
If you scroll up from your definition of patch_s3
, you'll probably find another duplicate line like @pytest.fixture
, causing the issue. That line can only be used once per fixture method definition.
The easiest way to reproduce this issue is with:
import pytest
@pytest.fixture(scope="session")
@pytest.fixture
def my_fixture():
yield
def test_abc(my_fixture):
pass
Running that with pytest
yields ValueError: fixture is being applied more than once to the same function
. But if you remove one of the two @pytest.fixture
lines, then the issue goes away.
Here are some examples where other people had the same issue and fixed it: