0

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

Kfir Cohen
  • 43
  • 6

1 Answers1

0

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
Devang Sanghani
  • 731
  • 5
  • 14
  • The problem is that I can't change the fixture name. let me give you a more complicated example, where the 'fixture_value' fixture is used as a value for another fixture, which can't be modified. Maybe I'm making it way more complicated than it should be, but I'm trying to see if there is a clean solution for this. – Kfir Cohen Mar 22 '22 at 14:24
  • You can keep the original name for fixture. Just change what you pass in parametrize. It can be anything, it doesn't matter. – Devang Sanghani Mar 22 '22 at 14:42