一、生成html測(cè)試報(bào)告
pytest xxx.py --html=./report/report.html --self-contained-html 生成的html報(bào)告合并了css樣式

html測(cè)試報(bào)告
二、生成Allure測(cè)試報(bào)告
1、Allure下載,可以下載最新版的:https://github.com/allure-framework/allure2/releases/tag/2.7.0。將下載的文件,解壓
2、配置環(huán)境變量:進(jìn)入解壓的文件目錄D:\soft\allure-2.7.0\bin,添加到path環(huán)境變量中。查詢是否安裝成功:cmd→allure
3、安裝:pip install allure-pytest,pip install pytest-html,pip install pyyaml
4、執(zhí)行:
a)pytest xx.py -s -q --alluredir 指定report路徑;默認(rèn)當(dāng)前路徑 :pytest xx.py -s -q --alluredir report。生成報(bào)告路徑后,以后就不用再執(zhí)行這條命令,直接執(zhí)行用例,再執(zhí)行生成測(cè)試報(bào)告就ok
b)allure generate report/ -o report/html這樣就生成了測(cè)試報(bào)告,去目錄中打開吧

image.png

image.png
5、由于不斷的執(zhí)行,會(huì)先生成更多的json文件,會(huì)導(dǎo)致文件冗余,所以執(zhí)行用例的時(shí)候需要加上 --clean-alluredir:
if __name__ == '__main__':
# 由于每次執(zhí)行的時(shí)候都要先生成json文件,會(huì)導(dǎo)致json文件冗余,加上--clean-alluredir解決該問(wèn)題
pytest.main(["-s", '-q', "test_add_user.py", '--alluredir', '../../report', '--clean-alluredir'])
# 切換至根目錄
os.chdir(os.path.dirname(os.path.dirname(os.getcwd())))
# 將json文件轉(zhuǎn)換成html格式的測(cè)試報(bào)告
os.system('allure generate report/ -o report/html --clean')
補(bǔ)充:
- 首先生成json文件
pytest.ini
[pytest]
addopts = -vs -m "smoke" --tb=line --alluredir=./temps --clean-alluredir
python_files = test_*.py
testpaths = ../pytest_learn
markers =
smoke: this is a smoke tag
demo: this is a demo
- 然后在main函數(shù)里,調(diào)用系統(tǒng)命令,生成報(bào)告
if __name__ == '__main__':
pytest.main()
time.sleep(3)
# 生成json文件后,通過(guò)下面系統(tǒng)命令,生成可視化的報(bào)告
os.system('allure generate ./temp -o ./reports --clean')
os.system("allure open -h 127.0.0.1 -p 8083 ../result/report/html")
-
./temp:是尋找生成臨時(shí)的json文件目錄,注意目錄名根據(jù)實(shí)際情況來(lái) -
./reports:是reports目錄
PS:覺得這篇文章有用的朋友,多多點(diǎn)贊打賞哦~!