我有更好的方法为参数pytest.fixture
函数提供默认值吗?
我有几个测试用例需要在testcase之前运行fixture_func
,如果没有提供,我想在fixture中使用参数的默认值。我能想出的唯一代码如下。我有更好的方法吗?
@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
答案 0 :(得分:1)
None
是默认结果
request.param.get('argA', None)
所以,你可以:
argA = request.param.get('argA', 'default value for argA')
答案 1 :(得分:0)
将参数添加到您的装置:
@pytest.fixture(scope='function', params=[{'arg1': 'default'}, {'arg2': 'default'}])
def my_fixture(request):
return request.param['arg1'] + request.param['arg1']