目录
二、pytest结合allure-pytest插件 生成allure测试报告
一、pytest 生成测试报告
本文讲述pytest如何生成测试报告。
首先准备一段测试代码:
import py.test
class TestCase(object):
def test_eq_set(self):
assert set([0, 10, 11, 12]) == set([0, 20, 21])
def test_eq_dict(self):
assert {'a': 0, 'b': 1, 'c': 0} == {'a': 0, 'b': 2, 'd': 0}
def test_eq_list(self):
assert [0, 1, 2] == [0, 1, 3]
def test_eq_longer_list(self):
assert [1,2] == [1,2,3]
1、文本格式的报告
py.test test_report.py --resultlog=d:\log.txt
指定当前路径下生成log.txt文件,打开文件,内容如下:
2、生成JUnitXml格式报告
该格式方便与CI服务器进行集成
py.test test_report.py --junitxml=d:\log.xml
打开生成的log.xml,内容如下:
3、将测试报告发送到pastebin服务器
py.test test_report.py --pastebin=all
点击生成的网址,内容如下:
当然,你可以只将失败的报告发送到pastebin服务器
py.test test_report.py --pastebin=failed
4、生成Html格式报告
这个需要安装pytest的第三方插件pytest-html:
pip install -U pytest-html
执行测试:
py.test test_report.py --html=d:\log.html
打开生成的测试报告log.html:
二、pytest结合allure-pytest插件 生成allure测试报告
测试脚本中添加了Allure特性之后,可以通过两步,就可以展示出测试报告了。
2.1 allure基本使用
第一步,生成测试报告数据
在py.test执行测试的时候,指定–alluredir选项及结果数据保存的目录:
$ py.test test/ --alluredir ./result/
./result/中保存了本次测试的结果数据。另外,还可以执行指定features或者stories执行一部分测试用例,比如执行”购物车功能“下的”加入购物车“子功能的测试用例:
$ py.test test/ --allure_features='购物车功能' --allure_stories='加入购物车'
第二步,生成测试报告页面
通过下面的命令将./result/目录下的测试数据生成测试报告页面:
$ allure generate ./result/ -o ./report/ --clean
–clean选项目的是先清空测试报告目录,再生成新的测试报告。
启动allure报告服务,输入生成的IP地址和端口即可访问allure
allure sever report