轉自: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 Files\Python35\python.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:\Users\wangli\PycharmProjects\Test\test 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 Files\Python35\python.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:\Users\wangli\PycharmProjects\Test\test 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 Files\Python35\python.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:\Users\wangli\PycharmProjects\Test\test 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 Files\Python35\python.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:\Users\wangli\PycharmProjects\Test\test 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 Files\Python35\python.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:\Users\wangli\PycharmProjects\Test\test 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