Selenium+python automation 97--unittest parameterization (paramunittest)

foreword

paramunittest is a special module for unittest to implement parameterization. It can pass in multiple sets of parameters and automatically
generate multiple test cases. When we talked about data-driven, ddt can be used to solve the input of multiple sets of data and automatically generate multiple test cases. This article continues to introduce another parameterized framework paramunittest, which can also achieve the same effect.

Environmental preparation

1. First pip download and install paramunittest

pip install paramunittest

official documentation

1. Official document address: https://pypi.python.org/pypi/ParamUnittest/

2. github source code download address: https://github.com/rik0/ParamUnittest

3. Two use cases are given in the official document. Both of the following two methods are possible. The editor prefers the second one, which is easier to understand. Note here that when running in pycharm, the mouse must be placed at the end. Execute below if.

4. Add this parameter verbosity=2 to display the details of the use case execution

verbosity=2

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)
        
if __name__ == "__main__":
    unittest.main(verbosity=2)

Practical case

1. As can be seen from the case given by the official document above, the parameters can be passed through tuples or dictionaries. The dictionary parameters are passed first, similar to the following set of parameters:

{"user": "admin", "psw": "123", "result": "true"}

2. Note that when accepting parameters here, the method setParameters must be defined, and it can only be this name. The parameters after the parentheses respectively accept the parameter name passed in. The previous definition is a dictionary, and the parameters are consistent with the key of the previous dictionary.

import unittest
import paramunittest
import time
# python3.6
# 作者:上海-悠悠

@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.user = psw
        self.result = result

    def testcase(self):
        print("开始执行用例:--------------")
        time.sleep(0.5)
        print("输入用户名:%s" % self.user)
        print("输入密码:%s" % self.user)
        print("期望结果:%s " % self.result)
        time.sleep(0.5)
        self.assertTrue(self.result == "true")


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

3. Execution result:

开始执行用例:--------------
testcase (paramunittest.TestDemo_0) ... 输入用户名:123
输入密码:123
期望结果:true 
ok
testcase (paramunittest.TestDemo_1) ... 开始执行用例:--------------
输入用户名:1234
输入密码:1234
期望结果:true 
ok
testcase (paramunittest.TestDemo_10) ... 开始执行用例:--------------
输入用户名:1234
输入密码:1234
期望结果:true 
开始执行用例:--------------
ok
testcase (paramunittest.TestDemo_11) ... 输入用户名:1234
输入密码:1234
期望结果:true 
开始执行用例:--------------
ok
testcase (paramunittest.TestDemo_2) ... 输入用户名:1234
输入密码:1234
期望结果:true 
ok
开始执行用例:--------------
testcase (paramunittest.TestDemo_3) ... 输入用户名:1234
输入密码:1234
期望结果:true 
ok
testcase (paramunittest.TestDemo_4) ... 开始执行用例:--------------
输入用户名:1234
输入密码:1234
期望结果:true 
ok
testcase (paramunittest.TestDemo_5) ... 开始执行用例:--------------
输入用户名:1234
输入密码:1234
期望结果:true 
开始执行用例:--------------
ok
testcase (paramunittest.TestDemo_6) ... 输入用户名:1234
输入密码:1234
期望结果:true 
开始执行用例:--------------
ok
testcase (paramunittest.TestDemo_7) ... 输入用户名:1234
输入密码:1234
期望结果:true 
ok
开始执行用例:--------------
testcase (paramunittest.TestDemo_8) ... 输入用户名:1234
输入密码:1234
期望结果:true 
ok
testcase (paramunittest.TestDemo_9) ... 开始执行用例:--------------
输入用户名:1234
输入密码:1234
期望结果:true 
ok

----------------------------------------------------------------------
Ran 12 tests in 12.001s

4. Note that the execution order here is to execute 0, 1 first, and then execute 10, 11, and 12 in order. Don't ask me why, and don't ask me to solve it. The design is like this. The previous ddt framework also had the same problem.

5. In addition to passing dictionary parameters, it is also possible to pass tuple types

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

generate html report

1. The html report generated by unittest can refer to the http://www.cnblogs.com/yoyoketang/p/6680503.html introduced earlier . It will not be repeated here. The effect diagram of the realization is as follows:

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324443097&siteId=291194637