偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

使用Pytest的Reporting特性來生成報告

開發(fā) 測試
你知道如何使用pytest的 reporting特性來生成報告嗎?一起來了解一下吧。

特性

1. HTML 報告:使用 pytest-html 插件,你可以生成 HTML 格式的測試報告。只需在項目的 pytest.ini 文件中添加以下內(nèi)容:

[pytest]
addopts = --html=report.html

然后,在運(yùn)行 pytest 時,將會生成一個名為 report.html 的 HTML 文件,其中包含了測試用例的詳細(xì)信息和結(jié)果。

2. XML 報告:使用 pytest-xml 插件,你可以生成 XML 格式的測試報告。同樣,在項目的 pytest.ini 文件中添加以下內(nèi)容:

[pytest]
addopts = --xml=report.xml

運(yùn)行 pytest 后,將會生成一個名為 report.xml 的 XML 文件,可供其他工具或系統(tǒng)使用。

3. JSON 報告:使用 pytest-json 插件,你可以生成 JSON 格式的測試報告。在 pytest.ini 文件中添加以下內(nèi)容:

[pytest]
addopts = --jsnotallow=report.json

運(yùn)行 pytest 后,將會生成一個名為 report.json 的 JSON 文件,包含了測試用例的相關(guān)信息。

4. 控制臺報告:默認(rèn)情況下,pytest 在控制臺輸出測試結(jié)果。你可以通過設(shè)置 pytest.ini 文件中的 verbosity 選項來控制報告的詳細(xì)程度,例如:

[pytest]
verbosity = 2

這些 reporting 特性可以幫助你更好地了解測試的執(zhí)行情況,并與其他團(tuán)隊成員或工具進(jìn)行共享和分析。

如何在報告中添加自定義字段?

要在 pytest 的報告中添加自定義字段,你可以使用 pytest-html 插件來生成 HTML 格式的報告,并在報告中添加自定義字段。以下是一個示例,展示了如何在 HTML 報告中添加自定義字段 Environment 和 Execution Time:

import datetime
from py.xml import html
import pytest
import time
# 修改報告名稱
def pytest_html_report_title(report):
    report.title = "接口自動化測試報告"
# 添加環(huán)境項
def pytest_configure(config):
    config._metadata('測試人員') = 'emily'
# 添加執(zhí)行時間
def pytest_html_results_table_header(cells):
    cells.insert(0, html.th('用例編號'))
    cells.insert(1, html.th('所屬模塊'))
    cells.insert(2, html.th('用例名稱'))
    cells.insert(3, html.th('接口路徑'))
    cells.insert(5, html.th('執(zhí)行時間', class_='sortable time', col='time'))
    cells.pop(6)
    cells.pop()
# 獲取測試節(jié)點
def pytest_html_results_table_row(report, cells):
    url = 'http://xxx.com'
    testnode = report.nodeid.encode("utf-8").decode("unicode_escape")
    caseid = testnode.split('-')(3)
    cells.insert(0, html.td(caseid))
    module = testnode.split('-')(2)
    cells.insert(1, html.td(module))
    casename = testnode.split('-')(1)
    url = url+testnode.split('-')(4)(:-1)
    cells.insert(2, html.td(casename))
    cells.insert(3, html.td(url))
    cells.insert(5, html.td(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), class_='col-time'))
    cells.pop(6)
    cells.pop()
# 在運(yùn)行測試之前執(zhí)行的鉤子函數(shù)
@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport(item, call):
    outcome = yield
    report = outcome.get_result()
    report.casename = str(item.function.__code__.co_varnames)

首先,在 pytest_configure 函數(shù)中,使用 config._metadata 來添加一個名為 測試人員 的自定義字段,其值為 emily。接下來,在 pytest_html_results_table_header 函數(shù)中,向 HTML 報告的表頭中添加了一個名為 執(zhí)行時間 的新列。然后,在 pytest_html_results_table_row 函數(shù)中,從測試用例中獲取相關(guān)信息,并將其插入到報告的行數(shù)據(jù)中。最后,使用 pytest_runtest_makereport 鉤子函數(shù)來修改測試用例的名稱,使其包含函數(shù)的參數(shù)名。

運(yùn)行 pytest 命令后,將會生成一個名為 report.html 的 HTML 文件,其中包含了測試用例的詳細(xì)信息和結(jié)果,并且包含了自定義字段 Environment 和 Execution Time。

如何在報告中添加圖表?

可以使用一些第三方庫或工具來實現(xiàn)。以下是一種常見的方法,使用 Python 的 Matplotlib 庫來生成圖表并將其嵌入到 HTML 報告中:

1. 首先,確保你已經(jīng)安裝了 Matplotlib 庫。

2. 在你的測試用例中,使用 Matplotlib 繪制圖表,并將其保存為圖像文件(例如 PNG 格式)。

3. 在 HTML 報告中,使用 HTML 和 CSS 來嵌入和顯示圖像。你可以在報告的適當(dāng)位置添加  標(biāo)簽,并指定圖像的路徑。

以下是一個簡單的示例,展示了如何在 HTML 報告中添加圖表:

import pytest
import matplotlib.pyplot as plt
def test_sample_function():
    # 生成圖表數(shù)據(jù)
    x = [1, 2, 3, 4, 5]
    y = [2, 4, 6, 8, 10]
    # 繪制圖表
    plt.plot(x, y)
    plt.xlabel('X 軸')
    plt.ylabel('Y 軸')
    plt.title('圖表示例')
    plt.savefig('chart.png')
    # 斷言圖表文件存在
    assert os.path.exists('chart.png')
@pytest.mark.parametrize('param', [1, 2, 3])
def test_with_params(param):
    # 在這里使用參數(shù)進(jìn)行測試
# 修改報告名稱
def pytest_html_report_title(report):
    report.title = "測試報告"
# 添加圖表到報告中
def pytest_html_results_table_header(cells):
    cells.insert(0, html.th('用例編號'))
    cells.insert(1, html.th('所屬模塊'))
    cells.insert(2, html.th('用例名稱'))
    cells.insert(3, html.th('圖表', class_='sortable chart', col='chart'))
# 獲取測試節(jié)點
def pytest_html_results_table_row(report, cells):
    url = 'http://xxx.com'
    testnode = report.nodeid.encode("utf-8").decode("unicode_escape")
    caseid = testnode.split('-')(3)
    cells.insert(0, html.td(caseid))
    module = testnode.split('-')(2)
    cells.insert(1, html.td(module))
    casename = testnode.split('-')(1)
    cells.insert(2, html.td(casename))
    cells.insert(3, html.td(html.Img(src='chart.png')))
    cells.pop(4)
    cells.pop()
# 在運(yùn)行測試之前執(zhí)行的鉤子函數(shù)
@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport(item, call):
    outcome = yield
    report = outcome.get_result()
    report.casename = str(item.function.__code__.co_varnames)

在上述示例中,首先在測試用例中生成圖表并保存為 chart.png。然后,在 HTML 報告的表頭中添加了一個名為 圖表 的新列。在報告的行數(shù)據(jù)中,使用  標(biāo)簽嵌入了圖表圖像。

責(zé)任編輯:華軒 來源: 測試開發(fā)學(xué)習(xí)交流
相關(guān)推薦

2009-02-04 08:52:55

動態(tài)頁面XMLXSL

2009-12-10 16:46:06

PHP error_r

2009-07-27 10:35:20

2020-11-18 08:13:45

瀏覽器Reporting A

2022-07-18 14:33:05

PythonPDF報告

2010-11-26 13:55:38

Reporting S

2021-04-06 07:55:55

VitepressVue3工具

2021-04-09 21:18:53

DockerSSHWSL 2

2024-10-12 17:08:41

2022-08-31 12:57:58

PythonTemplate文件報告

2010-07-19 09:50:58

SQL Server2

2020-11-20 07:54:22

Java 特性語句

2012-02-23 10:28:12

MySQL

2022-05-29 17:12:18

元宇宙

2019-10-14 09:14:37

Linuxbash命令

2021-04-15 09:03:33

框架 Pytest測試

2025-06-26 17:08:43

2019-08-29 22:47:32

單庫id分庫

2024-05-16 11:45:19

Rust項目代碼

2009-09-18 09:23:21

ASP.NET 4自動啟動
點贊
收藏

51CTO技術(shù)棧公眾號