当前位置:网站首页>Unittest套件与运行器
Unittest套件与运行器
2022-07-27 05:14:00 【司小幽】
目录
1.Skip装饰器的使用
''' Skip装饰器的使用 skip表示无条件直接跳过 skipIf表示表达式成立,则跳过,不成立则继续执行 skipUnless与skipIf相反 expectedFailure 默认用例执行会失败,并将其忽略 Unittest下的杂技套装 '''
import unittest
class Demo(unittest.TestCase):
@unittest.skip('无条件跳过该条用例执行')
def test_01(self):
print(1)
@unittest.skipIf(1 == 2,'这是If的理由')
def test_02(self):
print(2)
# @unittest.skipUnless( 1== 2,'这是Unless的理由')
def test_03(self):
print(3 / 0)
# @unittest.expectedFailure
def test_04(self):
print(4)
self.assertEqual(1,2,msg = '断言失败')
# if __name__ == '__main__':
# unittest.main()
2.Suite测试套件,
''' Suite测试套件,专门用于管理所有的测试用例类。可以理解为是用例的List,物理形态下就是一个文件夹的概念 套件的使用,一定是重新建立一个py文件进行调用,如果在UnitTest类下通过main函数调用则不会生效。 unittest.TestSuite类来进行使用 suite的运行一定是基于运行器运行的 套件中运行的测试用例,是基于添加的顺序来进行执行的,与UnitTest的用例排序没有关系 HTMLTestRunner测试报告:本质意义而言,其实就是一个运行器。 环境部署:直接下载py文件,放到python安装路径下的lib文件夹中就可以了。不要通过pip安装 网上下载的默认是只支持2.7版本及以下的。如果要支持到3以上版本,需要修改源码。 修改py文件的源码: 第94行,将import StringIO修改成import io 第539行,将self.outputBuffer = StringIO.StringIO()修改成 self.outputBuffer = io.StringIO() 第642行,将if not rmap.has_key(cls):修改成if not cls in rmap: 第766行,将uo = o.decode('latin-1')修改成uo = e 第772行,将ue = e.decode('latin-1')修改成ue = e 第631行,将print >> sys.stderr, '\nTime Elapsed: %s' % (self.stopTime-self.startTime)修改成print(sys.stderr, '\nTime Elapsed: %s' % (self.stopTime-self.startTime)) UnitTest下所有的测试用例用test开头。所有的测试用例文件,也需要以test开头。 '''
import os
import unittest
# from HTMLTestRunner import HTMLTestRunner
from HTMLTestReportCN import HTMLTestRunner
# from class27.test_demo import UnitDemo, UnitDemo02
# from class28.test_demo import Demo
1.创建测试套件
suite = unittest.TestSuite()
2.在套件中添加测试用例
# 基于用例的名称来添加单个测试用例
# suite.addTest(UnitDemo('test_02'))
# suite.addTest(Demo('test_03'))
3.批量添加用例
# 基于用例名称进行添加,以list形式
# cases = [Demo('test_01'),UnitDemo('test_03')]
# suite.addTests(cases)
4.通过添加一个完整的class进入套件
# suite.addTests(unittest.TestLoader().loadTestsFromTestCase(Demo))
5.基于文件名称来进行添加
# 添加的是当前路径下的py文件,否则会报错
# cases = [unittest.TestLoader().loadTestsFromNames('unit_demo.UnitDemo')]
# suite.addTests(cases)
# names = ['unit_demo.UnitDemo','unit_demo.UnitDemo02'] # py文件的名称+class的名称
# suite.addTests(unittest.TestLoader().loadTestsFromNames(names))
6.批量添加测试用例
# 在持续集成中比较好用,批量化管理测试用例也很好用
# 定义用例的获取路径
path = '../class28'
discover = unittest.defaultTestLoader.discover(start_dir=path,pattern='test*.py')
7.通过运行器来运行套件
# run = unittest.TextTestRunner(verbosity=2) #verbosity 参数表示显示的控制台信息详细度,分为0、1、2三级。
# run.run(suite)
# run.run(discover)
3.HTMLTestRunner生成测试报告
1.配置测试报告的相关内容
report_dir = './report/' #保存路径
report_title = '虚竹的测试报告' # 报告名称
report_description = '这是测试报告中的描述部分' # 测试报告描述
report_file = report_dir + 'report.html' # 测试报告的文件:建议测试报告的名称以时间戳的形式进行命名
# 测试执行者
report_tester = '司小幽'
2.保存报告的路径是否存在,不存在则创建一个
if not os.path.exists(report_dir):
os.mkdir(report_dir)
3.生成测试报告,其实就是写入一个文件内容
with open(report_file,'wb') as file:
runner = HTMLTestRunner(stream = file,title = report_title,description=report_description,verbosity=2,tester = report_tester)
runner.run(discover)
边栏推荐
- Gbase 8C - SQL reference 6 SQL syntax (7)
- MySQL索引失效与解决方法实践
- 新冠时空分析——Global evidence of expressed sentiment alterations during the COVID-19 pandemic
- Digital image processing Chapter 2 fundamentals of digital image
- 7.合并与分割
- 数字图像处理 第二章 数字图像基础
- Only one looper may be created per thread
- 7. Merger and division
- 向量和矩阵的范数
- 【好文种草】根域名的知识 - 阮一峰的网络日志
猜你喜欢

10.梯度、激活函数和loss

11. Gradient derivation of perceptron

15.GPU加速、minist测试实战和visdom可视化

11.感知机的梯度推导

贪心高性能神经网络与AI芯片应用研修

Digital image processing Chapter 2 fundamentals of digital image

16. Over fitting and under fitting

数字图像处理 第二章 数字图像基础

【MVC架构】MVC模型

Emoji表情符号用于文本情感分析-Improving sentiment analysis accuracy with emoji embedding
随机推荐
MySQL索引优化相关原理
Day10. Work organization and mental health problems in PhD students
17. Attenuation of momentum and learning rate
向量和矩阵的范数
10. Gradient, activation function and loss
Rk3399 GPIO port how to find which GPIO port it is
16. Over fitting and under fitting
3. Classification problems - initial experience of handwritten digit recognition
常用adb命令汇总 性能优化
Emoji Emoji for text emotion analysis -improving sentimental analysis accuracy with Emoji embedding
18. Convolutional neural network
GBASE 8C——SQL参考6 sql语法(11)
rk3399 gpio口 如何查找是哪个gpio口
DDD领域驱动设计笔记
MySQL索引失效与解决方法实践
Gbase 8C - SQL reference 5 full text search
MySQL limit分页查询优化实践
pytorch的多GPU训练的两种方式
2.简单回归问题
贪心高性能神经网络与AI芯片应用研修