python + selenium Automation - parameterization (paramunittest)

unnittest parameterized modules -paramunittest

paramunittest unittest Parameterization is a dedicated module, multiple sets of parameters can be passed, by automatically generating a plurality of cases

Two uses

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) 

  A program was as follows:

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

  Scheme II as follows:

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

  Case:

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": "to true"},
    { "User": "admin11", "PSW": "1234", "Result": "to true"}, 
) 

class TestDemo (of unittest.TestCase): 
    DEF the setParameters (Self, User, PSW, Result): 
        '' ' Note here that the, user, psw, result and the three parameters previously defined correspondence dictionary '' ' 
        self.user = User 
        self.psw = PSW 
        self.result = Result 

    DEF TestCase (Self): 
        Print ( "start execution Example : -------------- ") 
        the time.sleep (0.5) 
        Print (" enter username:% S "self.user%) 
        Print (" enter password:% s "% self. PSW) 
        Print ( "expected results:% S"% self.result) 
        the time.sleep (0.5) 
        self.assertTrue (self.result == "to true")


if __name__ == "__main__":
    unittest.main(verbosity=2)

  

1, parameters can be passed tuple can pass dictionaries

2, when the accepted parameters must be defined setParameters this method, and only the name. Parentheses following parameters are to accept incoming parameter name. Is previously defined dictionary , that argument just in front of the dictionary key to maintain consistency

3, the order parameter is 0, then execute 10,11,12

4, except that the parameters pass dictionary, passed tuple type is also possible

@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")
)

  

 

Guess you like

Origin www.cnblogs.com/steven223-z/p/11569864.html