bcbeer 2020-04-23
Windows环境安装:paramunittest cmd输入命令:pip install paramunittest
总结说明:
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作为参数化,可以参考此
#!/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
#!/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)
运行结果
#说明:框架问题:当字典大于10时,执行顺序是先执行0,1再执行10,11,再执行#2,3,.....9 testcase (paramunittest.TestDemo_0) ... 开始执行用例:-------------- 输入用户名:test 输入密码:123456 期望结果:true ok 开始执行用例:-------------- testcase (paramunittest.TestDemo_1) ... 输入用户名:test1 输入密码:1234564 期望结果:true ok 开始执行用例:-------------- testcase (paramunittest.TestDemo_10) ... 输入用户名:test10 输入密码:1234564 期望结果:true ok 开始执行用例:-------------- testcase (paramunittest.TestDemo_11) ... 输入用户名:test11 输入密码:1234564 期望结果:true ok 开始执行用例:-------------- testcase (paramunittest.TestDemo_2) ... 输入用户名:test2 输入密码:1234564 期望结果:true ok 开始执行用例:-------------- testcase (paramunittest.TestDemo_3) ... 输入用户名:test3 输入密码:1234564 期望结果:true ok开始执行用例:-------------- testcase (paramunittest.TestDemo_4) ... 输入用户名:test4 输入密码:1234564 期望结果:true ok 开始执行用例:-------------- testcase (paramunittest.TestDemo_5) ... 输入用户名:test5 输入密码:1234564 期望结果:true ok 开始执行用例:-------------- testcase (paramunittest.TestDemo_6) ... 输入用户名:test6 输入密码:1234564 期望结果:true ok 开始执行用例:-------------- testcase (paramunittest.TestDemo_7) ... 输入用户名:test7 输入密码:1234564 期望结果:true ok 开始执行用例:-------------- testcase (paramunittest.TestDemo_8) ... 输入用户名:test8 输入密码:1234564 期望结果:true ok开始执行用例:-------------- testcase (paramunittest.TestDemo_9) ... 输入用户名:test9 输入密码:1234564 期望结果:true ok ---------------------------------------------------------------------- Ran 12 tests in 24.018s OK
#!/usr/bin/python3 # encoding:utf-8 import unittest import paramunittest import time tuple=( ("test","123456","true"), ("test1","1234564","true") ) #另一种赋值方式:直接赋值 #@paramunittest.parametrized( # ("test","123456","true"), # ("test1","1234564","true") #) @paramunittest.parametrized(*tuple) 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.002s OK
问题描述:一个元组只有一个列值,如("test"),("test1"),setParameters之后运行报错如下: testcase (paramunittest.TestDemo_0) ... ERROR testcase (paramunittest.TestDemo_1) ... ERROR ====================================================================== ERROR: testcase (paramunittest.TestDemo_0) ---------------------------------------------------------------------- Traceback (most recent call last): File "D:\pythonaz\lib\site-packages\paramunittest.py", line 89, in setUp self.setParameters(*parameters[0], **parameters[1]) TypeError: setParameters() takes 2 positional arguments but 5 were given 解决方法:保证一个元组列值两个以上,如("test","123456"),("test1","123") 注:只有元组有这个问题,列表或字典无此问题