111

Consider the following Pytest:

import pytest

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

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

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

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

The test test_timeline uses a Pytest fixture, timeline, which itself has the attribute instances. This attribute is iterated over in the test, so that the test only passes if the assertion holds for every instance in timeline.instances.

What I actually would like to do, however, is to generate 3 tests, 2 of which should pass and 1 of which would fail. I've tried

@pytest.mark.parametrize("instance", timeline.instances)
def test_timeline(timeline):
    assert instance % 2 == 0

but this leads to

AttributeError: 'function' object has no attribute 'instances'

As I understand it, in Pytest fixtures the function 'becomes' its return value, but this seems to not have happened yet at the time the test is parametrized. How can I set up the test in the desired fashion?

Kurt Peek
  • 52,165
  • 91
  • 301
  • 526
  • 1
    Does this answer your question? [Pass a parameter to a fixture function](https://stackoverflow.com/questions/18011902/pass-a-parameter-to-a-fixture-function) – congusbongus Mar 23 '21 at 21:54

7 Answers7

88

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.

imiric
  • 8,315
  • 4
  • 35
  • 39
  • 4
    Your test fails with `FAILED indir-param.py::test_timeline[timeline0] - assert (1 % 2) == 0` :-) (I know it doesn't matter for the answer). – gerrit Dec 03 '21 at 10:38
  • 2
    Yes, it should fail. 1 divided by 2 does not have a remainder (modulus) of 0. – wordsforthewise Sep 27 '22 at 12:50
59

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

hwjp
  • 15,359
  • 7
  • 71
  • 70
44

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.

smarie
  • 4,568
  • 24
  • 39
  • Important aspect here is that it the parameter should be called `request` in the test method signature. Mentioning that because I thought this name was arbitrary. – Victor Augusto Sep 01 '23 at 09:00
23

The use of indirect parametrization works, but I find the need to use request.param as a magic, unnamed variable a little awkard.

Here's a pattern I've used. It's awkward in a different way, arguably, but perhaps you'll prefer it too!

import pytest

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


@pytest.fixture
def instances():
    return [0, 0, 0]


@pytest.fixture
def timeline(instances):
    return TimeLine(instances)


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

the timeline fixture depends on another fixture called instances, which has a default vale of [0,0,0], but in the actual test, we use parametrize to inject a series of different values for instances.

the advantage as I see it is that everything has a good name, plus you don't need to pass that indirect=True flag.

hwjp
  • 15,359
  • 7
  • 71
  • 70
21

From Using fixtures in pytest.mark.parametrize pytest issue, it would appear that it is currently not possible to use fixtures in pytest.mark.parametrize.

Piotr Dobrogost
  • 41,292
  • 40
  • 236
  • 366
Kurt Peek
  • 52,165
  • 91
  • 301
  • 526
  • 4
    this is incorrect, the parametrize mark accepts indirect=True or indirect=['specific', 'named'] to say which arguments will have respective fixtures invoked. – msudder Jul 15 '19 at 15:53
  • 1
    The proposal referenced in your post has been moved to GitHub at the fillowing URL https://github.com/pytest-dev/pytest/issues/349. – Romain Sep 22 '19 at 13:36
  • 4
    As commented by @msudder, look up [indirect parameterization](https://docs.pytest.org/en/stable/example/parametrize.html#indirect-parametrization) – lifebalance Mar 24 '21 at 17:21
2

It is possible with vanilla pytest

test_numbers.py

import pytest


@pytest.fixture(params=[1, 2, 3, 4, 5])
def number(request):
    yield request.param


def test_number_is_integer(number):
    assert isinstance(number, int)

Output

test_numbers.py::test_number_is_integer[1] PASSED
test_numbers.py::test_number_is_integer[2] PASSED
test_numbers.py::test_number_is_integer[3] PASSED
test_numbers.py::test_number_is_integer[4] PASSED
test_numbers.py::test_number_is_integer[5] PASSED
Pithikos
  • 18,827
  • 15
  • 113
  • 136
1

Your parametrize paramset is referenceing the function. Perhaps you mean to reference Timeline.instances, not the fixture function /timeline/:

@pytest.mark.parametrize("instance", Timeline.instances)
def test_func(instance):
    pass

I think you are looking to add a mark xfail to one set of parameters, perhaps with a reason?

def make_my_tests():
    return [0, 2, mark.xfail(1, reason='not even')]

@mark.parametrize('timeline', paramset=make_my_tests(), indirect=True)
def test_func(timeline):
    assert timeline % 2 == 0
Danijel
  • 8,198
  • 18
  • 69
  • 133
msudder
  • 505
  • 3
  • 14