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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
import pytest #单参数单值 @pytest .mark.parametrize( "user" ,[ "18221124104" ]) def test(user): print (user) assert user = = "18221124104" "C:\Program Files\Python35\python.exe" C: / Users / wangli / PycharmProjects / Test / test / 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:\Users\wangli\PycharmProjects\Test\test collected 1 item test03.py 18221124104 . = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 1 passed in 0.15s = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = Process finished with exit code 0 #单参数多值 @pytest .mark.parametrize( "user" ,[ "18221124104" , "18200000000" , "18200000001" ]) def test(user): print (user) assert user = = "18221124104" "C:\Program Files\Python35\python.exe" C: / Users / wangli / PycharmProjects / Test / test / 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:\Users\wangli\PycharmProjects\Test\test collected 3 items test03.py 18221124104 . 18200000000 F18200000001 F = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = FAILURES = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = ______________________________ test[ 18200000000 ] ______________________________ user = '18200000000' @pytest .mark.parametrize( "user" ,[ "18221124104" , "18200000000" , "18200000001" ]) def test(user): print (user) > assert user = = "18221124104" E AssertionError test03.py: 74 : AssertionError ______________________________ test[ 18200000001 ] ______________________________ user = '18200000001' @pytest .mark.parametrize( "user" ,[ "18221124104" , "18200000000" , "18200000001" ]) def test(user): print (user) > assert user = = "18221124104" E AssertionError test03.py: 74 : AssertionError = = = = = = = = = = = = = = = = = = = = = = = = = 2 failed, 1 passed in 0.21s = = = = = = = = = = = = = = = = = = = = = = = = = Process finished with exit code 0 #多参数多值 @pytest .mark.parametrize( "user,pwd" ,[( "18221124104" , 111111 ),( "18200000000" , 111111 )]) def test(user,pwd): print (user,pwd) "C:\Program Files\Python35\python.exe" C: / Users / wangli / PycharmProjects / Test / test / 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:\Users\wangli\PycharmProjects\Test\test collected 2 items test03.py 18221124104 111111 . 18200000000 111111 . = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 2 passed in 0.03s = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = Process finished with exit code 0 # 使用内置的mark.xfail标记为失败的用例就不运行了,直接跳过显示xfailed @pytest .mark.parametrize( "user,pwd" ,[( "18221124104" , 111111 ),pytest.param( "18200000000" , 111111 ,marks = pytest.mark.xfail)]) def test(user,pwd): print (user,pwd) assert user = = "18221124104" assert pwd = = 111111 "C:\Program Files\Python35\python.exe" C: / Users / wangli / PycharmProjects / Test / test / 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:\Users\wangli\PycharmProjects\Test\test collected 2 items test03.py 18221124104 111111 . 18200000000 111111 x = = = = = = = = = = = = = = = = = = = = = = = = 1 passed, 1 xfailed in 0.14s = = = = = = = = = = = = = = = = = = = = = = = = = Process finished with exit code 0 #若要获得多个参数化参数的所有组合,可以堆叠参数化装饰器 @pytest .mark.parametrize( "x" , [ 0 , 1 ]) @pytest .mark.parametrize( "y" , [ 2 , 3 ]) def test_foo(x, y): print ( "测试数据组合:x->%s, y->%s" % (x, y)) if __name__ = = "__main__" : pytest.main([ "-s" , "test03.py" ]) "C:\Program Files\Python35\python.exe" C: / Users / wangli / PycharmProjects / Test / test / 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:\Users\wangli\PycharmProjects\Test\test collected 4 items test03.py 测试数据组合:x - > 0 , y - > 2 .测试数据组合:x - > 1 , y - > 2 .测试数据组合:x - > 0 , y - > 3 .测试数据组合:x - > 1 , y - > 3 . = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 4 passed in 0.03s = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = Process finished with exit code 0 |
到此这篇关于Python Pytest装饰器@pytest.mark.parametrize详解的文章就介绍到这了,更多相关pytest.mark.parametrize内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/qq_36502272/article/details/100986069