Search code examples
pythonpython-3.xpytestiterablepytest-fixtures

How to pass a list of fixtures to a test case in Pytest?


I have a case where I need a list of fixtures (similar ones) inside a test case and perform same operation with those fixtures.

For ex:

def testcase(fixture1, fixture2, fixture3):
    # do some operation with fixture1
    # do the same operation with fixture2
    # do the same operation with fixture3

The thing is I have multiple such fixtures. Around 15 or so. I'm looking for a way where I can mention these fixtures in a list like fixtures_list = [fixture1, fixture2, fixture3, ...] and update the list whenever needed.

And use a for loop inside the test case to perform operations on with the fixture like

for fixture in fixtures_list:
    #do something with fixture

But I'm unable to do it as we must definitely pass the fixture to the test case.

Is there any way to overcome this?


Solution

  • I was able to achieve this by creating another fixture and returning the fixtures inside that fixture.

    def fixture_that_returns_other_fixtures(fix1, fix2, fix3):
        return fix1, fix2, fix3
    

    and used fixture_that_returns_other_fixtures in all other tests wherever needed.