欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Pytest單元測試框架生成HTML測試報告及優(yōu)化的步驟

 更新時間:2022年01月28日 16:27:06   作者:Edisonyao  
本文主要介紹了Pytest單元測試框架生成HTML測試報告及優(yōu)化的步驟,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

一、安裝插件

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

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

IDE中安裝

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

命令行安裝

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

pip install -U pytest-html

二、生成html報告

先準備一個簡單的執(zhí)行腳本

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=報告名稱 要執(zhí)行的腳本文件 ,執(zhí)行上述腳本查看結(jié)果。

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

Environment:測試環(huán)境

Summary:用例統(tǒng)計

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

三、使用小技巧

指定路徑

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

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

可以發(fā)現(xiàn)項目根目錄下生成了outputs文件,測試報告也在其中。

報告獨立

  當(dāng)本地執(zhí)行完成,想把測試報告分享出去,卻發(fā)現(xiàn)分享出去的報告打開后樣式丟失。因為代碼執(zhí)行完成

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

對外分享了。

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

四、報告優(yōu)化

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

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

定的不可更改。

導(dǎo)入引用包

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

修改測試環(huán)境

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

修改用例統(tǒng)計

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

修改結(jié)果顯示

@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)) # 表頭對應(yīng)的內(nèi)容
    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")

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

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

到此這篇關(guān)于Pytest單元測試框架生成HTML測試報告及優(yōu)化的步驟的文章就介紹到這了,更多相關(guān)Pytest生成HTML測試報告內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論