当前位置:网站首页>Unittest单元测试(六)
Unittest单元测试(六)
2022-04-23 00:11:00 【努力卷】
一、批量生成自动化的测试报告
1.txt文本格式
unittest.TextTestRunner(verbosity=2).run(suite)
2.html格式
第一步下载HTMLTestRunner.py文件并且修改。下载教程传送门
然后放到C:\Program Files\Python37\libs目录下
if __name__ == '__main__':
# 读取用例
suite = unittest.defaultTestLoader.discover('./test', pattern='test_*.py')
# 生成html空的报告文件
now = time.strftime("%Y%m%d%H%M%S",time.localtime())
file = open("./report/report_"+str(now)+".html","wb")
# 初始化一个HTMLTestRunner对象
runner = HTMLTestRunner(stream=file,title="测试报告",description="报告详情如下:")
runner.run(suite)
二、数据驱动简介
1.为什么需要数据驱动呢?
正例,反例
登录:同一个业务逻辑,代码逻辑是不变的,数据是变化的。业务逻辑和数据分离。
三、自动化主流驱动模式介绍
1.数据驱动
数据驱动把数据保存在excel,csv,yaml,数据库。通过改变数据驱动我们的业务逻辑执行。并且得到不同的结果
2.关键字驱动
关键字驱动其实是从面向对象的思想出发,它是把一些业务逻辑代码封装成一个函数、方法,作为一个关键字,然后调用不同的函数组成不同的复杂的业务逻辑。
3.数据驱动+关键字驱动
四、unittest的ddt数据驱动
1.什么是ddt?
data driver test 它可以完美地应用于unittest框架实现数据驱动
2.ddt详解
它可以通过装饰器的方式来调用
装饰器:完成特定功能的一个函数。
在Python中有类装饰器和函数装饰器
举例:
@ddt 类装饰器 作用:申明当前类使用DDT框架
@data 函数装饰器 作用:用于给测试用例传递数据
@unpack 函数装饰器 作用:将数据解包,一般用于元组和列表
@file_data函数装饰器 作用:可以读取json或者yaml文件
3.@ddt 、@data、@unpack 用法
import unittest
from ddt import ddt, data, unpack
@ddt
class TestDdt(unittest.TestCase):
@data(("1","2"), ("3","4")) # 函数执行次数有参数个数决定
@unpack
def test_01(self, args1, args2):
print(args1, args2)
if __name__ == '__main__':
unittest.main()
总结:
1.ddt数据驱动中,测试用例的执行次数是由@data()传参个数决定。
2.如果传的是多个元组(或列表),那么可以使用@unpack解包元组和列表,元组或者列表中有多少个值就需要多少个参数来接受值。
3.解包字典时,接受的参数名要与键名相同
@data({"name":"test","age":"12"},{"name":"detect","age":"15"}) # 函数执行次数有参数个数决定
@unpack
def test_01(self, name, age):
print(name, age)
版权声明
本文为[努力卷]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq786558544/article/details/124331117
边栏推荐
- 2022年了,微信商城小程序还值得做吗?
- 力扣习题集1--两数之和
- (转)Aspose.words编程指南之Working with Document
- 教你两分钟做出一个精美好用的404页面
- Implementation method of interface between bar code WMS system and ERP
- Notes on "Introduction to dynamic programming" (under update)
- (transfer) Aspose Introduction to DOM tree structure, node class inheritance relationship and description of words Programming Guide
- (转)Aspose.words编程指南之DOM树再识,各层结构之间的关系
- (转)SSH 登录配置与文件传输
- Ansible Yum warehouse
猜你喜欢

BGP基本配置

What are the interfaces of the switch? The article takes you to remember its name and function

Compared with the traditional anchor based method, fcos is unique

算法--两数相加 II(Kotlin)

大型企业SAP集成WMS系统方案流程

倍福两台CX控制器之间添加路由

Skills of saving images with different depths in opencv

WMS仓储管理系统解决方案

归纳AI数据增强的方法

Invite you to participate in the ume plug-in development competition
随机推荐
Compared with the traditional anchor based method, fcos is unique
API interface knowledge summary
网站被DDOS攻击了怎么办?
两种方法实现mongodb自增id,推荐第二种
BGP basic configuration
Change the pycharm interface to Chinese and switch between Chinese and English
倍福TwinCAT两台CX控制器之间做ADS通信
What are the target keywords?
Programming 2022-02 KTV
Ansible Yum warehouse
WMS仓储管理系统解决方案
Human and God (l1-073)
Reinstall windows10
sideEffects导致的全局样式消失
WMS系统与ERP仓储管理的差异
Detailed explanation of kubernetes (VIII) -- resource allocation list of kubernetes
(转)Aspose.words编程指南之Working with Document
B端/C端中,产品or运营哪个更重要?
Teach you two minutes to make a beautiful and easy-to-use 404 page
Nodejs学习笔记