Skip to content Skip to sidebar Skip to footer

Initing A Pytest Fixture With A Parameter

I have a fixture that creates an object I use in many tests. This Object has a property - priority (1 - 10) Now, there are a lot of tests that need to 'know' the priority of the ob

Solution 1:

You can do :

@pytest.fixturedefmy_object(priority):
    return MyObj(priority)    

deftest_foo(my_object):
    obj_1 = my_object(1)
    assert something # 1
    obj_2 = my_object(2)
    assert something # 2

I come with this idea, then I found this Q&A :

py.test: Pass a parameter to a fixture function

I think your question maybe consider a duplicate if your problem is solved.

Solution 2:

You can do something like this, it will return run your fixture with each of those params:

@pytest.fixture(
    params=[
        "param1",
        "param2",
        "param3",
        "param4",
        "param5",
        "param6",
    ]
)deffixture_with_params(request):
    return request.param

Post a Comment for "Initing A Pytest Fixture With A Parameter"