1. 程式人生 > 其它 >Python測試框架pytest(09)Hooks函式 - pytest_runtest_makereport獲取用例執行結果

Python測試框架pytest(09)Hooks函式 - pytest_runtest_makereport獲取用例執行結果

鉤子方法 pytest_runtest_makereport 可以清晰的瞭解用例的執行過程,並獲取到每個用例的執行結果。

鉤子方法 pytest_runtest_makereport 原始碼:

按照執行順序,具體過程如下:

1、先判斷,當 report.when == 'setup' 時,返回執行結果。

2、然後判斷,當 report.when == 'call' 時,返回執行結果。

3、最後判斷,當 report.when == 'teardown' 時,返回執行結果。

示例一:conftest.py 新增 pytest_runtest_makereport

新建conftest.py檔案

寫pytest_runtest_makereport內容,列印執行過程與執行結果。

指令碼程式碼:

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

import pytest

@pytest.hookimpl(hookwrapper=True, tryfirst=True)
def pytest_runtest_makereport(item, call):
    print('======')
    # 獲取鉤子方法的呼叫結果
    runResult = yield
    print
('用例執行結果:', runResult) # 從鉤子方法的呼叫結果中獲取測試報告 report = runResult.get_result() print('測試報告:%s' % report) print('測試步驟:%s' % report.when) print('nodeid:%s' % report.nodeid) print('描述說明:%s' % str(item.function.__doc__)) print('執行結果:%s' % report.outcome)

建立test_case.py檔案

指令碼程式碼:

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

def test_abc():
    """
    測試用例:test_abc
    """
    print("AllTests軟體測試")

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

pytest -s

執行結果:

執行用例經歷了三個階段:setup->call->teardown,並列印了返回的物件與物件屬性。

示例二:給用例新增前置(setup)和後置(teardown)操作

修改conftest.py檔案

指令碼程式碼:

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

@pytest.hookimpl(hookwrapper=True, tryfirst=True)
def pytest_runtest_makereport(item, call):
    print('======')
    # 獲取鉤子方法的呼叫結果
    runResult = yield
    print('用例執行結果:', runResult)
    # 從鉤子方法的呼叫結果中獲取測試報告
    report = runResult.get_result()
    print('測試報告:%s' % report)
    print('測試步驟:%s' % report.when)
    print('nodeid:%s' % report.nodeid)
    print('描述說明:%s' % str(item.function.__doc__))
    print('執行結果:%s' % report.outcome)

@pytest.fixture(scope="session", autouse=True)
def fixture_setup_teardown():
    print("前置操作:setup")
    yield
    print("後置操作:teardown")

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

pytest -s

執行結果:

示例三:setup 失敗

修改conftest.py檔案

指令碼程式碼:

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

@pytest.hookimpl(hookwrapper=True, tryfirst=True)
def pytest_runtest_makereport(item, call):
    print('======')
    # 獲取鉤子方法的呼叫結果
    runResult = yield
    print('用例執行結果:', runResult)
    # 從鉤子方法的呼叫結果中獲取測試報告
    report = runResult.get_result()
    print('測試報告:%s' % report)
    print('測試步驟:%s' % report.when)
    print('nodeid:%s' % report.nodeid)
    print('描述說明:%s' % str(item.function.__doc__))
    print('執行結果:%s' % report.outcome)

@pytest.fixture(scope="session", autouse=True)
def fixture_setup_teardown():
    print("前置操作:setup")
    assert 1 == 2
    yield
    print("後置操作:teardown")

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

pytest -s

執行結果:

當setup執行失敗時,setup執行結果為failed;後面的用例和teardown都不會執行。

用例的狀態為error。

示例四:teardown 失敗

修改conftest.py檔案

指令碼程式碼:

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

@pytest.hookimpl(hookwrapper=True, tryfirst=True)
def pytest_runtest_makereport(item, call):
    print('======')
    # 獲取鉤子方法的呼叫結果
    runResult = yield
    print('用例執行結果:', runResult)
    # 從鉤子方法的呼叫結果中獲取測試報告
    report = runResult.get_result()
    print('測試報告:%s' % report)
    print('測試步驟:%s' % report.when)
    print('nodeid:%s' % report.nodeid)
    print('描述說明:%s' % str(item.function.__doc__))
    print('執行結果:%s' % report.outcome)

@pytest.fixture(scope="session", autouse=True)
def fixture_setup_teardown():
    print("前置操作:setup")
    yield
    print("後置操作:teardown")
    assert 1 == 2

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

pytest -s

執行結果:

用例的狀態為1個passed、1個error(teardown執行結果為failed)。

示例五:call 失敗

將conftest.py檔案改為示例二的正常程式碼

修改test_case.py檔案

指令碼程式碼:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公眾號:AllTests軟體測試
"""
def test_abc():
    """
    測試用例:test_abc
    """
    print("AllTests軟體測試")
    assert 1 == 2

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

pytest -s

執行結果:

call執行結果為failed,用例狀態為failed。

示例六:只獲取 call 的結果

根據示例二的conftest.py檔案,pytest_runtest_makereport鉤子方法執行了三次(setup、call、teardown),如只執行call,則新增條件判斷if report.when == "call":即可。

修改conftest.py檔案

指令碼程式碼:

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

@pytest.hookimpl(hookwrapper=True, tryfirst=True)
def pytest_runtest_makereport(item, call):
    print('======')
    # 獲取鉤子方法的呼叫結果
    runResult = yield
    print('用例執行結果:', runResult)
    # 從鉤子方法的呼叫結果中獲取測試報告
    report = runResult.get_result()
    if report.when == "call":
        print('測試報告:%s' % report)
        print('測試步驟:%s' % report.when)
        print('nodeid:%s' % report.nodeid)
        print('描述說明:%s' % str(item.function.__doc__))
        print('執行結果:%s' % report.outcome)

@pytest.fixture(scope="session", autouse=True)
def fixture_setup_teardown():
    print("前置操作:setup")
    yield
    print("後置操作:teardown")

並將test_case.py檔案改為示例一的正常程式碼

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

pytest -s

執行結果:

只獲取call的結果資訊

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