(1)pytest-html
插件
Pytest可以通過命令??式晒杈,?成xml/html
格式的測(cè)試報(bào)告嫂伞,并存儲(chǔ)于?戶指定路徑。
需要用到pytest-html
插件拯钻。
安裝?式:pip install pytest-html
插件使用方式:
命令格式:--html=?戶路徑/report.html
運(yùn)??式:
-
main()函數(shù)方式:
pytest.main(['--html=./report/report_01.html'])
(不好使帖努,可能配置了pytest.ini
文件) -
命令行方式:
在report目錄中生成
report.html
測(cè)試報(bào)告。pytest ./pytest_demo/test_pytest_01.py --html=./report/report.html
-
使用
pytest.ini
文件方式:在
addopts
屬性后追加--html
參數(shù)配置粪般,在report目錄中生成report.html
測(cè)試報(bào)告拼余。addopts = -s --html=../report/report.html
執(zhí)?結(jié)果:
在指定?錄中會(huì)?成assets
?件夾(css文件)和report.html
?件
提示:若要?成xml?件,可將
--html=./report.html
改成--junitxml= report/report.xml
(2)Allure測(cè)試報(bào)告
1)Allure框架說明:
Allure生成的測(cè)試報(bào)告與上面pytest-html
插件生成的測(cè)試報(bào)告對(duì)比亩歹,簡(jiǎn)直完美匙监!
Allure是一個(gè)Report框架,是一種靈活的輕量級(jí)捆憎,支持多語言的測(cè)試報(bào)告工具舅柜,它不僅能夠以簡(jiǎn)潔的web報(bào)告形式顯示已測(cè)試的內(nèi)容,并帶有失敗用例截圖躲惰、測(cè)試步驟和測(cè)試說明信息致份,也可以集成到Jenkins上展示高大上的報(bào)告界面。
而且允許參與開發(fā)過程的每個(gè)人從測(cè)試的日常執(zhí)行中提取最大限度的有用信息础拨。
Allure框架支持的語言包括:
- Java
- Python
- JavaScript
- Ruby
- Groovy
- PHP
- .Net
- Scala
Allure幫助文檔
2)Allure框架的使用
步驟1:下載Allure框架氮块,并配置到環(huán)境變量中
Allure框架下載地址:https://github.com/allure-framework/allure2/releases
然后解壓Allure框架文件绍载,放到自己指定的目錄中。
把Allure框架的bin目錄配置到Path環(huán)境變量中滔蝉。
步驟2:驗(yàn)證Allure框架是否安裝成功
使用命令:allure --version
需要在CMD命令行和PyCharm的Terminal中击儡,都需要驗(yàn)證一下。
因?yàn)镃MD可以驗(yàn)證通過蝠引,但是PyCharm中驗(yàn)證失敗阳谍,如下:
J:\PyCharmWorkSpace\Pytest_d>allure --version
'allure' 不是內(nèi)部或外部命令,也不是可運(yùn)行的程序
或批處理文件螃概。
解決:需要重啟PyCharm矫夯。
步驟3:下載allure-pytest
庫(插件)
執(zhí)行安裝命令:pip install allure-pytest
步驟4:設(shè)置生成的Json格式臨時(shí)報(bào)告的存放位置。
配置pytest.ini
文件吊洼,在pytest.ini
全局配置文件中的addopts
屬性中添加:
--alluredir ../report/temp_jsonreport
例如:addopts = -vs --alluredir ../report/temp_jsonreport
然后我們執(zhí)行測(cè)試用例就可以了训貌,當(dāng)然--alluredir
參數(shù)也可以不配置在pytest.ini
文件,比如在執(zhí)行測(cè)試的命令行或者mian()函數(shù)中填寫都可以冒窍。(主要是生成Json格式的測(cè)試報(bào)告递沪,是多個(gè)json文件)
提示:
- 命令?參數(shù):
pytest --alluredir report
,是在執(zhí)?命令?錄?成report?件夾综液,?件夾下包含xml?件款慨。- 將
pytest.ini
文件中的生成報(bào)告的命令替換成--alluredir report
,在命令行中運(yùn)行pytest即可生成報(bào)告格式為json格式意乓,保存在項(xiàng)目文件的report文件夾中樱调。
步驟5:生成Allure測(cè)試報(bào)告
原理是:使用第一步下載的Allure框架把Json格式的測(cè)試報(bào)告,轉(zhuǎn)換成精美的HTML測(cè)試報(bào)告届良。
將上面/report/temp_jsonreport
文件夾中的json格式的測(cè)試報(bào)告轉(zhuǎn)化為HTML格式的測(cè)試報(bào)告笆凌。
執(zhí)行命令:allure generate ./report/temp_jsonreport -o ./report/html --clean
注意:以執(zhí)行命令的目錄為相對(duì)路徑。
說明:
-
allure generate
: 固定命令士葫。 -
./report/temp_jsonreport
:生成的Json格式的臨時(shí)報(bào)告的路徑乞而。 -
-o
:輸出output。 -
./report/html
:生成的Allure報(bào)告的路徑慢显。 -
--clean
:清空./report/html
路徑中原來的Allure測(cè)試報(bào)告爪模。
提示:main()函數(shù)中執(zhí)行如上命令
if __name__ == '__main__':
pytest.main()
os.system("allure generate ./report/temp_jsonreport -o ./report/html --clean")
# 或者直接用main函數(shù)調(diào)用,哪種方式都可以荚藻。
# (直接執(zhí)行測(cè)試文件屋灌, 而不用pytest的方式執(zhí)行,就可以執(zhí)行)
pytest.main(["testCase_demo1.py","-sv","--alluredir","../report/temp_jsonreport"])
os.system("allure generate ./report/temp_jsonreport -o ./report/html --clean")
說明:找不到路徑的話应狱,可以在Python Console
窗口調(diào)試共郭。
最后:生成的Allure測(cè)試報(bào)告如下圖:
提示:Allure測(cè)試報(bào)告支持自定義修改。