pytest单元测试框架(3)allure报告

一、生成html测试报告

pytest xxx.py --html=./report/report.html --self-contained-html 生成的html报告合并了css样式

pytest单元测试框架(3)allure报告_第1张图片
html测试报告

二、生成Allure测试报告

1、Allure下载,可以下载最新版的:https://github.com/allure-framework/allure2/releases/tag/2.7.0。将下载的文件,解压
2、配置环境变量:进入解压的文件目录D:\soft\allure-2.7.0\bin,添加到path环境变量中。查询是否安装成功:cmd→allure
3、安装:pip install allure-pytest,pip install pytest-html,pip install pyyaml
4、执行:
a)pytest xx.py -s -q --alluredir 指定report路径;默认当前路径 :pytest xx.py -s -q --alluredir report。生成报告路径后,以后就不用再执行这条命令,直接执行用例,再执行生成测试报告就ok
b)allure generate report/ -o report/html这样就生成了测试报告,去目录中打开吧

pytest单元测试框架(3)allure报告_第2张图片
image.png

pytest单元测试框架(3)allure报告_第3张图片
image.png

5、由于不断的执行,会先生成更多的json文件,会导致文件冗余,所以执行用例的时候需要加上 --clean-alluredir

if __name__ == '__main__':
    # 由于每次执行的时候都要先生成json文件,会导致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文件转换成html格式的测试报告
    os.system('allure generate report/ -o report/html --clean')

PS:觉得这篇文章有用的朋友,多多点赞打赏哦~!

你可能感兴趣的:(pytest单元测试框架(3)allure报告)