Python单元测试框架Pytest——如何生成测试报告

来源:互联网 发布:niconico软件 编辑:程序博客网 时间:2024/06/05 10:36

本文讲述pytest如何生成测试报告。

首先准备一段测试代码:

import py.testclass 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、文本格式的报告

C:\Users\liu.chunming\Desktop>py.test test_report.py --resultlog=C:\Users\liu.chunming\Desktop\log.txt

指定当前路径下生成log.txt文件,打开文件,内容如下:


2、生成JUnitXml格式报告

该格式方便与CI服务器进行集成。

C:\Users\liu.chunming\Desktop>py.test test_report.py --junitxml=C:\Users\liu.chunming\Desktop\log.xml
打开生成的log.xml,内容如下:


3、将测试报告发送到pastebin服务器

C:\Users\liu.chunming\Desktop>py.test test_report.py --pastebin=all

点击生成的网址,内容如下:


当然,你可以只将失败的报告发送到pastebin服务器

C:\Users\liu.chunming\Desktop>py.test test_report.py --pastebin=failed

4、生成Html格式报告

这个需要安装pytest的第三方插件pytest-html:

C:\Users\liu.chunming\Desktop>pip install -U pytest-html
执行测试:

C:\Users\liu.chunming\Desktop>py.test test_report.py --html=C:\Users\liu.chunming\Desktop\log.html

打开生成的测试报告log.html:


0 0
原创粉丝点击