码迷,mamicode.com
首页 > 编程语言 > 详细

python+selenium自动化--参数化(paramunittest)

时间:2019-09-22 23:15:58      阅读:451      评论:0      收藏:0      [点我收藏+]

标签:参数   param   模块   --   输入   自动   testcase   类型   ==   

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)

标签:参数   param   模块   --   输入   自动   testcase   类型   ==   

原文地址:https://www.cnblogs.com/steven223-z/p/11569864.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!