问题
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