有关是否应将代码优化为test_prefix_2的问题。我有一个测试用例,需要传递在某些情况下已更改且某些参数未更改的不同参数。类似于方法1或method2的两种不同写法 但是,由于参数为state,所以country仅用于此测试用例。每个人都喜欢编写像method2这样的测试用例吗? shortpoint就是这样,函数parse_para将仅使用一次
import pytest
#method 1
@pytest.fixture
def funcFix(request):
return
#do something with request
@pytest.mark.parametrize('funcFix', [dict(name='google', age=7, state='CA', country='US'),
dict(name='apple', age=2, state='CA', country='US'),
dict(name='amazon', age=4, state='CA', country='US'),
dict(name='facebook', age=1, state='CA', country='US'),
dict(name='stackoverflow', age=6, state='CA', country='US'),
dict(name='walmart', age=8, state='CA', country='US'),
dict(name='wholefood', age=5, state='CA', country='US'),
dict(name='target', age=1, state='CA', country='US'),
], indirect=['funcFix'])
def test_prefix(funcFix):
#do some test
pass
#method 2
def parse_para(**kwargs):
return {**kwargs, **dict(state='CA', country='US')}
@pytest.mark.parametrize('funcFix', [parse_para(name='google', age=7),
parse_para(name='apple', age=2),
parse_para(name='amazon', age=4),
parse_para(name='facebook', age=1),
parse_para(name='stackoverflow', age=6),
parse_para(name='walmart', age=8),
parse_para(name='wholefood', age=5),
parse_para(name='target', age=1),
], indirect=['funcFix'])
def test_prefix2(funcFix):
#do some test
pass