Can params passed to pytest fixture be passed in as a variable?

两盒软妹~` 提交于 2019-11-30 12:45:02

If i understand your question correctly, you basically want to select one instance of a parametrized fixture for executing with a test, by providing some info with the test. It's not possible although we could probably think about a mechanism. I am not sure if the following solution maps to your whole problem, but here is one way to solve the above concrete case:

import pytest

@pytest.fixture(scope="module")
def myFixture1():
    return 5

@pytest.fixture(scope="module")
def myFixture2():
    return 2

@pytest.fixture(scope="module", params=["param1", "param2"])
def myFixture(request):
    if request.param == 'param1':
        return request.getfuncargvalue("myFixture1")
    elif request.param == 'param2':
        return request.getfuncargvalue("myFixture2")

def test_1(myFixture1):
    assert myFixture1 == 5

def test_2(myFixture2):
    assert myFixture2 == 2

def test_all(myFixture):
    assert myFixture in (2,5)

This runs four tests, because the test_all is executed twice with both fixtures.

If the setup of your fixtures is not heavy, you might also have one fixture that produces a list and an "iterating" parametrized one. A test could then grab the whole list and index it into it.

Not sure if this is what you want, but the example case can be implemented like:

@pytest.mark.parametrize(('param', 'expected'), [('param1', 5), ('param2', 10)])
def test_madeup(param, expected):
    assert param == expected
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!