pytest+allure生成测试报告一直loading且404
今天在使用pytest和allure生成测试报告时,pycharm和本地打开index.html文件均显示loading和404,百度了半天答案,现在总结下。(虽然都是其他大神的答案,但是自己手动汇总下也算巩固吧!)
问题
if __name__ == '__main__':
# 生成allure测试报告数据,存放在report文件夹下
os.system('pytest -vs test1.py --alluredir ./report')
# 将report文件夹数据渲染成html报告,存在report1文件夹中
os.system('allure generate report -o report1 --clean')
使用上面命令生成测试报告,本地打开时显示loading和404.
原因
allure生成的html测试报告,无法直接双击在浏览器中打开,需要用allure命令,执行后自动打开浏览器。
解决方法
allure提供了两种解决方法,allure serve和allure open两个命令。
1.allure serve
此命令适用于pytest -vs test1.py --alluredir ./report 命令执行后生成的测试报告数据,将数据直接转化成html并自动打开,report文件夹文件如下:
在report上级目录下执行allure serve report即可打开测试报告。
命令执行后自动打开浏览器,如下:
2.allure open
此命令使用于已生成了本地index.html文件后执行,即执行了上文问题中的两条命令,生成了如下文件:
此时在上级路径下执行allure open report1命令即可自动打开报告:
到了这里基本上问题已经解决了,但是每次都要手动的敲这两个命令也是挺烦的,我们可以把allure open或allure serve这两个命令写入到bat文件中,直接双击执行就好了。
if __name__ == '__main__':
os.system('pytest -vs test1.py --alluredir ./report')
# 测试报告路径
report_path = os.path.join(os.path.dirname(__file__), 'report')
# 生成的bat文件路径,该文件至于测试报告平级
bat_file_path = os.path.join(os.path.dirname(__file__), 'click.bat')
if os.listdir(report_path):
with open(bat_file_path, 'w') as f:
f.write("allure serve report")
这样就完美的解决了所有问题,查看测试报告直接双击bat文件就行了!