一、生成html測試報告
pytest xxx.py --html=./report/report.html --self-contained-html 生成的html報告合并了css樣式

html測試報告
二、生成Allure測試報告
1、Allure下載,可以下載最新版的:https://github.com/allure-framework/allure2/releases/tag/2.7.0。將下載的文件,解壓
2、配置環(huá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路徑;默認當(dāng)前路徑 :pytest xx.py -s -q --alluredir report。生成報告路徑后,以后就不用再執(zhí)行這條命令,直接執(zhí)行用例,再執(zhí)行生成測試報告就ok
b)allure generate report/ -o report/html這樣就生成了測試報告,去目錄中打開吧

image.png

image.png
5、由于不斷的執(zhí)行,會先生成更多的json文件,會導(dǎo)致文件冗余,所以執(zhí)行用例的時候需要加上 --clean-alluredir:
if __name__ == '__main__':
# 由于每次執(zhí)行的時候都要先生成json文件,會導(dǎo)致json文件冗余,加上--clean-alluredir解決該問題
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格式的測試報告
os.system('allure generate report/ -o report/html --clean')
補充:
- 首先生成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)命令,生成報告
if __name__ == '__main__':
pytest.main()
time.sleep(3)
# 生成json文件后,通過下面系統(tǒng)命令,生成可視化的報告
os.system('allure generate ./temp -o ./reports --clean')
os.system("allure open -h 127.0.0.1 -p 8083 ../result/report/html")
-
./temp:是尋找生成臨時的json文件目錄,注意目錄名根據(jù)實際情況來 -
./reports:是reports目錄
PS:覺得這篇文章有用的朋友,多多點贊打賞哦~!