Provide default argument value for py.test fixture function

大兔子大兔子 提交于 2021-02-09 10:12:57

问题


Is there a better way for me to provide default value for argument pytest.fixture function?

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

i wna to use as

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

回答1:


None is the default result

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

So, you can just:

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


来源:https://stackoverflow.com/questions/47758406/provide-default-argument-value-for-py-test-fixture-function

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!