Python中Unittest框架的具体使用
作者:Tester_hope 时间:2023-02-20 11:41:09
Unittest
1.环境
Unittest为Python内嵌的测试框架,不需要特殊配置,仅需要在File---settings---Tools----Python Intergrated Tools下配置框架为Unittest
2.编写规范
需要导入 import unittest
测试类必须继承unittest.TestCase
测试方法以 test_开头
模块和类名没有要求
3.Unittest介绍
import unittest
def add(x, y):
return x + y
def sub(x,y):
return x - y
class UnittestDemo(unittest.TestCase):
def setUp(self):
print("I an setup")
def test_add(self):
print("my first unit test demo----add")
self.assertEqual(add(10,1),11,"assert equal")
def test_sub(self):
print("my first unit test demo----sub")
self.assertEqual(sub(10, 1), 9, "assert equal2")
@unittest.skip("i don't want to run this case.")
def test_sub1(self):
print("my first unit test demo----sub1")
self.assertEqual(sub(10, 1), 9, "assert equal3")
def tearDown(self):
print("I an tearDown")
setUp及TearDown
方法级别的,类里面的每个方法调用一次
def setUp(self):
print("I an setup")
setUpClass及tearDownClass
类级别的,类里面的所有方法仅执行一次
@classmethod
def setUpClass(cls):
print("I an setUpClass")
setUpModule,tearDownModule
模块级别的,执行模块里面类中的所有方法仅执行一次
4.Unittest使用
import unittest
from unittest_task import UnittestDemo
from HTMLTestRunner import HTMLTestRunner
suite=unittest.TestSuite()#创建用例集合
tests = [UnittestDemo("test_add"), UnittestDemo("test_sub"),UnittestDemo("test_sub1")]
suite.addTests(tests)
with open('HTMLReport.html', 'wb') as fi:
runner = HTMLTestRunner(stream=fi,
title = 'MathFunc Test Report',
description='generated by HTMLTestRunner.',
verbosity=2
)
runner.run(suite)
TestCase
多个测试用例集合在一起,就是TestSuite
TestSuite
多个测试用例集合在一起,就是TestSuite
TestLoader
用来加载TestCase到TestSuite中的
TestRunner
执行测试用例的,测试的结果会保存到TestResult实例中,包括运行了多少测试用例,成功了多少,失败了多少等信息
skip装饰器
跳过某个用例不执行
@unittest.skip("i don't want to run this case.")
无条件跳过
unittest.skip(reason)
当condition为True时跳过
unittest.skipIf(condition, reason)
当condition为False时跳过
unittest.skipUnless(condition, reason)
self.skipTest('do not run this.')
5.Unittest生成报告
5.1 Unittest生成txt格式报告
if __name__ == '__main__':
suite = unittest.TestSuite()
tests = [TestMathFunc("test_add"), TestMathFunc("test_minus"), TestMathFunc("test_divide")]
suite.addTests(tests)
with open('UnittestTextReport.txt', 'a') as f:
runner = unittest.TextTestRunner(stream=f, verbosity=2)
runner.run(suite)
运行该文件,就会发现目录下生成了'UnittestTextReport.txt,所有的执行报告均输出到了此文件中。
verbosity参数可以控制执行结果的输出,0 是简单报告、1 是一般报告、2 是详细报告
5.2 Unittest生成html格式报告
with open('HTMLReport.html', 'wb') as fi:
runner = HTMLTestRunner(stream=fi,
title = 'MathFunc Test Report',
description='generated by HTMLTestRunner.',
verbosity=2
)
runner.run(suite)
输出测试报告为HTML格式,unittest中htmltestrunner导出测试报告,可以修改htmltestrunner中的测试代码
其中,的python3中用HTMLTestRunner.py报ImportError: No module named 'StringIO'如何解决见:
https://www.jb51.net/article/168573.htm
来源:https://blog.csdn.net/Hh20161314/article/details/97293642
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
CentOS 7.2 Yum编译安装MySQL 5.6
交互设计中的“007功能”
![](https://img.aspxhome.com/file/UploadPic/200710/29/2007102912114153s.jpg)
python中range()与xrange()用法分析
![](https://img.aspxhome.com/file/2023/2/97272_0s.png)
不同情况下修复Access数据库的实用方法
运筹学-Python实现图论与最短距离
![](https://img.aspxhome.com/file/2023/1/118401_0s.png)
vue货币过滤器的实现方法
![](https://img.aspxhome.com/file/2023/6/126636_0s.jpg)
MySQL 使用自定义变量进行查询优化
python基础教程之lambda表达式使用方法
golang常用库之操作数据库的orm框架-gorm基本使用详解
Python数据报表之Excel操作模块用法分析
![](https://img.aspxhome.com/file/2023/2/123662_0s.png)
利用Python复制文件的9种方法总结
![](https://img.aspxhome.com/file/2023/6/70656_0s.png)
vue实现选中效果
W3C发布HTML 5草案 最终版将于2010年敲定
Python连接数据库使用matplotlib画柱形图
![](https://img.aspxhome.com/file/2023/2/99562_0s.png)
OpenCV哈里斯(Harris)角点检测的实现
![](https://img.aspxhome.com/file/2023/6/134916_0s.png)
遗传算法python版
![](https://img.aspxhome.com/file/2023/5/127685_0s.jpg)
wxPython实现整点报时
python学习--使用QQ邮箱发送邮件代码实例
![](https://img.aspxhome.com/file/2023/4/76164_0s.png)
网页设计图标使用指南[译]
![](https://img.aspxhome.com/file/UploadPic/20093/11/01-19s.jpg)
在Python中通过机器学习实现人体姿势估计
![](https://img.aspxhome.com/file/2023/0/127650_0s.png)