PyTestReport

MIT License
Python
windows, linux
上帝De助手 · 2019年06月08日 · 2796 次阅读 · 2 条评论

PytestReport 一个由 HTMLTestRunner 项目为灵感,并基于 HTMLTestRunner 进行二次开发的一个项目。主要在 API 调用、报告样式、扩展性等方面进行了增强。

安装:

pip install PyTestReport -U

使用

PyTestReport 可用通过多种方式运行,分别如下:

单元测试 (unittest, pytest)
lib 库引入
命令行
REST API(暂未开放)

单元测试样例 (unittest)

import unittest
import pytestreport

class MyTest(unittest.TestCase):
    def testTrue(self):
        self.assertTrue(True)

if __name__ == '__main__':
    pytestreport.main(verbosity=2)

以这种方式执行之后,默认会在当前文件夹下生成一个 PyTestReport.html 日志文件,且这个文件名和样式模板都不可以重新指定的。或者你也可以用如下方式批量执行多个用例。

import unittest
from pytestreport import TestRunner

class MyTest(unittest.TestCase):
    def testTrue(self):
        self.assertTrue(True)

if __name__ == '__main__':
    suite = unittest.TestSuite()
    suite.addTests(unittest.TestLoader().loadTestsFromTestCase(MyTest))

    with open(r'/path/to/report.html', 'wb') as fp:
        runner = TestRunner(fp, title='测试标题', description='测试描述', verbosity=2)
        runner.run(suite)

单元测试样例(pytest)

import pytest

def testTrue():
    assert True

def testFalse():
    assert False

def testError():
    1 / 0

@pytest.mark.skip(reason="misunderstood the API")
def testSkip():
    assert 1 == 1

@pytest.mark.xfail(reason="Xpass")
def testXPass():
    assert True

@pytest.mark.xfail(reason="Xfail")
def testXFail():
    assert False


if __name__ == '__main__':
    pytest.main(["-s", "pytest_Demo.py", "--pytest_report", "Pytest_Report.html"])

基于 pytest 框架时,在调用时需要带上--pytest_report 参数,并指定一个报告的文件路径即可。当然你也可以同时指定一个非默认主题。比如:

import pytest

if __name__ == '__main__':
    pytest.main(["-s", "pytest_Demo.py", "--pytest_report", "Pytest_Report.html", 
    "--pytest_title", "report title", "--pytest_desc", "report desc",
    "--pytest_theme", "new_theme"])

引入 lib 生成报告

对于那些现有测试框架或者工具并不是基于单元测试的情况,也是可以直接使用 PyTestReport 的框架的,只需要把测试数据的格式整理成框架可接受的格式即可。下面是一个单测试类包含一个测试用例的测试报告数据样例,以及如何来生成测试报告。

from pytestreport.api import make_report

data = {
    "generator": "PyTestReport 0.1.4",
    "title": "默认主题",
    "description": "默认主题描述",
    "report_summary": {
        "start_time": "2019-05-12 23:07:49",
        "duration": "0:00:00.002000",
        "suite_count": 1,
        "status": {
            "pass": 1,
            "fail": 0,
            "error": 0,
            "skip": 0,
            "count": 1
        }
    },
    "report_detail": {
        "tests": [
            {
                "summary": {
                    "desc": "utDemo.UTestPass",
                    "count": 1,
                    "pass": 1,
                    "fail": 0,
                    "error": 0,
                    "skip": 0,
                    "cid": "testclass1",
                    "status": "pass"
                },
                "detail": [
                    {
                        "has_output": False,
                        "tid": "testpass.1.1",
                        "desc": "testTrue",
                        "output": "",
                        "status": "pass",
                        "status_code": 0
                    }
                ]
            }
        ],
        "count": "1",
        "pass": "1",
        "fail": "0",
        "error": "0",
        "skip": "0"
    }
}
with open('API_Report.html', 'wb') as fp:
    make_report(fp, data)
# will be create API_Report.html file at current directory.

报告截图

支持报告主题模板扩展

感兴趣的同学可以联系我来交流如何对扩展报告的主题和模板。five3@163.com

更多信息

关于测试报告框架的更多使用和开发,请移步至 github 官网。PyTestReport

评论列表
上帝De助手 发表于 2019年07月05日

目前 PyTestReport 已经更新至 0.1.9 版本了。可以支持 pytest 单元测试框架了。更多特性可以访问 github 官方查看哦

simple 发表于 2019年06月20日

感谢作者分享!