Python 中 unittest 的一个误会...
2006-03-01 17:47
344 查看
一直以为 TestCase 中的 setUp( ) 与 tearDown( ) 是针对 整个TestCase 的
昨天 偶然发现 原来不是这个样子.这两个方法都是在每个测试方法先后调用,帮助上是这么写的
setUp( )
Method called to prepare the test fixture. This is called immediately before calling the test method; any exception raised by this method will be considered an error rather than a test failure. The default implementation does nothing.
tearDown( )
Method called immediately after the test method has been called and the result recorded. This is called even if the test method raised an exception, so the implementation in subclasses may need to be particularly careful about checking internal state. Any exception raised by this method will be considered an error rather than a test failure. This method will only be called if the setUp() succeeds, regardless of the outcome of the test method. The default implementation does nothing.
相关代码如下,TestCase 中的__Call__
def __call__(self, result=None):
if result is None: result = self.defaultTestResult()
result.startTest(self)
testMethod = getattr(self, self.__testMethodName)
try:
try:
self.setUp()
except KeyboardInterrupt:
raise
except:
result.addError(self, self.__exc_info())
return
ok = 0
try:
testMethod()
ok = 1
except self.failureException:
result.addFailure(self, self.__exc_info())
except KeyboardInterrupt:
raise
except:
result.addError(self, self.__exc_info())
try:
self.tearDown()
except KeyboardInterrupt:
raise
except:
result.addError(self, self.__exc_info())
ok = 0
if ok: result.addSuccess(self)
finally:
result.stopTest(self)
昨天 偶然发现 原来不是这个样子.这两个方法都是在每个测试方法先后调用,帮助上是这么写的
setUp( )
Method called to prepare the test fixture. This is called immediately before calling the test method; any exception raised by this method will be considered an error rather than a test failure. The default implementation does nothing.
tearDown( )
Method called immediately after the test method has been called and the result recorded. This is called even if the test method raised an exception, so the implementation in subclasses may need to be particularly careful about checking internal state. Any exception raised by this method will be considered an error rather than a test failure. This method will only be called if the setUp() succeeds, regardless of the outcome of the test method. The default implementation does nothing.
相关代码如下,TestCase 中的__Call__
def __call__(self, result=None):
if result is None: result = self.defaultTestResult()
result.startTest(self)
testMethod = getattr(self, self.__testMethodName)
try:
try:
self.setUp()
except KeyboardInterrupt:
raise
except:
result.addError(self, self.__exc_info())
return
ok = 0
try:
testMethod()
ok = 1
except self.failureException:
result.addFailure(self, self.__exc_info())
except KeyboardInterrupt:
raise
except:
result.addError(self, self.__exc_info())
try:
self.tearDown()
except KeyboardInterrupt:
raise
except:
result.addError(self, self.__exc_info())
ok = 0
if ok: result.addSuccess(self)
finally:
result.stopTest(self)
相关文章推荐
- python调用HTMLTestRunner+unittest实现一次执行多个测试类,并生成与每个测试类对应的测试报告,并不像某些人写的每次只执行一个测试类,具体看代码,附上整个project代码
- 【selenium】python+selenium+unittest,关于每次执行完一个测试用例都关闭浏览器等时间较长的问题之解决方案
- python3.5+selenium3.4自动化测试9_selenium框架2.0-unittest(上)
- Python unittest parametrized test cases
- python unittest使用技巧
- python unittest 之mock学习笔记
- Python之自动单元测试之一(unittest使用实例)
- python 单元测试(assert 或者 unittest.TestCase)
- python unittest 像 TestNG 一样prodvider
- python+selenium的unittest框架通过读取properties文件自定义选择IE/Firefox/Chrome浏览器
- 深入解读Python的unittest并拓展HTMLTestRunner
- Python接口测试之unittest框架
- python-unittest(6)
- Python unittest中常用到的assert
- Python单元测试框架unittest使用方法讲解
- python unittest单元测试
- 让一个继承unittest.TestCase的类下的setUp和tearDown只执行一次
- python单元测试unittest实例详解
- python:解决pycharm运行py文件时只有unittest选项的方法
- Python之unittest测试代码