Pytest+Allure生成报告
来自:http://www.cnblogs.com/hao2018/p/9915044.html
https://mp.weixin.qq.com/s?src=11×tamp=1547536248&ver=1317&signature=tNsWNQnpqQFYRJMafAdm-l7VH-A9uCNCnWWyr4OgzuX5JNtmlhTOkZu*xEXgsfpTaf9pw*VmVmnc0oewuYiFgGdKY470x3LwJMvyOD7h2gXa4Sgdh*5VR8nq3lwu-YRQ&new=1
https://blog.csdn.net/yu1014745867/article/details/85120334
前言
allure是一个report框架,支持java的Junit/testng等框架,当然也可以支持python的pytest框架,也可以集成到Jenkins上展示高大上的报告界面。
一、环境准备
- 1.python3.6
- 2.windows环境
- 3.pycharm
- 4.pytest-allure-adaptor
- 5.allure2.9.0
- 6.java1.8
注:pytest-allure-adaptor安装和Command Tool安装(allure2)
1、pytest-allure-adaptor 快速安装:
pip install pytest-allure-adaptor
官方参考文档:https://pypi.org/project/pytest-allure-adaptor/
pytest命令基础上加–alluredir,执行后会生成xml报告。想要生成html报告,还需使用 Command Tool 来生成我们需要的美观报告。
2、安装 Command Tool:
allure的版本目前有2个,从github上看,allure1不再被支持,请考虑使用allure2 https://github.com/allure-framework/allure2替代。
allure-commandline releases版本地址:https://github.com/allure-framework/allure2/releases
(1)下载最新的Download allure2.9.0版本,如下:

(2)下载好之后,解压后放到想要的目录下,如当前放在D:\Dev\Python36下:

打开\allure-2.9.0\bin文件夹,会看到allure.bat文件,将此路径设置为系统环境变量path下,这样cmd任意目录都能执行了。

设置环境变量:

(3)验证allure,在cmd中输入allure,然后回车,如果可以看到一下,说明配置完成 (如不成功,重启电脑试试)
以上,环境准备完毕。
二、生成html报告命令
1、先在pytest命令基础上加–alluredir,生成xml报告。
pytest -s -q --alluredir report
如果不指定路径,默认在当前目录下新建一个report目录,当然也可以指定路径
pytest -s -q --alluredir 指定report路径
执行完之后打开report文件夹,会自动生成xml格式的报告。
栗子:在pycharm中使用,如下:
pytest.main([ "-s", "-q","testcxxx.py", "--alluredir", "./report/xml"])
用例执行完成之后会在[./report/xml]目录下生成了一堆xml的report文件,当然这不是我们最终想要的美观报告。
2、需要使用 Command Tool 来生成我们需要的美观报告。在命令行输入:
allure generate [xml_report_path] -o [html_report_path]
由于allure.bat已经加到环境变量了,所以也可以用相对路径去生成html报告。
栗子:在cmd命令行执行如下:
提示成功。
注:直接用chrome浏览器打开 report/html 中的 index.html 报告,会显示一个空的报告!!!
解决办法:
1、在pycharm中右击index.html选择打开方式Open in Browser就可以了。
2、使用 Firefox 直接打开index.html。

报告如下:

补:也可以直接在python中调用cmd命令执行。
完整版:
import pytest import time class TestClass(): def test_hehe(self): assert 2==1,"not good!" def test_haha(self): assert 1==1 if __name__ == '__main__': pytest.main([ "-s", "-q","testc001.py", "--alluredir", "./report/xml"]) import os command = 'allure generate ./report/xml -o ./report/html' time.sleep(2) os.system(command)
执行结果:
D:\Dev\Python36\python.exe D:/Dev/PyCharm/venvtest/case/Testa001/Testp001/testc001.py F. ================================== FAILURES =================================== _____________________________ TestClass.test_hehe _____________________________ self = <Testp001.testc001.TestClass object at 0x00000267EAE30E10> def test_hehe(self): > assert 2==1,"not good!" E AssertionError: not good! E assert 2 == 1 testc001.py:5: AssertionError 1 failed, 1 passed in 0.07 seconds Report successfully generated to .\report\html Process finished with exit code 0
报告生成成功,可查看报告。

浙公网安备 33010602011771号