1. 程式人生 > 其它 >Python測試框架pytest(11)Hooks函式 - pytest_terminal_summary統計測試結果

Python測試框架pytest(11)Hooks函式 - pytest_terminal_summary統計測試結果

當用例執行完成後,希望獲取到執行的結果,方便了解用例的執行情況,這時候就可以使用 pytest_terminal_summary 來進行測試結果的統計(可以拿到所有的執行結果)。

pytest_terminal_summary 原始碼:

引數:

  • terminalreporter(內部使用的終端測試報告物件)

  • exitstatus(返回給作業系統的返回碼)

  • config(pytest 的 config 物件)

示例一:正常情況

建立conftest.py檔案,pytest_terminal_summary函式用於收集測試結果。

指令碼程式碼:

#!/usr/bin/env python
# -*- coding: utf-8 -*- """ 微信公眾號:AllTests軟體測試 """ import time def pytest_terminal_summary(terminalreporter, exitstatus, config): """ 收集測試結果 """ print(terminalreporter.stats) print("total:", terminalreporter._numcollected) print('passed:', len([i for i in terminalreporter.stats.get('
passed', []) if i.when != 'teardown'])) print('failed:', len([i for i in terminalreporter.stats.get('failed', []) if i.when != 'teardown'])) print('error:', len([i for i in terminalreporter.stats.get('error', []) if i.when != 'teardown'])) print('skipped:', len([i for i in terminalreporter.stats.get('
skipped', []) if i.when != 'teardown'])) print('成功率:%.2f' % (len(terminalreporter.stats.get('passed', []))/terminalreporter._numcollected*100)+'%') # terminalreporter._sessionstarttime 會話開始時間 duration = time.time() - terminalreporter._sessionstarttime print('total times:', duration, 'seconds')

建立test_a.py檔案

指令碼程式碼:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公眾號:AllTests軟體測試
"""

import pytest

def test_a1():
    print("測試用例test_a1")
    assert 1 == 1

def test_a2():
    print("測試用例test_a2")

@pytest.mark.skip("跳過test_a3")
def test_a3():
    print("測試用例test_a3")
    assert 1 == 1

def test_a4():
    print("測試用例test_a4")
    assert 1 == 2

建立test_b.py檔案

指令碼程式碼:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公眾號:AllTests軟體測試
"""

def test_b1():
    print("測試用例test_b1")

def test_b2():
    print("測試用例test_b2")
    assert 1 == 2

開啟命令列,輸入執行命令

pytest -s

執行結果:

獲取到的測試用例執行狀態列印到控制檯。

示例二:setup 異常情況

修改test_b.py檔案

指令碼程式碼:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公眾號:AllTests軟體測試
"""
import pytest

@pytest.fixture(scope="function")
def my_setup():
    assert 1 == 2

def test_b1(my_setup):
    print("測試用例test_b1")

def test_b2():
    print("測試用例test_b2")
    assert 1 == 2

開啟命令列,輸入執行命令

pytest -s

執行結果:

setup報錯,結果標記為error。

示例三:teardown 異常情況

修改test_b.py檔案

指令碼程式碼:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公眾號:AllTests軟體測試
"""
import pytest

@pytest.fixture(scope="function")
def my_teardown():
    yield
    assert 1 == 2

def test_b1(my_teardown):
    print("測試用例test_b1")

def test_b2():
    print("測試用例test_b2")
    assert 1 == 2

開啟命令列,輸入執行命令

pytest -s

執行結果:

用例總數是6,但是測試報告狀態結果為2 failed, 3 passed, 1 skipped, 1 error,合計為7。

從獲取的terminalreporter.stats資訊可以看出:

passed裡when = 'call'時,統計一次test_b1用例

<TestReport 'test_b.py::test_b1' when='call' outcome='passed'>

error裡when = 'teardown'時,又統計一次test_b1用例

<TestReport 'test_b.py::test_b1' when='teardown' outcome='failed'>

所以報告結果合計為7。

但因為when='teardown'是測試用例的後置操作,一般用於資料的清理等操作,如報錯不影響測試用例的執行結果,所以在conftest.py檔案裡

pytest_terminal_summary函式獲取測試結果進行了忽略統計。

注:

when='teardown'是測試用例的後置操作,一般用於資料的清理等操作,如報錯不影響測試用例的執行結果,可以忽略。

但是如果想與測試報告結果保持一致(即報錯都要統計)

修改conftest.py檔案

指令碼程式碼:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公眾號:AllTests軟體測試
"""
import time

def pytest_terminal_summary(terminalreporter, exitstatus, config):
    """
    收集測試結果
    """
    print(terminalreporter.stats)
    print("total:", terminalreporter._numcollected)
    print('passed:', len(terminalreporter.stats.get('passed', [])))
    print('failed:', len(terminalreporter.stats.get('failed', [])))
    print('error:', len(terminalreporter.stats.get('error', [])))
    print('skipped:', len(terminalreporter.stats.get('skipped', [])))
    # terminalreporter._sessionstarttime 會話開始時間
    duration = time.time() - terminalreporter._sessionstarttime
    print('total times:', duration, 'seconds')

再次執行用例,執行結果:

抓取的測試結果與用例執行後的狀態結果一致。

本文來自部落格園,作者:AllTests軟體測試,轉載請註明原文連結:https://www.cnblogs.com/alltests/p/15434407.html