转自:Python Pytest装饰器@pytest.mark.parametrize详解
Pytest中装饰器@pytest.mark.parametrize('参数名',list)可以实现测试用例参数化,类似DDT
如:@pytest.mark.parametrize('请求方式,接口地址,传参,预期结果',[('get','www.baidu.com','{"page":1}','{"code":0,"msg":"成功"})',('post','www.baidu.com','{"page":2}','{"code":0,"msg":"成功"}')])
1、第一个参数是字符串,多个参数中间用逗号隔开
2、第二个参数是list,多组数据用元祖类型;传三个或更多参数也是这样传。list的每个元素都是一个元组,元组里的每个元素和按参数顺序一一对应
3、传一个参数 @pytest.mark.parametrize('参数名',list) 进行参数化
4、传两个参数@pytest.mark.parametrize('参数名1,参数名2',[(参数1_data[0], 参数2_data[0]),(参数1_data[1], 参数2_data[1])]) 进行参数化
1 import pytest 2 #单参数单值 3 @pytest.mark.parametrize("user",["18221124104"]) 4 def test(user): 5 print(user) 6 assert user=="18221124104" 7 8 9 "C:Program FilesPython35python.exe" C:/Users/wangli/PycharmProjects/Test/test/test03.py 10 ============================= test session starts ============================= 11 platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0 12 rootdir: C:UserswangliPycharmProjectsTest est 13 collected 1 item 14 15 test03.py 18221124104 16 . 17 18 ============================== 1 passed in 0.15s ============================== 19 20 Process finished with exit code 0 21 22 23 24 #单参数多值 25 @pytest.mark.parametrize("user",["18221124104","18200000000","18200000001"]) 26 def test(user): 27 print(user) 28 assert user=="18221124104" 29 30 31 "C:Program FilesPython35python.exe" C:/Users/wangli/PycharmProjects/Test/test/test03.py 32 ============================= test session starts ============================= 33 platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0 34 rootdir: C:UserswangliPycharmProjectsTest est 35 collected 3 items 36 37 test03.py 18221124104 38 .18200000000 39 F18200000001 40 F 41 42 ================================== FAILURES =================================== 43 ______________________________ test[18200000000] ______________________________ 44 45 user = '18200000000' 46 47 @pytest.mark.parametrize("user",["18221124104","18200000000","18200000001"]) 48 def test(user): 49 print(user) 50 > assert user=="18221124104" 51 E AssertionError 52 53 test03.py:74: AssertionError 54 ______________________________ test[18200000001] ______________________________ 55 56 user = '18200000001' 57 58 @pytest.mark.parametrize("user",["18221124104","18200000000","18200000001"]) 59 def test(user): 60 print(user) 61 > assert user=="18221124104" 62 E AssertionError 63 64 test03.py:74: AssertionError 65 ========================= 2 failed, 1 passed in 0.21s ========================= 66 67 Process finished with exit code 0 68 69 70 71 #多参数多值 72 @pytest.mark.parametrize("user,pwd",[("18221124104",111111),("18200000000",111111)]) 73 def test(user,pwd): 74 print(user,pwd) 75 76 77 "C:Program FilesPython35python.exe" C:/Users/wangli/PycharmProjects/Test/test/test03.py 78 ============================= test session starts ============================= 79 platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0 80 rootdir: C:UserswangliPycharmProjectsTest est 81 collected 2 items 82 83 test03.py 18221124104 111111 84 .18200000000 111111 85 . 86 87 ============================== 2 passed in 0.03s ============================== 88 89 Process finished with exit code 0 90 91 92 93 # 使用内置的mark.xfail标记为失败的用例就不运行了,直接跳过显示xfailed 94 @pytest.mark.parametrize("user,pwd",[("18221124104",111111),pytest.param("18200000000",111111,marks=pytest.mark.xfail)]) 95 def test(user,pwd): 96 print(user,pwd) 97 assert user == "18221124104" 98 assert pwd== 111111 99 100 101 102 "C:Program FilesPython35python.exe" C:/Users/wangli/PycharmProjects/Test/test/test03.py 103 ============================= test session starts ============================= 104 platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0 105 rootdir: C:UserswangliPycharmProjectsTest est 106 collected 2 items 107 108 test03.py 18221124104 111111 109 .18200000000 111111 110 x 111 112 ======================== 1 passed, 1 xfailed in 0.14s ========================= 113 114 Process finished with exit code 0 115 116 117 118 119 #若要获得多个参数化参数的所有组合,可以堆叠参数化装饰器 120 @pytest.mark.parametrize("x", [0, 1]) 121 @pytest.mark.parametrize("y", [2, 3]) 122 def test_foo(x, y): 123 print("测试数据组合:x->%s, y->%s" % (x, y)) 124 125 if __name__=="__main__": 126 pytest.main(["-s","test03.py"]) 127 128 129 "C:Program FilesPython35python.exe" C:/Users/wangli/PycharmProjects/Test/test/test03.py 130 ============================= test session starts ============================= 131 platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0 132 rootdir: C:UserswangliPycharmProjectsTest est 133 collected 4 items 134 135 test03.py 测试数据组合:x->0, y->2 136 .测试数据组合:x->1, y->2 137 .测试数据组合:x->0, y->3 138 .测试数据组合:x->1, y->3 139 . 140 141 ============================== 4 passed in 0.03s ============================== 142 143 Process finished with exit code 0 144 145 #json传参 146 user = [ 147 { 148 "aa": 'aa1', 149 "bb": 'bb1' 150 }, 151 { 152 "aa": 'aa2', 153 "bb": 'bb2' 154 } 155 ] 156 @pytest.mark.parametrize("user", user) 157 def test(user): 158 print("aa: %s, bb: %s" % (user['aa'], user['bb'])) 159 160 ===================== test session starts ===================== 161 platform darwin -- Python 3.9.2, pytest-6.2.2, py-1.10.0, pluggy-0.13.1 162 rootdir: /Users/admin/PythonProject/audit_auto_test/audit_web_i18n_test 163 collected 2 items 164 165 tests/test_pytest.py .. 166 ====================== 2 passed in 0.01s ======================
————————————————
版权声明:本文为CSDN博主「王大力测试进阶之路」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_36502272/article/details/100986069