今天有一个需求, 在单元测试失败的时候打印一些日志, 我们管他叫 dosomething 吧 ,反正就是做一些操作

查了下并没有查到相关的方法, 于是研究了一波unittest 的源码

发现了这个东西

try:
self._outcome = outcome with outcome.testPartExecutor(self):
self.setUp()
if outcome.success:
outcome.expecting_failure = expecting_failure
with outcome.testPartExecutor(self, isTest=True):
testMethod()
outcome.expecting_failure = False
with outcome.testPartExecutor(self):
self.tearDown() self.doCleanups()
for test, reason in outcome.skipped:
self._addSkip(result, test, reason)
self._feedErrorsToResult(result, outcome.errors)
if outcome.success:
if expecting_failure:
if outcome.expectedFailure:
self._addExpectedFailure(result, outcome.expectedFailure)
else:
self._addUnexpectedSuccess(result)
else:
result.addSuccess(self)
return result
finally:
result.stopTest(self)
if orig_result is None:
stopTestRun = getattr(result, 'stopTestRun', None)
if stopTestRun is not None:
stopTestRun() # explicitly break reference cycles:
# outcome.errors -> frame -> outcome -> outcome.errors
# outcome.expectedFailure -> frame -> outcome -> outcome.expectedFailure
outcome.errors.clear()
outcome.expectedFailure = None # clear the outcome, no more needed
self._outcome = None

其中重点关注下testMethod() 这个正是我们执行的用例

于是去看了下testPartExecutor 用例失败的处理是在这里进行处理的

def testPartExecutor(self, test_case, isTest=False):
old_success = self.success
self.success = True
try:
yield
except KeyboardInterrupt:
raise
except SkipTest as e:
self.success = False
self.skipped.append((test_case, str(e)))
except _ShouldStop:
pass
except:
exc_info = sys.exc_info()
if self.expecting_failure:
self.expectedFailure = exc_info
else:
self.success = False
self.errors.append((test_case, exc_info))
# explicitly break a reference cycle:
# exc_info -> frame -> exc_info
exc_info = None
else:
if self.result_supports_subtests and self.success:
self.errors.append((test_case, None))
finally:
self.success = self.success and old_success

奈何, 他只是在self.errors (其中self为我们测试类的一个实例)中加了点东西

于是对self.errors 进行观察,发现及时用例是正常的,他依然由内容.

这..............于是我想到他最终是怎么打出来失败的log的

看到 上面代码中的 self._feedErrorsToResult(result, outcome.errors)

于是找到了这个东西

def _feedErrorsToResult(self, result, errors):
for test, exc_info in errors:
if isinstance(test, _SubTest):
result.addSubTest(test.test_case, test, exc_info)
elif exc_info is not None:
if issubclass(exc_info[0], self.failureException):
result.addFailure(test, exc_info)
else:
result.addError(test, exc_info)

从上面的代码我们可以知道 如果 error的第二项是None那么就是一个执行成功的用例,经过实验并确认了这个事情

现在知道了 unittest 是如何处理 失败用例的了

于是便有了下面这种方法

def tearDown(self):
errors = self._outcome.errors
for test, exc_info in errors:
if exc_info:
# dosomething
pass

上面这种方法尽量少的改变原来的逻辑, 想到一种新的方法解决问题

既然unittest没有处理这个事情,那我们魔改之

于是有了下面这种方法

注意: 不建议魔改代码

import sys
import contextlib
import unittest
from unittest.case import SkipTest, _ShouldStop, _Outcome @contextlib.contextmanager
def testPartExecutor(self, test_case, isTest=False):
old_success = self.success
self.success = True
try:
yield
except Exception:
try:
# if error
getattr(test_case, test_case._testMethodName).__func__._error = True
raise
except KeyboardInterrupt:
raise
except SkipTest as e:
self.success = False
self.skipped.append((test_case, str(e)))
except _ShouldStop:
pass
except:
exc_info = sys.exc_info()
if self.expecting_failure:
self.expectedFailure = exc_info
else:
self.success = False
self.errors.append((test_case, exc_info))
# explicitly break a reference cycle:
# exc_info -> frame -> exc_info
exc_info = None
else:
if self.result_supports_subtests and self.success:
self.errors.append((test_case, None))
finally:
self.success = self.success and old_success _Outcome.testPartExecutor = testPartExecutor class MyTest(unittest.TestCase):
def test_1(self):
print("test_1") def test_2(self):
print("test_2")
raise ValueError def tearDown(self):
if hasattr(getattr(self, self._testMethodName), "_error"):
# dosomething
pass # def tearDown(self):
# 推荐这种方法
# errors = self._outcome.errors
# for test, exc_info in errors:
# if exc_info:
# # dosomething
# pass if __name__ == '__main__':
unittest.main()

这样我们就可以在用例执行失败后在tearDown的时候做一些操作

python 单元测试中处理用例失败的情况的更多相关文章

  1. unittest+HtmlTestRunner+python接口自动化测试:用例失败发送邮件

    一点啰嗦:发送邮件python中有另一个支持的第三方库yagmail更轻量级,代码参考可移步至此:https://www.cnblogs.com/princessironfan/p/13220601. ...

  2. c#调用python脚本实现排序(适用于python脚本中不包含第三方模块的情况)

    引用:https://www.cnblogs.com/zoe-yan/p/10374757.html 利用vs2017c#调用python脚本需要安装IronPython.我是通过vs2017的工具- ...

  3. QTP场景恢复之用例失败自动截图

    以下代码是在QC里运行QTP来执行脚本过程,当执行过程中发现用例失败后就会自动截图,然后把用例返回到最初始的状态,模拟了场景恢复的机制 Class QCImageErrorCapture Dim qt ...

  4. Python单元测试框架unittest重要属性 与 用例编写思路

    前言 本文为转载,原文地址作者列举python unittest这个测试框架的主要属性和 测试用例思路 unittest单元测试框架不仅可以适用于单元测试,还可以适用WEB自动化测试用例的开发与执行, ...

  5. day11_单元测试_读取yaml文件中的用例,自动获取多个yaml文件内容执行生成报告

    一.使用.yaml格式的文件直接可以存放字典类型数据,如下图,其中如果有-下一行有缩进代表这是个list,截图中是整体是一个list,其中有两部分,第二部分又包含另外一个list 二.单元测试:开发自 ...

  6. hanlp在Python环境中的安装失败后的解决方法

    Hanlp是由一系列模型与算法组成的javag工具包,目标是普及自然语言处理再生环境中的应用.有很多人在安装hanlp的时候会遇到安装失败的情况,下面就是某大神的分享的在python环境中安装失败的解 ...

  7. Python中的单例设计

    01. 单例设计模式 设计模式 设计模式 是 前人工作的总结和提炼,通常,被人们广泛流传的设计模式都是针对 某一特定问题 的成熟的解决方案 使用 设计模式 是为了可重用代码.让代码更容易被他人理解.保 ...

  8. Python单元测试简介及Django中的单元测试

    Python单元测试简介及Django中的单元测试 单元测试负责对最小的软件设计单元(模块)进行验证,unittest是Python自带的单元测试框架. 单元测试与功能测试都是日常开发中必不可少的部分 ...

  9. Python单元测试框架之pytest -- 断言

    对于测试来讲,不管是功能测试,自动化测试,还是单元测试.一般都会预设一个正确的预期结果,而在测试执行的过程中会得到一个实际的结果.测试的成功与否就是拿实际的结果与预期的结果进行比较.这个比的过程实际就 ...

随机推荐

  1. 模式匹配-BF算法

    /***字符串匹配算法***/ #include<cstring> #include<iostream> using namespace std; #define OK 1 # ...

  2. express 与 mvc

    听人介绍,说express.js是一个for nodejs的mvc框架. 既然是MVC,那么,express里面,什么是M,什么是V,又什么是C? C,很容易看出来,就是路由.express的路由机制 ...

  3. Getting console.log output with Selenium Python API bindings

    持久化存储 Getting console.log output from Chrome with Selenium Python API bindings - Stack Overflow http ...

  4. 【C++程序不输出】到底是什么造成了程序不输出

    (ubuntu 16.04) 最近做题的时候,经常莫名其妙地,程序写的明明没毛病但是就是输出不了,气得我呀 然后某一次突然发现了原因,竟然是输出之后没有加endl或者空格! 例如: cout<& ...

  5. TGraphicControl和TCustomControl自绘过程的理论解释

    TGraphicControl = class(TControl) // 这个类实在是简单,因为所有事情都已经委托给它的父Win控件了,只要管自己即可 private FCanvas: TCanvas ...

  6. [Sqlite]--&gt;数据迁移备份--从低版本号3.6.2到高版本号3.8.6

    引子: 1. Sqlite在Windows.Linux 和 Mac OS X 上的安装过程 2.嵌入式数据库的安装.建库.建表.更新表结构以及数据导入导出等等具体过程记录 个字段IPHONE和LOGI ...

  7. java 多线程——并发编程模型 学习笔记

                                                                                                 并发编程模型 ...

  8. [BZOJ 1698] 荷叶池塘

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1698 [算法] 最短路 [代码] #include<bits/stdc++.h ...

  9. eclipse和jdk版本对应问题

    日常开发中,32位eclipse要用32位jdk,64位则必须要用64位jdk,否则启动时就会报错,load jvm.dll失败,昨天又遇到了这个问题.更换对应的版本之后就好了.tomcat等应用也有 ...

  10. [Swift通天遁地]二、表格表单-(14)实时调整表单元素的激活和失效

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...