前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >pytest文档54-Hooks函数terminal打印测试结果(pytest_report_teststatus)

pytest文档54-Hooks函数terminal打印测试结果(pytest_report_teststatus)

作者头像
上海-悠悠
发布2020-09-10 17:44:42
9700
发布2020-09-10 17:44:42
举报

前言

使用命令行执行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

代码语言:javascript
复制
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

代码语言:javascript
复制
>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文件写钩子函数

代码语言:javascript
复制
# 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

代码语言:javascript
复制
>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’ 判断到前置操作的结果

代码语言:javascript
复制
# 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

运行结果

代码语言:javascript
复制
>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

代码语言:javascript
复制
# 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')

于是控制台的结果,就可以改了

代码语言:javascript
复制
>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获取到,可以这样写

代码语言:javascript
复制
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’

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-09-08,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 从零开始学自动化测试 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • pytest_report_teststatus
  • 关于Error异常
  • report相关的属性
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档