Is it possible to parametrize a test with fixtures?

大兔子大兔子 提交于 2019-12-02 01:35:43

I would insist in parametrizing the fixture

In your example this would amount to:

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

def test_name(name):
    print(name)

Another alternative:

@pytest.fixture(params=[1,2])
def name(name1, name2, request):
    if request.param == 1:
        return name1()
    elif request.param == 2:
        return name2()

def test_name(name):
    print(name)

Which approach works best for you will depend on the specifics of your tests and perhaps on what pre-existing fixtures you might be trying to leverage.

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