4

Im trying to use list of one fixture values as parameters into another. Here is my setup:

import pytest

d = {
    "first": [1, 2, 3],
    "second": [4, 5, 6]
}

@pytest.fixture(params=['first', 'second'])
def foo(request):
    return d.get(request.param)

@pytest.fixture(params=[pytest.lazy_fixture('foo')])
def bar(request):
    return request.param

def test_1(bar):
    pass

The problem is bar() always get full list as request.param ([1, 2, 3] not values of the list. If in params of bar() fixture send data directly, e.g:

@pytest.fixture(params=[1, 2, 3])
def bar(request):
    return request.param

def test_1(bar):
    pass

Then argument request will work properly (test starts three times). The same situation, if I pass arguments to params not directly, but from any method without fixture decorator, i.e:

def some():
    return [1, 2, 3]

@pytest.fixture(params=some())
def more(request):
    return request.param

def test_2(more):
    logging.error(more)
    pass

So, my question is it possible to obtain data from list one by one and then use it in my test? I've try to 'unparse' list:

@pytest.fixture(params=[i for i in i pytest.lazy_fixture('foo')])
def bar(request):
    return request.param

But in this case I get TypeError: 'LazyFixture' object is not iterable

grmmvv
  • 41
  • 1
  • 3

1 Answers1

4

See this answer to a very similar question: by design, fixture values can not be used as lists of parameters to parametrize tests. Indeed parameters are resolved at pytest collection phase, while, fixtures are resolved later, during pytest nodes execution.

The best that you can do with lazy_fixture or with pytest_cases.fixture_ref is to use a single fixture value as a parameter. With lazy_fixture you have limitations (if I remember correctly, the fixture cannot be parametrized) while with pytest_cases you can do pretty much anything (using the fixture_ref in a parameter tuple, using several fixture_refs each with different parametrization, etc.). I'm the author of pytest_cases by the way ;)

See also this thread.

smarie
  • 4,568
  • 24
  • 39
  • Great answer ! thanks – rsacchettini Jan 19 '22 at 21:20
  • [smaire] my problem is that I want to parameterize a fixture with comma-separated values passed as a custom command line argument I added. So I need to parameterize a fixture (not test) with another fixture. Will this come under phase B. – Sandeep Dharembra Jan 10 '23 at 15:32