Python基于paramunittest模块实现excl参数化

转载:https://www.jb51.net/article/185445.htm

1.paramunittest是unittest实现参数化的一个专门的模块,可以传入多组参数,自动生成多个用例
2.导入paramunittest:import paramunittest
3.paramunittest支持字典、元组、列表作为入参
列表格式:[["vaule"],["vaule2"]],setParameters赋值的个数与列值一一对应,例如此列表格式列值只有一个,赋值时只能自定义一个值
元组格式:(("pwd","vaule"),("pwd","vaule2")),元组列值必须保证两个以上,如果只有一个值,setParameters时报错,setParameters赋值的个数与列值一一对应,例如此元组格式列值有2个,赋值时只能自定义2个值
字典格式:[{"key": "vaule" },{"key": "vaule2"}],字典保证key值一致,如相同个数,相同位置,例如此字典格式列值只有一个key,setParameters赋值时只能定义一个key值
4.赋予列表/字典/元组值:@paramunittest.parametrized(*list/tuple)
5.列表元组赋予列值:def setParameters(self, t1, t2, tN):
'''注意t1, t2, t3名称自定义,从左到右按顺序一一对应值'''
self.t1 = t1
self.t2 = t2
self.tN = tN
6.列表元组取列值def testcase(self): self.t1
7.字典单赋予列值:def setParameters(self, key, keyN):
'''注意key, keyN是与字典key值必须一一对应'''
self.key = key
self.keyN = keyN
8.列表元组取取列def testcase(self): self.key1,字典通过key获取vaulue值
特别注意:如列值下标大于10时,执行顺序是先执行0,1再执行10,11,再执行2,3,.....9

二、源码举例(列表)

说明:一般读取exel会生成一个列表,如果使用exel作为参数化,可以参考此

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 #!/usr/bin/python3 # encoding:utf-8 import unittest import paramunittest import time   list =[   ['test', '123456', 'true'],   ['test1', '1234564', 'true'] ]   @paramunittest.parametrized(*list) class TestDemo(unittest.TestCase):   def setParameters(self, user, pwd, result):     '''注意user, pwd, result是从左到右按顺序一一对应'''     self.user = user     self.pwd = pwd     self.result = result     def testcase(self):     print("开始执行用例:--------------")     time.sleep(1)     print("输入用户名:%s" % self.user)     print("输入密码:%s" % self.pwd)     print("期望结果:%s " % self.result)     time.sleep(1)     self.assertTrue(self.result == "true") if __name__ == "__main__":   #verbosity=2 显示用例执行详情   unittest.main(verbosity=2)

运行结果

testcase (paramunittest.TestDemo_0) ... 开始执行用例:--------------
输入用户名:test
输入密码:123456
期望结果:true
ok
开始执行用例:--------------
testcase (paramunittest.TestDemo_1) ... 输入用户名:test1
输入密码:1234564
期望结果:true
ok

----------------------------------------------------------------------
Ran 2 tests in 4.003s

OK

三、源码举例(字典)

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 #!/usr/bin/python3 # encoding:utf-8 import unittest import paramunittest import time dict =[   {"user": "test", "pwd": "123456", "result": "true"},   {"user": "test1", "pwd": "1234564", "result": "true"},   {"user": "test2", "pwd": "1234564", "result": "true"},   {"user": "test3", "pwd": "1234564", "result": "true"},   {"user": "test4", "pwd": "1234564", "result": "true"},   {"user": "test5", "pwd": "1234564", "result": "true"},   {"user": "test6", "pwd": "1234564", "result": "true"},   {"user": "test7", "pwd": "1234564", "result": "true"},   {"user": "test8", "pwd": "1234564", "result": "true"},   {"user": "test9", "pwd": "1234564", "result": "true"},   {"user": "test10", "pwd": "1234564", "result": "true"},   {"user": "test11", "pwd": "1234564", "result": "true"} ] @paramunittest.parametrized(*dict) class TestDemo(unittest.TestCase):   def setParameters(self, user, pwd, result):     '''user, pwd, result三个参数字典的key一一对应'''     self.user = user     self.pwd = pwd     self.result = result     def testcase(self):     print("开始执行用例:--------------")     time.sleep(1)     print("输入用户名:%s" % self.user)     print("输入密码:%s" % self.pwd)     print("期望结果:%s " % self.result)     time.sleep(1)     self.assertTrue(self.result == "true") if __name__ == "__main__":   #verbosity=2 显示用例执行详情   unittest.main(verbosity=2)
上一篇:qt控件----button


下一篇:238. 除自身以外数组的乘积