python unittest框架中doCleanups妙用
偶看unittest官方文档中,发现一个很好用的功能函数doCleanups,看看官方是怎么解释的:
doCleanups()
This method is called unconditionally after tearDown(), or after setUp() if setUp() raises an exception. It is responsible for calling all the cleanup functions added by addCleanup(). If you need cleanup functions to be called prior to tearDown() then you can call doCleanups() yourself. doCleanups() pops methods off the stack of cleanup functions one at a time, so it can be called at any time.
该功能函数在2.7之后就支持了
来来来,简单用中文说明一下吧,大概意思如下:
无条件的执行改函数,在tearnDown()之后或者在setUp()之后即使setUp失败的情况下也会执行
与addCleanup搭配使用
看完后大家都明白了吧,平时我们使用tearDown函数时,当setUp运行失败时,tearDown就不执行了,所以就会有遗留资源的存在,正好doCleanups帮我们解决了这个困扰,不用再写try...except....finally了。
下面,用个实例来看看doCleanups怎么运行的
#coding:utf-8
'''
Created on 2016年8月31日
@author: zq
'''
import unittest class my(unittest.TestCase): def a(self):
print "aaaa" def setUp(self):
print "setUp"
raise IOError,"errorororororo" #这里特意让setUp产生错误 def test_1(self):
'''i dont konw'''
print "test_1" def tearDown(self):
print 'this tearDown' def doCleanups(self):
print "this is cleanups" def test_2(self):
print "test_2" @classmethod
def tearDownClass(cls):
print "teardownClass...." if __name__=="__main__":
test=unittest.TestSuite()
test.addTest(my('test_1'))
test.addTest(my('test_2'))
runner=unittest.TextTestRunner()
runner.run(test)
我们运行后看看结果:
setUp
this is cleanups
setUp
this is cleanups
teardownClass....
EE
看看,从运行结果中可以看出。即使setUp出现错误的情况下,doCleanups还是运行了。
python unittest框架中doCleanups妙用的更多相关文章
- python unittest框架中addCleanup函数详解
接上一篇doCleanups说明,这次介绍下另一个很好用的函数:addCleanup 还是老规矩,看官方文档说明: addCleanup(function, *args, **kwargs)¶ Add ...
- unittest框架中读取有特殊符号的配置文件内容的方法-configparser的RawConfigParser类应用
在搭建Unittest框架中,出现了一个问题,配置文件.ini中,出现了特殊字符如何处理? 通过 1.configparser的第三方库对应的ConfigParser类,无法完成对特殊字符的读取: # ...
- python的unittest框架中的assert断言
unittest框架自带断言,如果想用assert断言,一定要引入unittest.TestCase框架才行,不然不会自动识别assert断言
- 解惑unittest框架中导入HTMLTestRunner模块后正常运行却无法生成HTML报告问题
1.HTMLTestRunner介绍 HTMLTestRunner是一个第三方的unittest HTML报告库,用于python单元测试框架的TestRunner.它是生成一个HTML报告,以一目了 ...
- python unittest addCleanup中也加失败截图功能
在python web自动化测试中失败截图方法汇总一文中提到了失败截图的方法 但在实际测试中,如果我们的测试用例中加了addCleanups动作,如果addCleanups中动作失败了,就不会截图.那 ...
- python+unittest框架第一天unittest之简单认识Test Fixure:测试固件【8月17更新】
20万的慢慢会实现的吧,hhh unittest框架,我就不在介绍了,百度有很详细的介绍. 我们只要了解: 1.unittest是单元测试框架 2.它提供用例组织与执行:在实际工作中案例可能有上百条, ...
- python的unittest框架中如何删除测试数据,清理环境,可以通过addCleanup函数
def addCleanup(self, function, *args, **kwargs): """Add a function, with arguments, t ...
- python+unittest框架整理(一点点学习前辈们的封装思路,一点点成长。。。)
预期框架整理目标: 1.单个用例维护在单个.py文件中可单个执行,也可批量生成组件批量执行 2.对定位参数,定位方法,业务功能脚本,用例脚本,用例批量执行脚本,常用常量进行分层独立,各自维护在单独的. ...
- python unittest框架理解与总结(二)
unittest基本原理: ♦整个平台的搭建使用的是python的unittest测试框架,这里简单介绍下unittest模块的简单应用. ♦unittest是python的标准测试库,相比于其他测试 ...
随机推荐
- iview 下拉刷新loadTop报错解决
<div class="noData" v-if="lifeList.length==0"> <img src="../assets ...
- MyEclipse使用笔记
简单记录下个人常用的一些MyEclipse设置 VS颜色方案 Window-->Preference-->Java->Editor-->Syntax Coloring Clas ...
- jquery与php的HTML转义与反转义
1.jquery (1)Html转义 var tmp = '<a href="https://www.baidu.com/">连接</a>'; var tm ...
- php命令
今天因为psi无法在5.3版本上运行,正好看了下这些php版本的特性. 无意中,5.4版本 Buid-in web server内置了一个简单的Web服务器 $ php -S localhost: 于 ...
- Mac下配置svn服务器
Mac OS X 系统已经内置了svn支持,所以需要做的就只是配置,可以用svnadmin –vsersion(svnserve –version)查看.希望能对 您配置 SVN.进行开发版本控制有所 ...
- Explaining Delegates in C# - Part 3 (Events 2)
I was thinking that the previous post on Events and Delegates was quite self-explanatory. A couple o ...
- Nginx 用户认证
访问一些比较私密的页面 ( 如管理后台,数据后台 ) 时,我们就可以设置访问该页面时需要使用用户名和密码进行验证,这就是用户认证 [root@localhost ~]$ cat /usr/local/ ...
- mybatis 之 parameterType="list"
<!-- 根据货品编号查找货品图片地址,货品ID,商品ID,货品名称 --> <select id="getGoodsInfoByGoodsNo" paramet ...
- 新唐ISP操作步骤(转)
1,电脑上装上“NuMicro_ICP_Programming_Tool_v1.18.5320.zip”:2,把目标板通过SWD口的NU-LINK连接到电脑的USB口上:3,打开桌面的“NuMicro ...
- Hadoop计算平均值【转】
file1.txt a 1b 2a 3b 3a 5b 7c 3c 5 file2.txt a 1b 7c 5a 1c 3 结果: a 2.2b 4.75c 4.0 代码: package org.ap ...