首页 > 编程语言 > 详细

Python Pytest装饰器@pytest.mark.parametrize详解

时间:2021-03-10 16:27:55      阅读:51      评论:0      收藏:0      [点我收藏+]

转自: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  


————————————————
版权声明:本文为CSDN博主「王大力测试进阶之路」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_36502272/article/details/100986069

Python Pytest装饰器@pytest.mark.parametrize详解

原文:https://www.cnblogs.com/hi3254014978/p/14512172.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!