Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a fixture that creates a list of items during tests. I want to have another fixture which is parametrized with values generated by the first one.

Example code

import random
import pytest

@pytest.fixture
def values():
    return [random.randint(0, 100) for _ in range(10)]


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


@pytest.mark.parametrize("value", params=values):
def test_function(value):
    assert value > 0

The problem with above code is that values is a function and not a list. I did quite a lot of digging but didnt find any way to unpack fixture to parametrize another with it.

Im aware that i can pass values fixture and iterate over it in tests, but that not a good solution since i want to see which values cause test to fail.

Im also open to alternative solutions, for example if it is possible to run subtests from started test.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
794 views
Welcome To Ask or Share your Answers For Others

1 Answer

This seems like a misunderstanding of the concept of fixtures and its difference with the concept of parameters.

Pytest has two major phases:

  • a collection phase where the goal is to create a list of test "nodes" to run. One test "node" corresponds to one test id, and means one value for each parameter. In this phase fixtures are NOT executed, only the decorator marks (containing parameters) are read. Therefore only parameters declared in the decorators can influence this phase.

  • an execution phase where each test node is run. Before the run, all required fixtures that are not already setup are setup. Therefore the fixture functions are executed in this phase, and only in this phase. Their results can not modify the list of tests already done in the previous phase.

In your example, you want the result of a fixture setup (phase B) to change the list of tests to create (phase A): this is not possible by design. You have to create this list somewhere else, for example in a pytest init hook in a conftest.py or simply as a shared variable in any of your test modules, and refer to it in the parameters of your test or fixture.

See also this question that is quite similar: Parametrizing tests depending of also parametrized values in pytest

Note that to complement hoefling's comment to your question you can now use a parametrized fixture in a parameters list: I have added this feature in my pytest-cases plugin, for evaluation, so that we can eventually propose to merge it within pytest (see this discussion, so not hesitate to provide feedback!). But unfortunately this will not solve the precise problem you describe in this post, for the fundamental reason described above.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...