Pytest 生成Report
Pytest支持以下几种测试Report,跟着我一起来学习吧!
在你的PyCharm工程中新建test_one_func.py文件,然后在Terminal中运行下面的实例,检查有没有生成对应的Report。
在你的工程对应的目录下建立一个新文件夹:report,你也可以在其他目录创建。
![700](http://upload-images.jianshu.io/upload_images/4921719-0c62ee3c31fe8c04.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/700)
新建文件夹report
###This is test_one_func.py
def func(x):
return x + 1
def test_func():
assert func(3) == 5
1. 生成JunitXML 格式的测试报告
JunitXML报告是一种很常用的测试报告,比如可以和Jenkins进行集成,在Jenkins的GUI上显示Pytest的运行结果,非常便利。
运行完case后可以到report路径下去查看相应的xml文件,也可以在PyCharm打开该xml文件查看。
运行
pytest --junitxml=path
#绝对路径:
pytest -v test_one_func.py --junitxml=C:\Users\yatyang\PycharmProjects\pytest_example\report\test_one_func.xml
#相对路径
pytest -v test_one_func.py --junitxml=report\test_one_func.xml
![700](http://upload-images.jianshu.io/upload_images/4921719-ddad6d819edaf8cf.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/700)
生成JunitXML Report.png
![700](http://upload-images.jianshu.io/upload_images/4921719-2f1e86365c5c0f6c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/700)
test_one_func.xml 内容
2. 生成result log格式的测试报告
txt也是最简单的一种测试报告,实例如下。运行完case后可以到report路径下去查看相应的txt文件,也可以在PyCharm打开该txt文件查看。
pytest -v test_one_func.py --resultlog=report\log.txt
![700](http://upload-images.jianshu.io/upload_images/4921719-f4adfa5647c67233.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/700)
生成log Report.png
3. 生成Html格式的测试报告
html格式的测试报告在浏览器观看效果很好,还可以把这些测试报告放在Web服务器上。
首先,需要安装pytest-html插件。
pip install pytest-html
![700](http://upload-images.jianshu.io/upload_images/4921719-a8fc43bffc24e467.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/700)
安装pytest-html插件.png
pytest -v test_one_func.py --html=report\test_one_func.html
这个report的格式还是很漂亮的,有Environment,Summary和Report,非常友好。
![700](http://upload-images.jianshu.io/upload_images/4921719-5a75c4b0bc360eef.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/700)
test_one_func.html
最后,如果你的report文件夹内正确生成了上面介绍的这三种格式的测试报告,恭喜你!
![700](http://upload-images.jianshu.io/upload_images/4921719-4b28e70840933dc4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/700)
pytest的3种Report
</div>
</div>