日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Pytest單元測試框架生成HTML測試報告及優化的步驟_python

作者:Edisonyao ? 更新時間: 2022-04-10 編程語言

一、安裝插件

  要生成html類型的報告,需要使用pytest-html插件,可以在IDE中安裝,也可以在命令行中安裝。插件安裝

的位置涉及到不同項目的使用,這里不再詳述,想了解的可自行查詢。

IDE中安裝

  在File>Settings>Project>Project Interpreter界面,點擊“ + ”搜索pytest-html即可進行安裝。

命令行安裝

  建議先在命令行中切換到python安裝路徑“ Lib\site-packages ”目錄,再執行安裝命令。

pip install -U pytest-html

二、生成html報告

先準備一個簡單的執行腳本

import pytest


def fun(x):
    return x + 1

def test_answer_1():
    """測試斷言一"""
    assert fun(3) == 4
def test_answer_2():
    """測試斷言二"""
    assert fun(5) == 7

@pytest.mark.parametrize("test_input,expected",[
    ("3+5",8),
    ("2+4",6),
    pytest.param("6 * 9",42,marks=pytest.mark.xfail),
    pytest.param("6 * 6",42,marks=pytest.mark.skip)
])

def test_mark(test_input,expected):
    """用例集合"""
    assert eval(test_input) == expected

if __name__ == '__main__':
    pytest.main(['-v','--html=report.html','test_08.py'])

生成報告命令pytest --html=報告名稱 要執行的腳本文件 ,執行上述腳本查看結果。

report.html:報告名稱,記錄報告生成時間以及插件版本

Environment:測試環境

Summary:用例統計

Results:測試結果,點擊Show all details / Hide all details可以展開結果詳情或收縮全部結果

三、使用小技巧

指定路徑

  通過上述命令運行腳本后可以發現,測試報告保存在項目的根目錄下,查找報告比較繁瑣。我們可以

在運行命令中指定報告路徑pytest -v --html=./outputs/report.html test_08.py,代碼執行完成,

可以發現項目根目錄下生成了outputs文件,測試報告也在其中。

報告獨立

  當本地執行完成,想把測試報告分享出去,卻發現分享出去的報告打開后樣式丟失。因為代碼執行完成

會生成assets文件,將CSS保存在了本地。我們可以通過命令將CSS寫入HTML中,這樣生成的測試報告就能

對外分享了。

pytest -v --html=./outputs/report.html --self-contained-html test_08.py

四、報告優化

  在實際的工作中,通過上述操作生成的測試報告一般不是我們想要的結果。環境信息通過增減更換成需

要展示的內容、增加用例描述、去掉多余的列等等。這里需要將優化代碼寫入conftest.py文件,該文件名是固

定的不可更改。

導入引用包

import pytest
from py._xmlgen import html
from datetime import datetime

修改測試環境

@pytest.mark.parametrize
def pytest_configure(config):
    config._metadata.pop("JAVA_HOME") # 刪除java_home
    config._metadata["項目名稱"] = "引擎自動化" # 添加項目名稱
    config._metadata["接口地址"] = "https://www.example.com/poke" # 添加接口地址

修改用例統計

@pytest.mark.parametrize
def pytest_html_results_summary(prefix,summary,postfix):
    prefix.extend([html.p("所屬部門:測試組")])
    prefix.extend([html.p("測試人員:許衛玲")])

修改結果顯示

@pytest.mark.optionalhook
def pytest_html_results_table_header(cells):
    cells.insert(1,html.th("Description")) # 表頭添加Description
    cells.insert(2,html.th("Time",class_="sortable time",col="time"))
    cells.pop(-1) # 刪除link

@pytest.mark.optionalhook
def pytest_html_results_table_row(report,cells):
    cells.insert(1,html.td(report.description)) # 表頭對應的內容
    cells.insert(2,html.td(datetime.now(),class_="col-time"))
    cells.pop(-1) # 刪除link

@pytest.mark.hookwrapper
def pytest_runtest_makereport(item,call): # Description取值為用例說明__doc__
    outcome = yield
    report = outcome.get_result()
    report.description = str(item.function.__doc__)
    report.nodeid = report.nodeid.encode("utf-8").decode("unicode_escape")

修改完成,重新執行腳本,查看最終效果。

作者:Sweettesting —— 半醉半醒半浮生
出處:http://www.cnblogs.com/Sweettesting/

原文鏈接:https://www.cnblogs.com/Sweettesting/p/15843803.html

欄目分類
最近更新