pytest生成HTML测试报告

背景

最近开发有关业务场景的功能时,涉及的API接口比较多,需要自己模拟多个业务场景的自动化测试(暂时不涉及性能测试),并且在每次测试完后能够生成一份测试报告。 考虑到日常使用python自带的unittest,所以先从官方文档下手,了解到有相关的TestTextRunner:

自带的TextTestRunner每次能把测试结果输出到流中的测试运行器,可以简单根据verbosity调整每次测试结果输出的信息,但是都太基础了,如果我想在测试过程中打印一些请求参数或者docstring,看了一下unittest内置的方法,实现过程可能会比较繁琐。

模块安装

pip install pytest
pip install pytest-html
复制代码

方案设计

pytest.ini

首先定义pytest.ini(pytest的基础配置文件,和测试文件在同一目录,使用pytest命令时会先读取该文件)

[pytest]
log_cli = True
log_cli_level = INFO

复制代码

备注:开启日志消息打印,设置日志记录捕获的最低消息级别为INFO

conftest.py

设置conftest.py(没有自己创建,同样是和测试文件同个目录下,用于pytest-html生成测试报告的配置文件)

# --*-- coding: utf-8 --*--
from datetime import datetime
from py.xml import html
import pytest


def pytest_html_report_title(report):
    report.title = "测试报告"


def pytest_html_results_table_header(cells):
    cells.insert(2, html.th("Description"))
    cells.insert(1, html.th("Time", class_="sortable time", col="time"))
    cells.pop()


def pytest_html_results_table_row(report, cells):
    cells.insert(2, html.td(report.description))


def pytest_html_results_table_row(report, cells):
    cells.insert(2, html.td(report.description))
    cells.insert(1, html.td(datetime.utcnow(), class_="col-time"))
    cells.pop()


@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport(item, call):
    outcome = yield
    report = outcome.get_result()
    report.description = str(item.function.__doc__)
复制代码

通过设置钩子函数分别修改测试报告的列,添加描述列,测试用例耗时时间列,删除链接列,我这里是直接参考官方文档中给出的示例

pytest-html.readthedocs.io/en/latest/u…

有兴趣的可以研究一下,

测试用例

根据pytest的官方文档,pytest同样是支持unittest的功能,所以可以在原有的基础上直接运行pytest

# -*- coding: utf-8 -*-
# @Author: linshukai
# @Desc: pytestc测试用例
# @Date: 20220827

import unittest
import pytest
import logging

import requests


class TestString(unittest.TestCase):
    def test_upper_method(self):
        """
        测试字符串大写
        """
        self.assertEqual("linshukai".upper(), "LINSHUKAI")
        logging.info("测试linshukai")

    def test_lower_method(self):
        """
        测试字符串小写
        """
        self.assertEqual( "ZhangSan".lower(), "zhangsan")
        logging.info("测试ZhangSan")

    def test_count_method(self):
        """
        测试字符串长度统计
        """
        self.assertEqual( len("zhangsan"), 8)
        logging.info("测试Zhangsan")


class TestString2(unittest.TestCase):

    def test_get_html(self):
        """
        测试请求网页状态码是否正常
        """
        response = requests.get("http://www.baidu.com")
        self.assertEqual(response.status_code, 500)


if __name__ == "__main__":
    pytest.main(["pytest_example.py", "--html=report.html", "--self-contained-html"])
复制代码

备注:
简单的写了几个测试用例,需要特别说明的是可以通过pytest.main()方法,直接在python代码中调用pytest,所以我每次只需要执行这个脚本就行,当然也可以选择在命令行界面通过pytest命令调用指定测试文件执行指定测试用例。
另外一个地方需要注意的是--self-contained-html这个参数主要是针对pytest-html模块,由于默认pytest-html中生成测试报告的网页和CSS文件都是分开来存储的,如果想直接将css文件合并到html中,这样分享测试报告的时候也更加方便,所以只需要加入这个参数即可--self-contained-html。

预期效果

 

预期会出现的问题

pytest中使用requests,抛出错误,但不影响测试结果,抛出的异常:

Windows fatal exception: code 1073807366
复制代码

解决方式,降低pytest为4.6.11版本后,异常就不会抛出,但是pytest-html需要6.0版本上的pytest,由于不影响测试结果,更加完善的解决方法后续再研究;

备注

后续关于pytest和pytest-html生成测试报告的方案后期如果有什么问题,或者优化的地方都会不断完善更新,欢迎讨论。。。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值