这篇文章主要介绍了Python中Pytest装饰器@pytest.mark.parametrize有什么用,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获、下面让小编带着大家一起了解一下。
Pytest中装饰器@pytest.mark.parametrize(& # 39;参数名& # 39;,列表)可以实现测试用例参数化、类似DDT
如:@pytest.mark.parametrize(& # 39;请求方式,接口地址,传参,预期结果& # 39;,((& # 39;得到# 39;& # 39;www.baidu.com& # 39;, & # 39; {“page": 1} & # 39;, & # 39; {“code": 0,“msg":“成功“})& # 39;,(& # 39;文章# 39;& # 39;www.baidu.com& # 39;, & # 39; {“page": 2} & # 39;, & # 39; {“code": 0,“msg":“成功“}& # 39;)])
1,第一个参数是字符串,多个参数中间用逗号隔开
2,第二个参数是列表,多组数据用元祖类型;传三个或更多参数也是这样传.list的每个元素都是一个元组,元组里的每个元素和按参数顺序一一对应
3,传一个参数@pytest.mark.parametrize(& # 39;参数名& # 39;,列表)进行参数化
4,传两个参数@pytest.mark.parametrize(& # 39;参数名1,参数名2 & # 39;,[(参数1 _data[0],参数2 _data[0]),(参数1 _data[1],参数2 _data[1])))进行参数化
import Pytest #单参数单值 @pytest.mark.parametrize (“user" (“18221124104“)) def 测试(用户): ,,,印刷(用户) ,,,assert 用户==?8221124104”; , “C: \ Program 文件\ Python35 \ python.exe", C:/用户/王丽/PycharmProjects/测试/测试/test03.py=============================,test session starts =============================platform win32 ——, Python 3.5.2,, pytest-5.1.2,, py-1.8.0, pluggy-0.12.0 rootdir: C: \ \王丽\ PycharmProjects \ \测试用户 collected 1项 , test03.py 18221124104 . ,==============================,1,passed 0.15 s 拷贝==============================, Process finished  with exit  code 0 , #单参数多值 @pytest.mark.parametrize (“user" (“18221124104”,“18200000000”,“18200000001”)) def 测试(用户): ,,,印刷(用户) ,,,assert 用户==?8221124104”; , , “C: \ Program 文件\ Python35 \ python.exe", C:/用户/王丽/PycharmProjects/测试/测试/test03.py=============================,test session starts =============================platform win32 ——, Python 3.5.2,, pytest-5.1.2,, py-1.8.0, pluggy-0.12.0 rootdir: C: \ \王丽\ PycharmProjects \ \测试用户 collected 3,物品 , test03.py 18221124104 .18200000000 F18200000001 F ,==================================,FAILURES ===================================______________________________ 测试(18200000000)______________________________ , 时间=user & # 39; 18200000000 & # 39; , ,,,@pytest.mark.parametrize (“user", (“18221124104”,“18200000000”,“18200000001”)) ,,,def 测试(用户): ,,,,,,,印刷(用户) 在,,,,,,,assert 用户==?8221124104”; E ,,,,, AssertionError , test03.py: 74: AssertionError ______________________________ 测试(18200000001)______________________________ , 时间=user & # 39; 18200000001 & # 39; , ,,,@pytest.mark.parametrize (“user", (“18221124104”,“18200000000”,“18200000001”)) ,,,def 测试(用户): ,,,,,,,印刷(用户) 在,,,,,,,assert 用户==?8221124104”; E ,,,,, AssertionError , test03.py: 74: AssertionError=========================,2,失败了,,1,passed 0.21 s 拷贝=========================, Process finished  with exit  code 0 #多参数多值 @pytest.mark.parametrize(“用户、pwd"[(“18221124104“, 111111),(111111年“18200000000”,))) pwd def 测试(用户): ,,,print(用户、pwd) ,, “C: \ Program 文件\ Python35 \ python.exe", C:/用户/王丽/PycharmProjects/测试/测试/test03.py=============================,test session starts =============================platform win32 ——, Python 3.5.2,, pytest-5.1.2,, py-1.8.0, pluggy-0.12.0 rootdir: C: \ \王丽\ PycharmProjects \ \测试用户 collected 2,物品 , test03.py 18221124104, 111111 .18200000000 111111 . ,==============================,2,passed 0.03 s 拷贝==============================, Process finished  with exit  code 0 , #,使用内置的mark.xfail标记为失败的用例就不运行了,直接跳过显示xfailed @pytest.mark.parametrize(“用户、pwd" [(“18221124104“, 111111), pytest.param(“18200000000“, 111111年,标志着=pytest.mark.xfail))) pwd def 测试(用户): ,,,print(用户、pwd) ,,,assert user ==,“18221124104”; null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null nullPython中Pytest装饰器@pytest.mark.parametrize有什么用