Python unittest数据处理ddt

yizhihongxing

Python unittest数据处理ddt

1.装饰器

#装饰器的官方定义:
装饰器本质上是一个Python函数(其实就是闭包),它可以让其他函数在不需要做任何代码变动的前提下增加额外功能,装饰器的返回值也是一个函数对象。装饰器用于有以下场景,比如:插入日志、性能测试、事务处理、缓存、权限校验等场景。

2.ddt

import unittest
#引入ddt库
from ddt import ddt,data,unpack
test_data = [{"name":"张三"},2]

@ddt#装饰测试类
class DemoCase(unittest.TestCase):
    @data(test_data)#装饰函数
    def test_jia(self,item):
        print(item)
    def test_jian(self):
        pass

if __name__ == '__main__':
    unittest.main()
运行结果:
plugins: html-3.2.0, metadata-2.0.4collected 2 items demo.py .[{'name': '张三'}, 2] . [100%] ============================== 2 passed in 0.04s ============================== Process finished with exit code 0

3.ddt脱外套

import unittest
#引入ddt库
from ddt import ddt,data,unpack
test_data = [{"name":"张三"},2]

@ddt#装饰测试类
class DemoCase(unittest.TestCase):
    @data(*test_data)#装饰函数,*test_data脱外套,根据脱外套后的数据执行次数
    def test_jia(self,item):
        print(item)

if __name__ == '__main__':
    unittest.main()
运行结果:
plugins: html-3.2.0, metadata-2.0.4collected 2 items demo.py .{'name': '张三'} .2 [100%] ============================== 2 passed in 0.04s ============================== Process finished with exit code 0

4.ddt中的@unpack

import unittest
#引入ddt库
from ddt import ddt,data,unpack
test_data = [[2,6],[9,1000]]

@ddt#装饰测试类
class DemoCase(unittest.TestCase):

    @data(*test_data)#装饰函数,*test_data脱外套,根据脱外套后的数据执行次数
    @unpack
    def test_jia(self,a,b):
        print("a: ",a)
        print("b: ",b)

if __name__ == '__main__':
    unittest.main()
运行结果:
plugins: html-3.2.0, metadata-2.0.4collected 2 items
demo.py 
.a:
2 b: 6 .a: 9 b: 1000 [100%] ============================== 2 passed in 0.04s ==============================

 

 

5.ddt+openpyxl参数化实现

import unittest
#引入ddt库
from ddt import ddt,data,unpack
from test01.doexcel import DoExcel
Python unittest数据处理ddt

#引入仓库
from openpyxl import load_workbook

class DoExcel():
    def __init__(self,file,sheet):
        self.file=file
        self.sheet=sheet

    def return_excel_value(self):
        wb = load_workbook(self.file)#打开excel
        sheet_content = wb[self.sheet]#定位sheet工作博
        data_list = []#列表用于存储测试数据
        for n in range(2,sheet_content.max_row+1):#行,第一行是标题,所以从第二行开始
            data_dict = {}#字典用于存储每组测试数据
            for m in range(2,sheet_content.max_column+1):
                data_dict["method"]=sheet_content.cell(n,2).value
                data_dict["url"] = sheet_content.cell(n, 3).value
                data_dict["data"] = eval(sheet_content.cell(n, 4).value)#eval()将数据类型还原
                data_dict["expect"] = sheet_content.cell(n, 5).value
            data_list.append(data_dict)#将字典存储到list
        return data_list

DoExcel类代码

test_data = DoExcel("C:\Users\Administrator\Desktop\testdemo.xlsx","s1").return_excel_value()#[{},{},{}]

@ddt#装饰测试类
class DemoCase(unittest.TestCase):

    @data(*test_data)#装饰函数,*test_data脱外套,根据脱外套后的数据执行次数
    def test_jia(self,item):
        print(item["method"],item["url"],item["data"],item["expect"])

if __name__ == '__main__':
    unittest.main()
执行结果:
plugins: html-3.2.0, metadata-2.0.4collected 2 items
demo.py .post http://www.qabujiaban.com/user/login {'username': 'uuuu222都44', 'password': 'WJHasb124*1'} 0000
.get http://www.qabujiaban.com/user/login {'username': 'uuuu222都44', 'password': 'WJHasb124*1'} 0000
                                                               [100%]
============================== 2 passed in 0.31s ==============================
Process finished with exit code 0

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Python unittest数据处理ddt - Python技术站

(0)
上一篇 2023年4月2日 下午4:11
下一篇 2023年4月2日 下午4:11

相关文章

  • Python异常处理

    1.异常处理 异常:在运行代码过程中遇到的任何错误,大有error字样的都为异常 异常处理:对代码中所有可能会出现的异常进行处理 疑问:我们为什么要处理异常? 2.异常代码 import os #创建一个已存在的文件夹 os.mkdir(“Eclipse”) #异常:FileExistsError: [WinError 183] 当文件已存在时,无法创建该文…

    2023年4月2日
    00
  • python基础语法/数据类型/运算符

    1.标识符 # -*- coding:utf-8 -*- # @Time :2021/1/16 10:28 # @Author :QAbujiaban # @Email :wbxztoo@163.com # @File :basic.py 1 # 单行注释 这是单行注释 2 ”’多行注释”’ 这是多行注释 3 “””多行注释””” 这是多行注释 2.标识…

    2023年4月2日
    00
  • Python操作Excel(openpyxl)

    1.引入openpyxl库 安装openpyxl库:pip install openpyxl 引入openpyxl库:from openpyxl import load_worbook 2.代码实现 from openpyxl import load_workbook #打开Excel wb = load_workbook(“C:\Users\Adminis…

    2023年4月2日
    00
  • Python控制语句/循环语句

    1.控制语句 1.1.判断语句 if…elif…else #if 语句(比较/逻辑/成员均可) #字符串/列表/元组/字典 为空返回False,非空返回True #条件成立返回True,不成立返回False ar = “” list = [] tuple = () dic = {} if ar:#条件成立返回True继续执行,不成立返回False不执…

    2023年4月2日
    00
  • Python怎么引入不同的库?

    怎么引入不同的库? 1.在线安装库 1.1.pip install 模块名 1.2.国内源: 清华:https://pypi.tuna.tsinghua.edu.cn/simple 阿里云:http://mirrors.aliyun.com/pypi/simple/ 中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple…

    2023年4月2日
    00
  • Python unittest+ddt+openpyxl+configparser

    1.技术介绍 框架:unittest 请求处理:requests excel数据处理:openpyxl 参数化:ddt 配置解析器:configparser 报告模板:HTMLTestRunnerNew.py(下载地址:https://pan.baidu.com/s/1w9AZU9AkIpxCYuzTto0EQA?pwd=1234) testdemo.xls…

    2023年4月2日
    00
  • Python unittest+ddt+openpyxl

    1.技术介绍 框架:unittest 请求处理:requests excel数据处理:openpyxl 参数化:ddt 报告模板:HTMLTestRunnerNew.py(下载地址:https://pan.baidu.com/s/1w9AZU9AkIpxCYuzTto0EQA?pwd=1234) testdemo.xlsx:测试数据,注意:implement…

    2023年4月2日
    00
  • Python之open()/OS

    1.File对象数据读写与操作 #def open(file, mode=’r’, buffering=None, encoding=None, errors=None, newline=None, closefd=True): # known special case of open #file: 操作的文件 #mode:打开这个文件的模式 ‘r’ ope…

    2023年4月2日
    00
合作推广
合作推广
分享本页
返回顶部