跳转到主要内容

用于替换--resultlog选项,专注于简洁和可扩展性

项目描述

python version anaconda ci black

用于替换--resultlog选项,专注于简洁和可扩展性。

用法

pytest-reportlog作为测试环境中的测试需求安装。

--report-log=FILE选项在测试会话执行时以JSON lines格式写入文件。

报告日志中的每一行都包含一个自包含的JSON对象,对应一个测试事件,例如收集或测试结果报告。该文件保证在写入每一行后刷新,因此系统可以实时读取和处理事件。

每个JSON对象都包含一个特殊键 $report_type,它包含该类型报告对象的唯一标识符。为了未来的兼容性,文件的使用者应该忽略他们不认识的报告,以及忽略他们知道的JSON对象中的未知属性/键,因为未来的pytest版本可能会通过更多的属性/键来丰富对象。

压缩

常见的压缩后缀如 .gz.bz2.xz 将自动使用请求的压缩格式。使用的是python stdlib的实现,必须在python构建中启用。

示例

考虑这个文件

# content of test_report_example.py


def test_ok():
    assert 5 + 5 == 10


def test_fail():
    assert 4 + 4 == 1
$ pytest test_report_example.py -q --report-log=log.jsonl
.F                                                                   [100%]
================================= FAILURES =================================
________________________________ test_fail _________________________________

    def test_fail():
>       assert 4 + 4 == 1
E       assert (4 + 4) == 1

test_report_example.py:8: AssertionError
------------------- generated report log file: log.jsonl --------------------
1 failed, 1 passed in 0.12s

生成的 log.jsonl 将每行包含一个JSON对象

$ cat log.jsonl
{"pytest_version": "5.2.2", "$report_type": "SessionStart"}
{"nodeid": "", "outcome": "passed", "longrepr": null, "result": null, "sections": [], "$report_type": "CollectReport"}
{"nodeid": "test_report_example.py", "outcome": "passed", "longrepr": null, "result": null, "sections": [], "$report_type": "CollectReport"}
{"nodeid": "test_report_example.py::test_ok", "location": ["test_report_example.py", 0, "test_ok"], "keywords": {"test_ok": 1, "pytest-reportlog": 1, "test_report_example.py": 1}, "outcome": "passed", "longrepr": null, "when": "setup", "user_properties": [], "sections": [], "duration": 0.0, "$report_type": "TestReport"}
{"nodeid": "test_report_example.py::test_ok", "location": ["test_report_example.py", 0, "test_ok"], "keywords": {"test_ok": 1, "pytest-reportlog": 1, "test_report_example.py": 1}, "outcome": "passed", "longrepr": null, "when": "call", "user_properties": [], "sections": [], "duration": 0.0, "$report_type": "TestReport"}
{"nodeid": "test_report_example.py::test_ok", "location": ["test_report_example.py", 0, "test_ok"], "keywords": {"test_ok": 1, "pytest-reportlog": 1, "test_report_example.py": 1}, "outcome": "passed", "longrepr": null, "when": "teardown", "user_properties": [], "sections": [], "duration": 0.00099945068359375, "$report_type": "TestReport"}
{"nodeid": "test_report_example.py::test_fail", "location": ["test_report_example.py", 4, "test_fail"], "keywords": {"test_fail": 1, "pytest-reportlog": 1, "test_report_example.py": 1}, "outcome": "passed", "longrepr": null, "when": "setup", "user_properties": [], "sections": [], "duration": 0.0, "$report_type": "TestReport"}
{"nodeid": "test_report_example.py::test_fail", "location": ["test_report_example.py", 4, "test_fail"], "keywords": {"test_fail": 1, "pytest-reportlog": 1, "test_report_example.py": 1}, "outcome": "failed", "longrepr": {"reprcrash": {"path": "D:\\projects\\pytest-reportlog\\test_report_example.py", "lineno": 6, "message": "assert (4 + 4) == 1"}, "reprtraceback": {"reprentries": [{"type": "ReprEntry", "data": {"lines": ["    def test_fail():", ">       assert 4 + 4 == 1", "E       assert (4 + 4) == 1"], "reprfuncargs": {"args": []}, "reprlocals": null, "reprfileloc": {"path": "test_report_example.py", "lineno": 6, "message": "AssertionError"}, "style": "long"}}], "extraline": null, "style": "long"}, "sections": [], "chain": [[{"reprentries": [{"type": "ReprEntry", "data": {"lines": ["    def test_fail():", ">       assert 4 + 4 == 1", "E       assert (4 + 4) == 1"], "reprfuncargs": {"args": []}, "reprlocals": null, "reprfileloc": {"path": "test_report_example.py", "lineno": 6, "message": "AssertionError"}, "style": "long"}}], "extraline": null, "style": "long"}, {"path": "D:\\projects\\pytest-reportlog\\test_report_example.py", "lineno": 6, "message": "assert (4 + 4) == 1"}, null]]}, "when": "call", "user_properties": [], "sections": [], "duration": 0.0009992122650146484, "$report_type": "TestReport"}
{"nodeid": "test_report_example.py::test_fail", "location": ["test_report_example.py", 4, "test_fail"], "keywords": {"test_fail": 1, "pytest-reportlog": 1, "test_report_example.py": 1}, "outcome": "passed", "longrepr": null, "when": "teardown", "user_properties": [], "sections": [], "duration": 0.0, "$report_type": "TestReport"}
{"exitstatus": 1, "$report_type": "SessionFinish"}

record_property

record_property fixture 允许为测试记录额外的信息,就像使用JUnitXML格式一样。考虑这个测试文件

def test_function(record_property):
    record_property("price", 12.34)
    record_property("fruit", "banana")
    assert True

这些信息将被记录在报告JSON对象中的 user_properties 键下,如下所示

..., "user_properties": [["price", 12.34], ["fruit", "banana"]], ...

请注意,这个嵌套列表结构只是元组列表(键值对)的JSON表示。

项目详情


下载文件

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

源分发

pytest-reportlog-0.4.0.tar.gz (10.4 kB 查看散列)

上传

构建分发

pytest_reportlog-0.4.0-py3-none-any.whl (6.4 kB 查看散列)

上传 Python 3

由以下支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误日志 StatusPage StatusPage 状态页面