python+selenium自动化--参数化(paramunittest)
Posted 子不语332
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python+selenium自动化--参数化(paramunittest)相关的知识,希望对你有一定的参考价值。
unnittest的参数化模块-paramunittest
paramunittest是unittest实现参数化的一个专门的模块,可以传入多组参数,自动生成多个用例
两种用法
import unittest import paramunittest # 方案一 @paramunittest.parametrized( (‘1‘, ‘2‘), #(4, 3), (‘2‘, ‘3‘), ((‘4‘, ), {‘b‘: ‘5‘}), ((), {‘a‘: 5, ‘b‘: 6}), {‘a‘: 5, ‘b‘: 6}, ) class TestFoo(paramunittest.ParametrizedTestCase): def setParameters(self, a, b): self.a = a self.b = b def testLess(self): self.assertLess(self.a, self.b) # 方案二 @paramunittest.parametrized( (‘1‘, ‘2‘), #(4, 3), (‘2‘, ‘3‘), ((‘4‘, ), {‘b‘: ‘5‘}), ((), {‘a‘: 5, ‘b‘: 6}), {‘a‘: 5, ‘b‘: 6}, ) class TestBar(unittest.TestCase): def setParameters(self, a, b): self.a = a self.b = b def testLess(self): self.assertLess(self.a, self.b) print("%s<%s"%(self.a,self.b)) if __name__ == "__main__": unittest.main(verbosity=2)
方案一结果如下:
testLess[0](((‘1‘, ‘2‘), {})) (paramunittest.TestFoo_0) ... ok testLess[1](((‘2‘, ‘3‘), {})) (paramunittest.TestFoo_1) ... ok testLess[2](((‘4‘,), {‘b‘: ‘5‘})) (paramunittest.TestFoo_2) ... ok testLess[3](((), {‘a‘: 5, ‘b‘: 6})) (paramunittest.TestFoo_3) ... ok testLess[4](((), {‘a‘: 5, ‘b‘: 6})) (paramunittest.TestFoo_4) ... ok ---------------------------------------------------------------------- Ran 5 tests in 0.099s OK >>>
方案二结果如下:
testLess (paramunittest.TestBar_0) ... 1<2 ok testLess (paramunittest.TestBar_1) ... 2<3 ok testLess (paramunittest.TestBar_2) ... 4<5 ok testLess (paramunittest.TestBar_3) ... 5<6 ok testLess (paramunittest.TestBar_4) ... 5<6 ok ---------------------------------------------------------------------- Ran 5 tests in 0.313s OK >>>
案例:
import unittest import paramunittest import time @paramunittest.parametrized( {"user": "admin", "psw": "123", "result": "true"}, {"user": "admin1", "psw": "1234", "result": "true"}, {"user": "admin2", "psw": "1234", "result": "true"}, {"user": "admin3", "psw": "1234", "result": "true"}, {"user": "admin4", "psw": "1234", "result": "true"}, {"user": "admin5", "psw": "1234", "result": "true"}, {"user": "admin6", "psw": "1234", "result": "true"}, {"user": "admin7", "psw": "1234", "result": "true"}, {"user": "admin8", "psw": "1234", "result": "true"}, {"user": "admin9", "psw": "1234", "result": "true"}, {"user": "admin10", "psw": "1234", "result": "true"}, {"user": "admin11", "psw": "1234", "result": "true"}, ) class TestDemo(unittest.TestCase): def setParameters(self, user, psw, result): ‘‘‘这里注意了,user, psw, result三个参数和前面定义的字典一一对应‘‘‘ self.user = user self.psw = psw self.result = result def testcase(self): print("开始执行用例:--------------") time.sleep(0.5) print("输入用户名:%s" % self.user) print("输入密码:%s" % self.psw) print("期望结果:%s " % self.result) time.sleep(0.5) self.assertTrue(self.result == "true") if __name__ == "__main__": unittest.main(verbosity=2)
1、参数可以传元组也可以传字典
2、接受参数的时候,必须要定义setParameters这个方法,并且只能是这个名称。括号后面的参数分别接受传入的参数名称。前面定义的是字典,那参数就跟前面字典的key保持一致
3、参数的执行顺序是0,1,再执行10,11,12
4、除了传字典参数,传元组类型的也是可以的
@paramunittest.parametrized( ("admin", "123", "true"), ("admin1", "123", "true"), ("admin2", "123", "true"), ("admin3", "123", "true"), ("admin4", "123", "true"), ("admin5", "123", "true"), ("admin6", "123", "true"), ("admin7", "123", "true"), ("admin8", "123", "true"), ("admin9", "123", "true"), ("admin10", "123", "true"), ("admin11", "123", "true"), ("admin12", "123", "true") )
以上是关于python+selenium自动化--参数化(paramunittest)的主要内容,如果未能解决你的问题,请参考以下文章
selenium+python自动化97--unittest参数化(paramunittest)
selenium+python自动化97--unittest参数化(paramunittest)
Selenium2+python自动化20-Excle数据参数化
selenium+python自动化78-autoit参数化与批量上传