Skip to main content

使用模板生成 Pytest 报告

项目描述

PyPI 版本

从模板生成Pytest报告。您可以使用 PyPI 上可用的模板之一(如参考模板pytest-reporter-html1),在您自己的模板中继承它们以调整其内容和外观或从头开始制作自己的模板。

可以生成任何基于文本的内容,例如 HTML、LaTeX、CSV 等。

安装

您可以通过PyPI中的pip安装“pytest-reporter” :

$ pip install pytest-reporter

用法

指定要使用的模板和报告的输出路径:

$ pytest --template-dir=templates --template=report.html --report=report.html

编写模板

这个插件没有内置对任何模板引擎的支持,它取决于每个模板来实现渲染(或使用另一个模板插件作为基础)。一个最小的模板可能只实现 pytest_reporter_render钩子。

这是一个非常基本的 Jinja2 模板实现:

from jinja2 import Environment, FileSystemLoader, TemplateNotFound

def pytest_reporter_render(template_name, dirs, context):
    env = Environment(loader=FileSystemLoader(dirs))
    try:
        template = env.get_template(template_name)
    except TemplateNotFound:
        # Don't know about this name, may be provided by some other template
        return
    return template.render(context)

有关完整的参考实现,请参阅pytest-reporter-html1

模板上下文

适用于所有模板的标准上下文包括以下内容:

  • 配置配置

  • 会话会话

  • started : 会话开始时的 Unix 时间戳

  • 结束:会话结束时的 Unix 时间戳

  • warnings[] : 警告列表。WarningMessage

  • items : 以 nodeid 为键的收集项目字典

  • tests[]:每个测试运行的列表作为具有以下键的字典:

    • 项目项目

    • phase [] : 每个测试阶段 (setup, call, teardown) 的列表作为具有以下键的字典:

      • 呼叫(可选):呼叫信息(如果可用)。

      • 报告测试报告

      • section:与report.sections相同,但仅针对此阶段捕获的部分

      • log_records[] : 测试阶段记录的logging.LogRecord列表

      • status:此阶段的状态。具有以下键的字典:

        • category:状态的类别(例如“通过”)或空字符串

        • letter:状态的单字母版本(例如"P")或空字符串

        • word:状态的大写单词版本(例如“PASSED”)或空字符串

        • style : 带有例如{"yellow": True}的字典或空字典

    • status:整个测试的状态。具有以下键的字典:

      • category:测试状态的类别(例如“通过”

      • letter:测试状态的单字母版本(例如“P”

      • word:测试状态的大写单词版本(例如“PASSED”

      • style : 带有例如{"yellow": True}的字典或空字典

可以使用以下方法扩展或修改上下文:

  • config.template_context

  • template_context夹具_

  • pytest_reporter_context () 钩子

挂钩

有关可用钩子的完整列表,请参阅hooks.py

执照

根据MIT许可条款分发,“pytest-reporter”是免费的开源软件

问题

如果您遇到任何问题,请提交问题并附上详细说明。

项目详情


下载文件

下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。

源分布

pytest-reporter-0.5.2.tar.gz (9.6 kB 查看哈希

已上传 source

内置分布

pytest_reporter-0.5.2-py3-none-any.whl (7.5 kB 查看哈希

已上传 py3