参数化参数
# 多个参数必须是tuple或list
@pytest.mark.parametrize(argnames='a,b,sum',argvalues=[(1,1,2),(-1,1,0)], ids=['正整数之和', '正整数和负整数之和'])
def test_01(a,b,sum):
response = requests.request(method='get', url=url('/api/sum'), json={'a': a, 'sum': sum})
assert response.json()['sum'] == sum
# 合并为单个参数
@pytest.mark.parametrize(argnames='data', argvalues=[{'json': {'a': 1, 'b': 1}, 'expect': {'sum': 2}},
{'json': {'a':-1, 'b': 1}, 'expect': {'sum': 0}}], , ids=['正整数之和', '正整数和负整数之和'])
def test_01(data):
expect = data['expect']
response = requests.request(method='get', url=url('/api/sum'), json=data['json'])
assert response.json()['sum'] == expect['sum']
参数化中文用例名编码问题
# conftest.py
def pytest_collection_modifyitems(items):
"""
测试用例收集完成时,将收集到的item的name和nodeid的中文显示在控制台上
:return:
"""
for item in items:
item.name = item.name.encode("utf-8").decode("unicode_escape")
item._nodeid = item.nodeid.encode("utf-8").decode("unicode_escape")