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'


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM