是否有更好的方法为参数pytest.fixture提供默认值?

我有几个测试用例,它们需要在测试用例之前运行fixture_func,如果没有提供它们,我想在夹具中使用默认值作为参数。我唯一能想到的代码如下。我有更好的方法吗?

@pytest.fixture(scope="function")
def fixture_func(self, request):
    argA = request.param['argA'] if request.param.get('argA', None) else 'default value for argA'
    argB = request.param['argB'] if request.param.get('argB', None) else 'default value for argB'
    # do something with fixture

@pytest.mark.parametrize('fixture_func', [dict(argA='argA')], indirect=['fixture_func'])
def test_case_1(self, fixture_func):
    #do something under testcase
    pass

@pytest.mark.parametrize('fixture_func', [dict()], indirect=['fixture_func'])
def test_case_2(self, fixture_func):
    #do something under testcase
    pass


我想用作

def test_case_3(self, fixture_func):
    #do something under testcase
    pass

最佳答案

None是默认结果

request.param.get('argA', None)


因此,您可以:

argA = request.param.get('argA', 'default value for argA')

09-12 23:46