Python Pytest unpack fixture

老子叫甜甜 提交于 2019-12-31 03:56:05

问题


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.


回答1:


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.

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 as a beta 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.



来源:https://stackoverflow.com/questions/50231627/python-pytest-unpack-fixture

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!