python+selenium自動化--參數化(paramunittest)

unnittest的參數化模塊-paramunittest

paramunittest是unittest實現參數化的一個專門的模塊,能夠傳入多組參數,自動生成多個用例python

兩種用法

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) 

  方案一結果以下:spa

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
>>> 

  方案二結果以下:blog

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
>>> 

  案例:it

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)

  

一、參數能夠傳元組也能夠傳字典class

二、接受參數的時候,必需要定義setParameters這個方法,而且只能是這個名稱。括號後面的參數分別接受傳入的參數名稱。前面定義的是字典,那參數就跟前面字典的key保持一致test

三、參數的執行順序是0,1,再執行10,11,12import

四、除了傳字典參數,傳元組類型的也是能夠的密碼

@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")
)
相關文章
相關標籤/搜索