How to concatenate several parametrized fixtures into a new fixture in py.test?

后端 未结 3 1527
清酒与你
清酒与你 2020-12-30 04:46

If I have two parametrized fixtures, how can I create a single test function that is called first with the instances of one fixture and then with the instances of the other

3条回答
  •  旧时难觅i
    2020-12-30 05:50

    It is not beautiful, but may be today you know the better way.

    Request object inside 'all' fixture know only about own params: 'lower', 'upper'. One way using fixtures from a fixture function.

    import pytest
    
    @pytest.fixture(params=[1, 2, 3])
    def lower(request):
        return "i" * request.param
    
    @pytest.fixture(params=[1, 2])
    def upper(request):
        return "I" * request.param
    
    @pytest.fixture(params=['lower', 'upper'])
    def all(request, lower, upper):
        if request.param == 'lower':
            return lower
        else:
            return upper
    
    def test_all(all):
        assert 0, all
    

提交回复
热议问题