python参数化处理:使用参数化装饰器、解析列表、元祖、字典等数据

在pytest中,也可以使用参数化测试,即每组参数都独立执行一次测试。

使用的工具就是pytest.mark.parametrize(argnames,argvalues)。  注:argnames参数名称,argvalues参数值。

 1 """
 2 在pytest中,也可以使用参数化测试,即每组参数都独立执行一次测试
 3 使用的工具就是 pytest.mark.paramtrize(argnames,argvalues)
 4 例如:username,password
 5 1.用户名错误,密码正确
 6 2.用户名正确,密码错误
 7 3.用户名错误,密码错误
 8 4.用户名正确,密码正确
 9 这种情况下需要用参数化,只需要传参数
10 """
11 
12 import pytest
13 #列表
14 
15 data = ['123','456']
16 @pytest.mark.parametrize('pwd',data)     #argnames参数名称需要和方法里面的名称做对应,data指的是参数值。argnames可以传多个参数,以逗号来分割。
17 def test1(pwd):
18     print(pwd)
19 
20 #元祖
21 data2 = [(1,2,3,),
22          (4,5,6)
23 ]
24 @pytest.mark.parametrize('a,b,c',data2 )
25 def test2(a,b,c):
26     print(a,b,c)
27 
28 #字典
29 data3 = ({
30     'user':'wangfuyan',
31     'pwd':'123456'
32 },
33 {
34     'age':2,
35     'email':'tom@qq.com'
36 })
37 @pytest.mark.parametrize('dic',data3)
38 def test3(dic):
39     print(dic)
40 
41 data_1 = [
42     pytest.param(1,2,3,id="(a+b):pass"),   #id的值可以自定义,只要方便理解每个用例是干什么的即可
43     pytest.param(4,5,10,id="(a+b:fail")
44 ]
45 def add(a,b):
46     return a+b
47 class TestParametrize(object):
48     @pytest.mark.parametrize('a,b,expect',data_1 )
49     def test_parametrize_1(self,a,b,expect):
50         assert add(a,b) == expect

 

 1 import pytest
 2 import math
 3 
 4 #pytest参数化
 5 @pytest.mark.parametrize(
 6     "base,exponent,expeceted",          #base,exponent,expected用于定义参数的名称
 7     [(2,2,4),
 8      (2,3,8),
 9      (1,9,1),
10      (0,9,0)],
11      ids = ["case1","case2","case3","case4"]        #ids参数默认为None,用于定义测试用例的名称
12 )
13 def test_pow(base,exponent,expeceted):
14     assert math.pow(base,exponent) == expeceted     #math模块的pow()方法用于计算x的y次方的值

 

posted @ 2021-09-11 13:36  小黄鸭调试法  阅读(205)  评论(0)    收藏  举报
/*simplememory*/ #google_ad_c1, #google_ad_c2 {display:none;} .syntaxhighlighter a, .syntaxhighlighter div, .syntaxhighlighter code, .syntaxhighlighter table, .syntaxhighlighter table td, .syntaxhighlighter table tr, .syntaxhighlighter table tbody, .syntaxhighlighter table thead, .syntaxhighlighter table caption, .syntaxhighlighter textarea { font-size: 14px!important; } #home { opacity: 0.80; margin: 0 auto; width: 85%; min-width: 950px; background-color: #fff; padding: 30px; margin-top: 30px; margin-bottom: 50px; box-shadow: 0 2px 6px rgba(100, 100, 100, 0.3); } #blogTitle h1 { font-size: 30px; font-weight: bold; font-family: "Comic Sans MS"; line-height: 1.5em; margin-top: 20px; color: #515151; } #navList a:hover { color: #4C9ED9; text-decoration: none; } #navList a { display: block; width: 5em; height: 22px; float: left; text-align: center; padding-top: 18px; } #navigator { font-size: 15px; border-bottom: 1px solid #ededed; border-top: 1px solid #ededed; height: 50px; clear: both; margin-top: 25px; } .catListTitle { margin-top: 21px; margin-bottom: 10.5px; text-align: left; border-left: 10px solid rgba(82, 168, 236, 0.8); padding: 10px 0 14px 10px; background-color: #f5f5f5; } #ad_under_post_holder #google_ad_c1,#google_ad_c2{ display: none !important; } body { color: #000; background: url(https://i.cnblogs.com/albums/edit/2011852) fixed; background-size: 100%; background-repeat: no-repeat; font-family: "Helvetica Neue",Helvetica,Verdana,Arial,sans-serif; font-size: 12px; min-height: 101%; } #topics .postTitle { border: 0px; font-size: 200%; font-weight: bold; float: left; line-height: 1.5; width: 100%; padding-left: 5px; } div.commentform p{ margin-bottom:10px; } .comment_btn { padding: 5px 10px; height: 35px; width: 90px; border: 0 none; border-radius: 5px; background: #ddd; color: #999; cursor:pointer; font-family: "Lato", Helvetica Neue, Helvetica, Microsoft Yahei, 宋体, Arial, sans-serif; text-shadow: 0 0 1px #fff; display: inline !important; } .comment_btn:hover{ padding: 5px 10px; height: 35px; width: 90px; border: 0 none; border-radius: 5px; background: #258fb8; color: white; cursor:pointer; font-family: "Lato", Helvetica Neue, Helvetica, Microsoft Yahei, 宋体, Arial, sans-serif; text-shadow: 0 0 1px #fff; display: inline !important; } #commentform_title { background-image:none; background-repeat:no-repeat; margin-bottom:10px; padding:0; font-size:24px; } #commentbox_opt,#commentbox_opt + p { text-align:center; } .commentbox_title { width: 100%; } #tbCommentBody { font-family:'Microsoft Yahei', Microsoft Yahei, 宋体, sans-serif; margin-top:10px; max-width:100%; min-width:100%; background:white; color:#333; border:2px solid #fff; box-shadow:inset 0 0 8px #aaa; // padding:10px; height:250px; font-size:14px; min-height:120px; } .feedbackItem { font-size:14px; line-height:24px; margin:10px 0; padding:20px; background:#F2F2F2; box-shadow:0 0 5px #aaa; } .feedbackListSubtitle { font-weight:normal; } #blog-comments-placeholder, #comment_form { padding: 20px; background: #fff; -webkit-box-shadow: 1px 2px 3px #ddd; box-shadow: 1px 2px 3px #ddd; margin-bottom: 50px; } .feedback_area_title { margin-bottom: 15px; font-size: 1.8em; } .feedbackItem { border-bottom: 1px solid #CCC; margin-bottom: 10px; padding: 5px; background: rgb(248, 248, 248); } .color_shine {background: rgb(226, 242, 255);} .feedbackItem:hover {-webkit-animation-name: color_shine;-webkit-animation-duration: 2s;-webkit-animation-iteration-count: infinite;} #comment_form .title { font-weight: normal; margin-bottom: 15px; }