Pytest 生成Report
2017-03-11 本文已影响975人
Yating_Yang
Pytest支持以下几种测试Report,跟着我一起来学习吧!
在你的PyCharm工程中新建test_one_func.py文件,然后在Terminal中运行下面的实例,检查有没有生成对应的Report。
在你的工程对应的目录下建立一个新文件夹: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


2. 生成result log格式的测试报告
txt也是最简单的一种测试报告,实例如下。运行完case后可以到report路径下去查看相应的txt文件,也可以在PyCharm打开该txt文件查看。
pytest -v test_one_func.py --resultlog=report\log.txt

3. 生成Html格式的测试报告
html格式的测试报告在浏览器观看效果很好,还可以把这些测试报告放在Web服务器上。
首先,需要安装pytest-html插件。
pip install pytest-html

pytest -v test_one_func.py --html=report\test_one_func.html
这个report的格式还是很漂亮的,有Environment,Summary和Report,非常友好。

最后,如果你的report文件夹内正确生成了上面介绍的这三种格式的测试报告,恭喜你!
