成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

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

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

特性

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

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

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

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

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

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

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

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

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

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

[pytest]
verbosity = 2

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

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

要在 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 = "接口自動化測試報告"
# 添加環境項
def pytest_configure(config):
    config._metadata('測試人員') = 'emily'
# 添加執行時間
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('執行時間', class_='sortable time', col='time'))
    cells.pop(6)
    cells.pop()
# 獲取測試節點
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()
# 在運行測試之前執行的鉤子函數
@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 函數中,使用 config._metadata 來添加一個名為 測試人員 的自定義字段,其值為 emily。接下來,在 pytest_html_results_table_header 函數中,向 HTML 報告的表頭中添加了一個名為 執行時間 的新列。然后,在 pytest_html_results_table_row 函數中,從測試用例中獲取相關信息,并將其插入到報告的行數據中。最后,使用 pytest_runtest_makereport 鉤子函數來修改測試用例的名稱,使其包含函數的參數名。

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

如何在報告中添加圖表?

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

1. 首先,確保你已經安裝了 Matplotlib 庫。

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

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

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

import pytest
import matplotlib.pyplot as plt
def test_sample_function():
    # 生成圖表數據
    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):
    # 在這里使用參數進行測試
# 修改報告名稱
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'))
# 獲取測試節點
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()
# 在運行測試之前執行的鉤子函數
@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 報告的表頭中添加了一個名為 圖表 的新列。在報告的行數據中,使用  標簽嵌入了圖表圖像。

責任編輯:華軒 來源: 測試開發學習交流
相關推薦

2009-02-04 08:52:55

動態頁面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工具

2022-08-31 12:57:58

PythonTemplate文件報告

2010-07-19 09:50:58

SQL Server2

2024-10-12 17:08:41

2021-04-09 21:18:53

DockerSSHWSL 2

2022-05-29 17:12:18

元宇宙

2019-10-14 09:14:37

Linuxbash命令

2012-02-23 10:28:12

MySQL

2020-11-20 07:54:22

Java 特性語句

2021-04-15 09:03:33

框架 Pytest測試

2024-05-16 11:45:19

Rust項目代碼

2019-08-29 22:47:32

單庫id分庫

2009-12-17 15:05:09

Linux發行版

2009-03-20 17:14:57

點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 97综合在线 | 一区二区三区免费在线观看 | 久久91精品国产 | 久久中文字幕一区 | 999视频在线播放 | 99久久久久久99国产精品免 | 综合五月婷 | 欧美国产日本一区 | 欧美日韩精品在线免费观看 | 日韩精品一 | h片在线免费看 | 欧美一级黄色片免费观看 | 成人三级影院 | 欧美夜夜 | 久久久www成人免费无遮挡大片 | 久草久草久草 | 黄色电影在线免费观看 | 一区二区精品 | 久久久久久蜜桃一区二区 | 日韩在线视频一区 | 一区二区三区在线 | 青青艹在线视频 | 国产精品久久一区二区三区 | 亚洲一区二区精品 | 亚洲欧美日韩在线不卡 | 中文字幕 在线观看 | 91亚洲视频在线 | 久久综合香蕉 | 成人亚洲在线 | 91亚洲精华国产 | 亚洲精品9999| 伊人中文网 | 久久久久久中文字幕 | 久久成人午夜 | 国产成人99久久亚洲综合精品 | 久久久高清 | 亚洲经典一区 | 91久久久久久久久久久久久 | 免费在线观看成人 | 色橹橹欧美在线观看视频高清 | 久久久久久91香蕉国产 |