How to parametrize a Pytest fixture

This is actually possible via indirect parametrization.

This example does what you want with pytest 3.1.2:

import pytest

class TimeLine:
    def __init__(self, instances):
        self.instances = instances

@pytest.fixture
def timeline(request):
    return TimeLine(request.param)

@pytest.mark.parametrize(
    'timeline',
    ([1, 2, 3], [2, 4, 6], [6, 8, 10]),
    indirect=True
)
def test_timeline(timeline):
    for instance in timeline.instances:
        assert instance % 2 == 0

if __name__ == "__main__":
    pytest.main([__file__])

Also see this similar question.


This is a confusing topic, because people tend to think that fixtures and parameters are the same thing. They are not and are not even collected in the same phase of the pytest run. By design, parameters are meant to be collected when tests are collected (the list of test nodes is under construction), while fixtures are meant to be executed when test nodes are run (the list of test nodes is fixed and cannot be modified). So fixture contents can not be used to change the number of test nodes - this is the role of parameters. See also my detailed answer here.

This is the reason why your question has no solution "as is": you should put the Timeline instances in a parameter, not in a fixture. Like this:

import pytest

class TimeLine(object):
    instances = [0, 1, 2]

@pytest.fixture(params=TimeLine().instances)
def timeline(request):
    return request.param

def test_timeline(timeline):
    assert timeline % 2 == 0

Kurt Peek's answer mentions another topic that IMHO adds confusion here because it is not directly related to your question. Since it is mentioned, and even accepted as the solution, let me elaborate: indeed in pytest, you can not use a fixture in a @pytest.mark.parametrize. But even if you could that would not change anything.

Since this feature is now available in pytest-cases (I'm the author), you can try yourself. The only way to make your example work even with that feature, is still the same: to remove the list from the fixture. So you end up with:

import pytest
from pytest_cases import parametrize, fixture_ref

class TimeLine(object):
    instances = [0, 1, 2]

@pytest.fixture(params=TimeLine().instances)
def timeline(request):
    return request.param

@parametrize("t", [fixture_ref(timeline)])
def test_timeline(t):
    assert t % 2 == 0

Which is the same than previous example with an extra, probably useless, layer. Note: see also this discussion.


instead of indirect parametrization, or my hacky solution below involving inheritance, you can also just use the params argument to @pytest.fixture() -- i think this is the simplest solution maybe?

import pytest

class TimeLine:
    def __init__(self, instances=[0, 0, 0]):
        self.instances = instances


@pytest.fixture(params=[
    [1, 2, 3], [2, 4, 5], [6, 8, 10]
])
def timeline(request):
    return TimeLine(request.param)


def test_timeline(timeline):
    for instance in timeline.instances:
        assert instance % 2 == 0

https://docs.pytest.org/en/latest/how-to/fixtures.html#parametrizing-fixtures

Tags:

Python

Pytest