[Python] How to extend fixture from the base class?

Discussão em 'Python' iniciado por Stack, Julho 25, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to extend a fixture: that means not only overriding, but partially reusing the old one. For example, in the code below the fixture Test.values uses the fixture values and concatenates lists defined in both functions:

    import pytest

    @pytest.fixture
    def values():
    return [1, 2, 3]

    class Test:
    @pytest.fixture
    def values(self, values):
    return values + [4, 5, 6]

    def test_values(self, values):
    assert 1 in values
    assert 2 in values
    assert 3 in values
    assert 4 in values
    assert 5 in values
    assert 6 in values


    I want to do the same for a fixture defined as a class member:

    class Base:
    @pytest.fixture
    def values(self):
    return [1, 2, 3]

    class Test(Base):
    @pytest.fixture
    def values(self, values):
    return values + [4, 5, 6]

    def test_values(self, values):
    assert 1 in values
    assert 2 in values
    assert 3 in values
    assert 4 in values
    assert 5 in values
    assert 6 in values


    This time I'm getting an error:


    E recursive dependency involving fixture 'values' detected
    ​

    The definition of the second fixture doesn't see the first one, and that is because there is no function overloading in Python. Please advise a way to overcome the issue.

    Continue reading...

Compartilhe esta Página