Using a fixture inside pytest.mark.parametrize

Viewed 557

Say I have a test function that takes a parametrize record as a dict, where one of its values is a fixture which is already defined.

For example, we have a fixture:

@pytest.fixture
def a_value():
    return "some_value"

And the test function:

@pytest.mark.parametrize("record", [{"a": a_value, "other": "other_value"},
                                    {"a": a_value, "another": "another_value"}])
def test_record(record):
    do_something(record)

Now, I know that this can be solved by passing the fixture to the test function and updating the record accordingly, like:

@pytest.mark.parametrize("record", [{"other": "other_value"},
                                    {"another": "another_value"}])
def test_record(a_value, record):
    record["a"] = a_value
    do_something(record)

But I was wondering if there is a way of doing this without this "workaround", when I have many fixtures that are already defined and I just want to use them in each parametrized record I pass to the function.

I have already checked this question, although it doesn't seem to fit my case exactly. Couldn't find a correct use from the answers there.

1 Answers

One solution is to create record as a fixture rather than using parametrize and accept a_value as a paraemter:

@pytest.fixture
def record(a_value):
    return {
        'a': a_value,
        'other': 'other_value',
    }
Related