Python3-接口自动化-13- unittest参数化(paramunittest)

1. 先pip 下载安装 paramunittest

pip install paramunittes

2. 官方文档

https://pypi.org/project/ParamUnittest/

3.实际案例

  3.1 从上面官方文档给的案例可以看出,参数可以传元组也可以传字典,先传字典参数

这里接受参数的时候,必须要定义setParameters这个方法,并且只能是这个名称。括号后面的参数分别接受传入的参数名称。前面定义的是字典,那参数就跟前面字典的key保持一致

#!/usr/bin/python3.8
# -*- coding: utf-8 -*-
# @Time    : 2021/3/4 19:39
# @Author  : csjin
# @File    : Test06.py

import unittest
import paramunittest
import time

@paramunittest.parametrized(
    {"user_name": "admin", "password": "123", "result": "true"},
    {"user_name": "admin1", "password": "1234", "result": "true"},
    {"user_name": "admin2", "password": "1234", "result": "true"},
    {"user_name": "admin3", "password": "1234", "result": "true"},
    {"user_name": "admin4", "password": "1234", "result": "true"},
    {"user_name": "admin5", "password": "1234", "result": "true"},
    {"user_name": "admin6", "password": "1234", "result": "true"},
    {"user_name": "admin7", "password": "1234", "result": "true"},
    {"user_name": "admin8", "password": "1234", "result": "true"},
    {"user_name": "admin9", "password": "1234", "result": "true"},
    {"user_name": "admin10", "password": "1234", "result": "true"},
    {"user_name": "admin11", "password": "1234", "result": "true"},
)
class TestDemo(unittest.TestCase):
    def setParameters(self, user_name, password, result):
        '''这里注意了,user_name, password, result三个参数和前面定义的字典一一对应'''
        self.user = user_name
        self.user = password
        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)

输出结果

D:\ceshisoftware\Python38\python3.exe D:/Ddworkspace/workspace/DdScript/dmall-small-program/autoPyWxInterface/test/Test06.py
开始执行用例:--------------
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.015s

OK

Process finished with exit code 0

  3.2 传入列表,注意入参是一个对象时,需要加*        @paramunittest.parametrized(*l)

l = [["admin", "123", "true"],
    ["admin", "123",  "true"],
    ["admin", "123",  "true"],
    ["admin", "123",  "true"],
    ["admin", "123",  "true"],
    ["admin", "123",  "true"],
    ["admin", "123",  "true"],
    ["admin", "123",  "true"],
    ["admin", "123",  "true"]]
@paramunittest.parametrized(*l)
class TestDemo(unittest.TestCase):
    def setParameters(self, user_name, password, result):
        '''这里注意了,user, psw, result三个参数和前面定义的字典一一对应'''
        self.user = user_name
        self.user = password
        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)

输出结果

D:\ceshisoftware\Python38\python3.exe D:/Ddworkspace/workspace/DdScript/dmall-small-program/autoPyWxInterface/test/Test06.py
testcase (paramunittest.TestDemo_0) ... 开始执行用例:--------------
输入用户名:123
输入密码:123
期望结果:true 
开始执行用例:--------------
ok
testcase (paramunittest.TestDemo_1) ... 输入用户名:123
输入密码:123
期望结果:true 
ok
testcase (paramunittest.TestDemo_2) ... 开始执行用例:--------------
输入用户名:123
输入密码:123
期望结果:true 
开始执行用例:--------------
ok
testcase (paramunittest.TestDemo_3) ... 输入用户名:123
输入密码:123
期望结果:true 
开始执行用例:--------------
ok
testcase (paramunittest.TestDemo_4) ... 输入用户名:123
输入密码:123
期望结果:true 
ok
testcase (paramunittest.TestDemo_5) ... 开始执行用例:--------------
输入用户名:123
输入密码:123
期望结果:true 
开始执行用例:--------------
ok
testcase (paramunittest.TestDemo_6) ... 输入用户名:123
输入密码:123
期望结果:true 
开始执行用例:--------------
ok
testcase (paramunittest.TestDemo_7) ... 输入用户名:123
输入密码:123
期望结果:true 
开始执行用例:--------------
ok
testcase (paramunittest.TestDemo_8) ... 输入用户名:123
输入密码:123
期望结果:true 
ok

----------------------------------------------------------------------
Ran 9 tests in 9.011s

OK

Process finished with exit code 0

  3.2 除了字典和列表外,元组也是可以的

posted @ 2021-03-05 17:23  旅行没有终点  阅读(281)  评论(0编辑  收藏  举报