您的位置:首页 > 编程语言 > Python开发

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

2015-08-31 18:34 841 查看
本文讲述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、文本格式的报告

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:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: