PyTestReport-0.1.9版本更新

PyTestReport 0.1.9版本正式发布,增加了对PyTest框架的支持,支持通过lib API接口生成测试报告、HTML报告转图片报告及发送邮件功能,还修复了一些bug、规范了数据格式。文中介绍了与PyTest框架结合使用、通过API生成报告、生成图片报告并发送邮件的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原文首发

还记得那个发布不就的Python单元测试报告框架么?噢!如果不记得那我今天就再来说一篇吧!_ PyTestReport 0.1.9版本正式发布了!

多了哪些功能呢?

  • 增加对PyTest框架的支持(目前可以支持unittest、pytest两大框架)
  • 增加了支持通过lib API接口的方式生成测试报告(前提是把报告数据整理成要求的格式)
  • 增加了HTML报告转图片报告的API
  • 增加了发送邮件功能的API
  • 修复了一些bug!这些是自然的!
  • 规范了一些数据格式的字段和内容

接下来,请坐好小板凳,我准备带你们飞了!!!

如何与PyTest框架结合使用

对于pytest框架,收集其测试结果信息是通过pytest插件形式实现的。使用之前只要确保正常安装了PyTestReport即可。具体使用方式如下:

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"])

另外,你也可以通过命令行的方式来启动pytest执行单元测试。比如:

pytest -s pytest_Demo.py --pytest_report Pytest_Report.html --pytest_theme new_theme

如何通过API的方式生成报告

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.

同样的,你也可以指定特定的主题或者样式。比如:

...
with open('API_Report.html', 'wb') as fp:
    make_report(fp, data, theme='new_theme', stylesheet='new_stylesheet_2.css')

如何生成图片报告并发送邮件

from pytestreport.api import make_image, send_report

html = r"D:\test\report.html"
image = r"D:\test\report.png"
make_image(html, image)
# will be generate "report.png" file at "D:\test\". 

send_report("subject", html, image, "form@email.com", ["to@email.com"], cc=["cc@email.com"])
# will be send image content email with html as attach accordingly.

新书推荐

Python Web自动化测试设计与实现

获取更多关于Python和自动化测试的文章,请扫描如下二维码!
关注二维码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

上帝De助手

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值