Pytest獲取測試用例執行結果儲存log檔案

2021-10-10 16:21:13 字數 1296 閱讀 7969

pytest 提供的pytest_runtest_makereport鉤子方法,獲取每個用例的執行結果。

from _pytest import runner

# 對應原始碼

def pytest_runtest_makereport(item, call):

""" return a :py:class:`_pytest.runner.testreport` object

for the given :py:class:`pytest.item` and

:py:class:`_pytest.runner.callinfo`.

"""這裡item是測試用例,call是測試步驟,具體過程如下:

先執行 when=』setup』 返回setup的執行結果。

然後執行 when=』call』 返回call的執行結果。

最後執行 when=』teardown』 返回teardown的執行結果。

conftest.pypytest_runtest_makereport內容,列印執行過程和執行結果。加個判斷:if report.when == 「call」 。

def pytest_runtest_makereport(item, call):

# 獲取鉤子方法的呼叫結果

out = yield

# print('用例執行結果', out)

# 3. 從鉤子方法的呼叫結果中獲取測試報告

report = out.get_result()

if report.when == "call":

print('測試報告:%s' % report)

print('步驟:%s' % report.when)

print('nodeid:%s' % report.nodeid)

print('description:%s' % str(item.function.__doc__))

print(('執行結果: %s' % report.outcome))

logs.info('nodeid:%s' % report.nodeid)

logs.info('執行結果: %s' % report.outcome)

PYTEST中對測試用例標記執行

標記三種方式 1 顯式指定函式名,通過 標記 pytest test.py test012 模糊查詢,使用 k標識 pytest k test test.py3 使用pytest.mark在函式上標記 建立pytest.ini檔案 注意縮排 pytest markers do do undo und...

Pytest 測試用例的執行方式(2)

在pytest框架中,編寫測試用例有如下約束 在滿足上面約束後,有兩種方式來執行測試用例 主函式方式 命令列方式 使用pytest.main 方式執行用例,如果不加引數,會自動從當前目錄及子目錄中尋找符合上述約束的測試用例來執行。import pytest class testpractice de...

pytest實現測試用例引數化

本文總www.cppcns.com結pytest的測試用例引數化。軟體測試中,輸入相應值,檢查期望值,是常見測試方法。在自動化測試中,乙個測試用例對應乙個測試點,通常一組測試資料無法完全覆蓋測試範圍,所以,需要引數化來傳遞多組資料。pytest的測試用例引數化使用如下裝飾器即可完成。pytest.m...