pytest文档54-Hooks函数terminal打印测试结果(pytest_report_teststatus)
前言
使用命令行执行pytest用例的时候,会在 terminal 终端打印整个用例的测试结果:
- .代表通过的用例
- F代表失败的用例
- E代表异常的用例
 如果我们不喜欢这种报告结果,可以通过 pytest_report_teststatus 钩子函数改变测试报告的内容,接下来试试吧.改成√,把F改成x,这样更直观。
pytest_report_teststatus
pytest_report_teststatus(report, config): 返回各个测试阶段的result, 可以用when属性来区分不同阶段。
- when=='setup' 用例的前置操作
- when=='call' 用例的执行
- when=='teardown' 用例的后置操作
运行案例test_x.py
import pytest
# 作者-上海悠悠 QQ交流群:717225969
# blog地址 https://www.cnblogs.com/yoyoketang/
def test_01():
    a = "hello"
    b = "hello"
    assert a == b
def test_02():
    a = "hello"
    b = "hello world"
    assert a == b
def test_03():
    a = "hello"
    b = "hello world"
    assert a in b
def test_04():
    a = "hello"
    b = "hello world"
    assert a not in b
命令行执行pytest test_x.py --tb=line
>pytest test_x.py --tb=line
============================= test session starts =============================
collected 4 items
test_x.py .F.F                                                           [100%]
================================== FAILURES ===================================
D:\test_x.py:13: AssertionError: assert 'hello' == 'hello world'
D:\test_x.py:25: AssertionError: assert 'hello' not in 'hello world'
===================== 2 failed, 2 passed in 0.07 seconds ======================
运行的结果是.和F,我们希望改成√和x,在conftest.py文件写钩子函数
# conftest.py
import pytest
# 作者-上海悠悠 QQ交流群:717225969
# blog地址 https://www.cnblogs.com/yoyoketang/
def pytest_report_teststatus(report, config):
    '''turn . into √,turn F into x'''
    if report.when == 'call' and report.failed:
        return (report.outcome, 'x', 'failed')
    if report.when == 'call' and report.passed:
        return (report.outcome, '√', 'passed')
重新运行pytest test_x.py --tb=line
>pytest test_x.py --tb=line
collected 4 items
test_x.py √x√x                                                           [100%]
================================== FAILURES ===================================
D:\soft\kecheng202004\xuexi\test_x.py:13: AssertionError: assert 'hello' == 'hello world'
D:\soft\kecheng202004\xuexi\test_x.py:25: AssertionError: assert 'hello' not in 'hello world'
===================== 2 failed, 2 passed in 0.07 seconds ======================
关于Error异常
前面这篇https://www.cnblogs.com/yoyoketang/p/12609871.html讲到关于测试用例的执行结果,
当 setup 出现异常的时候,用例才会Error,于是可以通过report.when == 'setup' 判断到前置操作的结果
# test_x.py
import pytest
# 作者-上海悠悠 QQ交流群:717225969
# blog地址 https://www.cnblogs.com/yoyoketang/
@pytest.fixture()
def login():
    print("前置操作:准备数据")
    assert 1 == 2   # 前置出现异常
    yield
    print("后置操作:清理数据")
def test_01(login):
    a = "hello"
    b = "hello"
    assert a == b
def test_02():
    a = "hello"
    b = "hello world"
    assert a == b
def test_03():
    a = "hello"
    b = "hello world"
    assert a in b
def test_04():
    a = "hello"
    b = "hello world"
    assert a not in b
运行结果
>pytest test_x.py --tb=line
============================= test session starts =============================
collected 4 items
test_x.py Ex√x                                                           [100%]
=================================== ERRORS ====================================
__________________________ ERROR at setup of test_01 __________________________
E   assert 1 == 2
---------------------------- Captured stdout setup ----------------------------
前置操作:准备数据
================================== FAILURES ===================================
D:\soft\kecheng202004\xuexi\test_x.py:21: AssertionError: assert 'hello' == 'hello world'
D:\soft\kecheng202004\xuexi\test_x.py:33: AssertionError: assert 'hello' not in 'hello world'
================= 2 failed, 1 passed, 1 error in 0.09 seconds =================
当前置失败的时候,改成0
# conftest.py
import pytest
# 作者-上海悠悠 QQ交流群:717225969
# blog地址 https://www.cnblogs.com/yoyoketang/
def pytest_report_teststatus(report, config):
    '''turn . into √,turn F into x, turn E into 0'''
    if report.when == 'call' and report.failed:
        return (report.outcome, 'x', 'failed')
    if report.when == 'call' and report.passed:
        return (report.outcome, '√', 'passed')
    if report.when == 'setup' and report.failed:
        return (report.outcome, '0', 'error')
于是控制台的结果,就可以改了
>pytest test_x.py --tb=line
============================= test session starts =============================
collected 4 items
test_x.py 0x√x                                                           [100%]
================================== FAILURES ===================================
D:\soft\kecheng202004\xuexi\test_x.py:7: assert 1 == 2
D:\soft\kecheng202004\xuexi\test_x.py:21: AssertionError: assert 'hello' == 'hello world'
D:\soft\kecheng202004\xuexi\test_x.py:33: AssertionError: assert 'hello' not in 'hello world'
===================== 3 failed, 1 passed in 0.07 seconds ======================
skip的用例可以通过report.skiped获取到,可以这样写
if report.skipped:
        return (report.outcome, '/', 'skipped')
report相关的属性
report相关的属性,参考以下
'_from_json',
'_get_verbose_word',
'_to_json',
'caplog',
'capstderr',
'capstdout',
'count_towards_summary',
'duration',
'failed',
'from_item_and_call',
'fspath',
'get_sections',
'head_line',
'keywords',
'location',
'longrepr',
'longreprtext',
'nodeid',
'outcome',
'passed',
'sections',
'skipped',
'toterminal',
'user_properties',
'when'
pytest文档54-Hooks函数terminal打印测试结果(pytest_report_teststatus)的更多相关文章
- pytest文档7-pytest-html生成html报告
		前言 pytest-HTML是一个插件,pytest用于生成测试结果的HTML报告.兼容Python 2.7,3.6 pytest-html 1.github上源码地址[https://github. ... 
- pytest文档3-pycharm运行pytest
		前言 上一篇pytest文档2-用例运行规则已经介绍了如何在cmd执行pytest用例,平常我们写代码在pycharm比较多 写完用例之后,需要调试看看,是不是能正常运行,如果每次跑去cmd执行,太麻 ... 
- pytest文档55-plugins插件开发
		前言 前面一篇已经学会了使用hook函数改变pytest运行的结果,代码写在conftest.py文件,实际上就是本地的插件了. 当有一天你公司的小伙伴觉得你写的还不错,或者更多的小伙伴想要你这个功能 ... 
- C#通过调用WinApi打印PDF文档类,服务器PDF打印、IIS PDF打印
		其他网站下载来的类,可以用于Winform.Asp.Net,用于服务器端PDF或其他文件打印. 直接上代码: using System; using System.Collections.Generi ... 
- pytest文档19-doctest测试框架
		前言 doctest从字面意思上看,那就是文档测试.doctest是python里面自带的一个模块,它实际上是单元测试的一种. 官方解释:doctest 模块会搜索那些看起来像交互式会话的 Pytho ... 
- pytest文档1-环境准备与入门
		前言 首先说下为什么要学pytest,在此之前相信大家已经掌握了python里面的unittest单元测试框架,那再学一个框架肯定是需要学习时间成本的. 刚开始我的内心是拒绝的,我想我用unittes ... 
- pytest文档46-关于https请求警告问题(InsecureRequestWarning: Unverified HTTPS request is being made)
		前言 使用 pytest 执行 https 请求用例的时候,控制台会出现警告:InsecureRequestWarning: Unverified HTTPS request is being mad ... 
- pytest文档43-元数据使用(pytest-metadata)
		前言 什么是元数据?元数据是关于数据的描述,存储着关于数据的信息,为人们更方便地检索信息提供了帮助. pytest 框架里面的元数据可以使用 pytest-metadata 插件实现.文档地址http ... 
- python文档字符串(函数使用说明)
		关键字: 函数说明.help()函数 1.效果图: 2.代码: # 文档字符串( doc str) 是 函数使用说明 # 用法: 在函数第一行写一个字符串 def fn(*nums): ''' 函数的 ... 
随机推荐
- GuestOS? HostOS?
			起因 今天在网上看到一篇文章 有几个陌生的关键词不太熟悉,就随笔记一下. 名词解释 # OS :操作系统 # VM(虚拟机) 里的OS 称为 GuestOS # 物理机 ... 
- cookie和session讲解
			1.cookie是什么? 保存在浏览器本地上的一组组键值对 2.session是什么? 保存在服务器上的一组组键值对 3.为什么要有cookie? HTTP是无协议状态,每次请求都是互相独立的,没有办 ... 
- 记录一次OCR程序开发的尝试
			记录一次OCR程序开发的尝试 最近工作中涉及到一部分文档和纸质文档的校验工作,就想把纸质文件拍下来,用文字来互相校验.想到之前调用有道智云接口做了文档翻译.看了下OCR文字识别的API接口,有道提供了 ... 
- [深入理解JVM虚拟机]第2章-Java内存区域与内存溢出异常
			2.0引-Java内存区域中,栈内存和堆内存分别装什么,为什么? 栈:解决程序的运行问题,即程序如何执行,或者说如何处理数据. 堆:解决的是数据存储的问题,即数据怎么放,放在哪儿. 参考链接https ... 
- 解读Java NIO Buffer
			从jdk1.4开始,java中引入了nio包,提供了非阻塞式的网络编程模型,提供网络性能.nio中核心组件有三个:channel.buffer.selector.这里主要探讨buffer的概念和使用. ... 
- Gradle实战(01)--介绍与安装
			前言 本章你将学习到 Gradle的介绍 Gradle的安装 Gradle的hello world 1 Gradle的介绍 Gradle是专注于灵活性和性能的开源构建自动化工具 Gradle构建脚本是 ... 
- 云计算openstack共享组件——时间同步服务ntp(2)
			一.标准时间讲解 地球分为东西十二个区域,共计 24 个时区 格林威治作为全球标准时间即 (GMT 时间 ),东时区以格林威治时区进行加,而西时区则为减. 地球的轨道并非正圆,在加上自转速度逐年递减, ... 
- Docker:一、开始部署第一个Asp.net应用
			工具: docker desktop :一个使用Docker的IDE工具,可以理解为SourceTree,也是使用git的一个桌面化工具: kitematic :配合desctop,用来管理本地的镜像 ... 
- python中yield的用法详解——最简单,最清晰的解释(转载)
			原文链接 首先我要吐槽一下,看程序的过程中遇见了yield这个关键字,然后百度的时候,发现没有一个能简单的让我懂的,讲起来真TM的都是头头是道,什么参数,什么传递的,还口口声声说自己的教程是最简单的, ... 
- 安装Windows10操作系统 - 初学者系列 - 学习者系列文章
			今天无事,就将安装操作系统的几种方式进行了总结( https://www.cnblogs.com/lzhdim/p/13719725.html ).这篇博文主要是对安装windows10操作系统的过程 ... 
