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?
JavaScript
x
16
16
1
@pytest.fixture(scope="function")
2
def fixture_func(self, request):
3
argA = request.param['argA'] if request.param.get('argA', None) else 'default value for argA'
4
argB = request.param['argB'] if request.param.get('argB', None) else 'default value for argB'
5
# do something with fixture
6
7
@pytest.mark.parametrize('fixture_func', [dict(argA='argA')], indirect=['fixture_func'])
8
def test_case_1(self, fixture_func):
9
#do something under testcase
10
pass
11
12
@pytest.mark.parametrize('fixture_func', [dict()], indirect=['fixture_func'])
13
def test_case_2(self, fixture_func):
14
#do something under testcase
15
pass
16
i wna to use as
JavaScript
1
4
1
def test_case_3(self, fixture_func):
2
#do something under testcase
3
pass
4
Advertisement
Answer
None
is the default result
JavaScript
1
2
1
request.param.get('argA', None)
2
So, you can just:
JavaScript
1
2
1
argA = request.param.get('argA', 'default value for argA')
2