1、单个参数传参
class Test_D:
@pytest.mark.parametrize("a", [1, 2, 3]) # 参数a被赋予3个值,test_a将会运行3遍
def test_a(self, a): # 参数必须和parametrize里面的参数一致
print('\n------------------> test_a has ran, and a = {}'.format(a))
assert 1 == a
import pytest
class Test_D:
data = [{"name": "John", "age": 25}, {"name": "Alice", "age": 30}]
@pytest.mark.parametrize('param1', data)
def test_function(self,param1):
print(param1)
if __name__ == '__main__':
pytest.main(['-s', 'pymysqlconnect.py'])
2、多个参数传参
class Test_D:
@pytest.mark.parametrize("a,b", [(1, 2), (2, 3), (3, 4)]) # 参数a,b均被赋予三个值,函数会运行三遍
def test_b(self, a, b): # 参数必须和parametrize里面的参数一致
print('\n------------------> test_b has ran, and a = {}, b = {}'.format(a, b))
assert 1
3、利用函数的返回值进行用例参数化 把拿到的json数据转成符合pytest参数化的格式,即[('用例名称',请求参数),('用例名称',请求参数)]然后传参
@pytest.mark.parametrize("title,rep", get_data('Scheduling_Workstation.json', key='scheduling_list_select'))
get_data=[('通过姓名查询', {'data': {'querystring': '测试'}, 'page': '0', 'size': '100'}), ('通过住院号查询', {'data': {'querystring': '000111'}, 'page': '0', 'size': '100'}), ('通过类型=急诊查询', {'data': {'type': 2}, 'page': '0', 'size': '100'}), ('通过类型=择期查询', {'data': {'type': 1}, 'page': '0', 'size': '100'})]
返回一个列表,列表里有很多元组,元组里是字典,有title,rep