Is it possible to parametrize a test with fixtures?

后端 未结 1 1020
长情又很酷
长情又很酷 2021-01-22 22:59

I would like to pass @pytest.mark.parametrize not particular values but fixtures. Like so.

Given a conftest with:

@pytest.fixture
def name1(         


        
1条回答
  •  隐瞒了意图╮
    2021-01-22 23:41

    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.

    0 讨论(0)
提交回复
热议问题