使用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 報告的表頭中添加了一個名為 圖表 的新列。在報告的行數據中,使用 標簽嵌入了圖表圖像。