46

I just want to understand what does it mean or what happens if I set indirect parameter to True or False in the pytest.mark.parametrize?

vvvvv
  • 25,404
  • 19
  • 49
  • 81
Froodo
  • 573
  • 1
  • 6
  • 16
  • https://stackoverflow.com/questions/18011902/pass-a-parameter-to-a-fixture-function/33879151#33879151 It is used to pass a parameter to a fixture – CrazyVideoGamer Jan 04 '21 at 03:16

1 Answers1

47

With indirect=True you can parametrize your fixture, False - default value. Example:

import pytest

@pytest.fixture
def fixture_name(request):
    return request.param

@pytest.mark.parametrize('fixture_name', ['foo', 'bar'], indirect=True)
def test_indirect(fixture_name):
    assert fixture_name == 'baz'

So this example generates two tests. First one gets from fixture_name value foo, because this fixture for this test runs with parametization. Second test gets bar value. And each tests will fail, because of assert checking for baz.

Sergei Voronezhskii
  • 2,184
  • 19
  • 32
  • 2
    indirect=True ensures you still execute the fixture body when you use parametrize on a test. – MechanTOurS Jan 02 '18 at 12:22
  • 17
    So `indirect` is a way to parametrize fixtures. And the main difference compared to parametrize the fixture using `pytest.fixture(params=[...])` is that the fixture can be parametrized only for the specific test function (otherwise it always parametrize for all test functions that use the fixture) ? – SuperGeo Nov 20 '18 at 00:05
  • 1
    How would you assert the returns in this case? – GabrielChu Jan 09 '20 at 23:49
  • 13
    I just discovered that it's necessary that the fixture's argument (that will accept the parameter) is called `request`. Other names will not work, and pytest will complain that that other-named fixture does not exist. pytest 6.2.2 – t-mart Feb 11 '21 at 07:19
  • how do you use named parameters where `['foo', 'bar']` is? – red888 Apr 28 '22 at 13:30