Pytest override fixture with parameterization

1.1k Views Asked by At

The answer to how to override a fixture with parametrization is already given here: pytest test parameterization override

My question is what is the correct way to run the same test, both with the original fixture and the overridden values.

I'm already using the pytest-lazy-fixture library to override one fixture with another, but when I run the following pytest:

import pytest
from pytest_lazyfixture import lazy_fixture

@pytest.fixture
def fixture_value():
    return 0

def test_foo(fixture_value):
    ...

@pytest.mark.parametrize('fixture_value', (lazy_fixture('fixture_value'), 1, 2, 3))
def test_bar(fixture_value):
    ...

I get this error:

E recursive dependency involving fixture 'fixture_value' detected

1

There are 1 best solutions below

2
Devang Sanghani On

The error is because fixture_value name of the fixture is same as the paramter passed to the test.

import pytest
from pytest_lazyfixture import lazy_fixture

@pytest.fixture
def fixture_value():
    return 0



def test_foo(fixture_value):
    assert True

@pytest.mark.parametrize('fixtures_value'[(pytest.lazy_fixture('fixture_value'))]) # Square brackets used here.
def test_bar(fixtures_value): # Note the name here is changed.
    assert True

Output:

Platform win32 -- Python 3.9.2, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
plugins: lazy-fixture-0.6.3
collected 2 items

myso_test.py::test_foo PASSED
myso_test.py::test_bar[fixtures_value0] PASSED