偶看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妙用的更多相关文章

  1. python unittest框架中addCleanup函数详解

    接上一篇doCleanups说明,这次介绍下另一个很好用的函数:addCleanup 还是老规矩,看官方文档说明: addCleanup(function, *args, **kwargs)¶ Add ...

  2. unittest框架中读取有特殊符号的配置文件内容的方法-configparser的RawConfigParser类应用

    在搭建Unittest框架中,出现了一个问题,配置文件.ini中,出现了特殊字符如何处理? 通过 1.configparser的第三方库对应的ConfigParser类,无法完成对特殊字符的读取: # ...

  3. python的unittest框架中的assert断言

    unittest框架自带断言,如果想用assert断言,一定要引入unittest.TestCase框架才行,不然不会自动识别assert断言

  4. 解惑unittest框架中导入HTMLTestRunner模块后正常运行却无法生成HTML报告问题

    1.HTMLTestRunner介绍 HTMLTestRunner是一个第三方的unittest HTML报告库,用于python单元测试框架的TestRunner.它是生成一个HTML报告,以一目了 ...

  5. python unittest addCleanup中也加失败截图功能

    在python web自动化测试中失败截图方法汇总一文中提到了失败截图的方法 但在实际测试中,如果我们的测试用例中加了addCleanups动作,如果addCleanups中动作失败了,就不会截图.那 ...

  6. python+unittest框架第一天unittest之简单认识Test Fixure:测试固件【8月17更新】

    20万的慢慢会实现的吧,hhh unittest框架,我就不在介绍了,百度有很详细的介绍. 我们只要了解: 1.unittest是单元测试框架 2.它提供用例组织与执行:在实际工作中案例可能有上百条, ...

  7. python的unittest框架中如何删除测试数据,清理环境,可以通过addCleanup函数

    def addCleanup(self, function, *args, **kwargs): """Add a function, with arguments, t ...

  8. python+unittest框架整理(一点点学习前辈们的封装思路,一点点成长。。。)

    预期框架整理目标: 1.单个用例维护在单个.py文件中可单个执行,也可批量生成组件批量执行 2.对定位参数,定位方法,业务功能脚本,用例脚本,用例批量执行脚本,常用常量进行分层独立,各自维护在单独的. ...

  9. python unittest框架理解与总结(二)

    unittest基本原理: ♦整个平台的搭建使用的是python的unittest测试框架,这里简单介绍下unittest模块的简单应用. ♦unittest是python的标准测试库,相比于其他测试 ...

随机推荐

  1. 16 go操作Mysql

    mysql模块下载 mysql模块我们从github上下载,地址为:www.github.com/go-sql-driver/mysql go get "github.com/go-sql- ...

  2. python-反射、新式类与经典类搜索的优先级

    preface include: getattr setattr delattr hasattr class webserver(object): def __init__(self,num): se ...

  3. dos 批处理中%cd% 和%~dp0%的区别

    看网上介绍区别,写的好复杂,其实很简单: %cd%  在批处理和命令窗口都能使用.用于打印,当前工作路径. %~dp0% 则只能用于批处理中,用于获得当前批处理文件所在的路径. 做个试验试一下: @e ...

  4. 错误 Unable to find vcvarsall.bat 的终极无敌最完美的解决办法

    Windows 上通过 pip 安装 python 包,经常会出现这种错误. 如:pip install pyodbc. 这种错误的简单明了解释就是:python 编译器找不到计算机上面的 VC 编译 ...

  5. WebSocket、Socket、TCP、HTTP区别

    https://www.cnblogs.com/merray/p/7918977.html

  6. 解决Spring Boot中,通过filter打印post请求的 request body 问题

    http://slackspace.de/articles/log-request-body-with-spring-boot/ (filter + RequestWrapper:最优雅的写法) ht ...

  7. 图解Python深拷贝和浅拷贝

    Python中,对象的赋值,拷贝(深/浅拷贝)之间是有差异的,如果使用的时候不注意,就可能产生意外的结果. 下面本文就通过简单的例子介绍一下这些概念之间的差别. 对象赋值 直接看一段代码: will ...

  8. windows本地hash值获取和破解详解

    powershell版的procdump https://www.t00ls.net/articles-48428.html procdump procdump是微软官方提供的一个小工具, 微软官方下 ...

  9. 【RF库Collections测试】Remove From List

    Name:Remove From ListSource:Collections <test library>Arguments:[ list_ | index ]Removes and r ...

  10. FFMPEG转换WAV到MP3

    下载FFMPEG https://ffmpeg.zeranoe.com/builds/ Example to encode VBR MP3 audio with ffmpeg using the li ...