1. 程式人生 > 其它 >Python測試框架pytest(12)Hooks函式 - 其他Hooks函式

Python測試框架pytest(12)Hooks函式 - 其他Hooks函式

1、pytest_report_teststatus自定義測試結果

pytest_report_teststatus(report, config) 鉤子函式返回結果類別,狀態報告的短字母和詳細單詞。

結果類別是對結果進行計數的類別,例如 "passed"、"skipped"、"error" 或空字串。

在測試過程中會顯示短字母,例如 "."、"s"、"E" 或空字串。

在詳細模式下,隨著測試的進行,將顯示詳細單詞,例如"PASSED"、"SKIPPED"、"ERROR"或空字串。

引數:

  • report -- 要返回其狀態的報表物件。

  • config(_pytest.config.Config) -- pytest 配置物件。

建立test_case.py檔案

指令碼程式碼:

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

import pytest

@pytest.fixture()
def my_setup():
    print("前置操作")
    assert 0 == 1

@pytest.fixture()
def my_teardown():
    yield
    print("後置操作")
    assert 0 == 1

def test_case1(my_setup):
    assert
1 == 1 def test_case2(): assert 0 == 1 def test_case3(): assert 1 == 1 def test_case4(): assert 1 == 0 def test_case5(my_teardown): assert 1 == 1

命令列執行命令:

pytest test_case.py

執行結果:

命令列執行pytest用例,預設情況下.代表通過的用例,F代表失敗的用例,E代表異常的用例。

如果想自定義測試結果,就可以使用pytest_report_teststatus鉤子函式,將函式寫在conftest.py檔案裡。

建立conftest.py檔案

將測試結果.自定義為√,F自定義為x,setup的error自定義為0,teardown的error自定義為1,跳過skipped自定義為/

指令碼程式碼:

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

def pytest_report_teststatus(report, config):
    if report.when == 'call' and report.passed:
        return (report.outcome, '', 'passed')
    if report.when == 'call' and report.failed:
        return (report.outcome, 'x', 'failed')
    if report.when == 'setup' and report.failed:
        return (report.outcome, '0', 'error')
    if report.when == 'teardown' and report.failed:
        return (report.outcome, '1', 'error')
    if report.skipped:
        return (report.outcome, '/', 'skipped')

命令列再次執行命令:

pytest test_case.py

執行結果:

按照自定義結果顯示在控制檯裡。

2、pytest_generate_tests引數化生成測試用例

pytest_generate_tests 在測試用例引數化收集前呼叫此鉤子函式,並根據測試配置或定義測試函式的類或模組中指定的引數值生成測試用例。

1、建立conftest.py檔案

自定義引數化的鉤子, 判斷當測試用例傳param引數時,生成引數化的用例。

通過傳入的metafunc物件,檢查請求的測試上下文,並可以呼叫metafunc.parametrize()方法進行引數化。

指令碼程式碼:

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

def pytest_generate_tests(metafunc):
    if "param" in metafunc.fixturenames:
        metafunc.parametrize("param", metafunc.module.case_data, ids=metafunc.module.case_id, scope="function")

2、建立test_case.py檔案

case_id為用例ID,case_data為用例資料

指令碼程式碼:

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

case_id = ["001", "002"]

case_data = [{"url": "https://www.baidu.com/"},
             {"url": "https://www.cnblogs.com/alltests/"}]

def test_url(param):
    print("\n引數:" + str(param))

3、開啟命令列,輸入執行命令:

pytest -s -v test_case.py

執行結果:

3、更多Hooks函式

Hooks鉤子函式總共有6大類:

  • Bootstrapping hooks - 引導鉤子,呼叫足夠早註冊的外掛(內部和 setuptools 外掛)。

  • Initialization hooks - 初始化鉤子,呼叫外掛和 conftest.py 檔案的初始化鉤子。

  • Collection hooks - 集合鉤子,pytest 呼叫集合鉤子來收集檔案和目錄。

  • Test running (runtest) hooks - 測試執行 (runtest) 鉤子,所有與 runtest 相關的鉤子都接收一個 pytest.Item 物件。

  • Reporting hooks - 報告鉤子。

  • Debugging/Interaction hooks - 除錯/互動掛鉤,很少有鉤子可用於特殊報告或與異常的互動。

關於Hooks鉤子函式的詳細使用,可檢視官方文件:

https://docs.pytest.org/en/latest/reference/reference.html#hooks


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