为 py.test 夹具函数提供默认参数值 [英] Provide default argument value for py.test fixture function
本文介绍了为 py.test 夹具函数提供默认参数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有更好的方法为参数 pytest.fixture
函数提供默认值?
Is there a better way for me to provide default value for argument pytest.fixture
function?
我有几个测试用例需要在测试用例之前运行 fixture_func
,如果没有提供它们,我想在 fixture 中使用默认值作为参数.我能想出的唯一代码如下.我有什么更好的方法吗?
I have a couple of testcases that requires to run fixture_func
before testcase and I would like to use default value for argument in fixture if none of them is provided. The only code I can come up with is as follows. Any better way for me to do it?
@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')
这篇关于为 py.test 夹具函数提供默认参数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文