用于替换--resultlog选项,专注于简洁和可扩展性
项目描述
用于替换--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的散列
算法 | 散列摘要 | |
---|---|---|
SHA256 | c9f2079504ee51f776d3118dcf5e4730f163d3dcf26ebc8f600c1fa307bf638c |
|
MD5 | 29f720999f61608dc427dd735f96b1b8 |
|
BLAKE2b-256 | daa0d1372b23d415a0766389480633a676fb1530e94ae8f6ea84619cae0ac215 |
关闭
pytest-reportlog-0.4.0-py3-none-any.whl的散列
算法 | 散列摘要 | |
---|---|---|
SHA256 | 5db4d00586546d8c6b95c66466629f1e913440c36d97795a673d2e19c5cedd5c |
|
MD5 | 58bf0392f9f26883ab290d5a6a3c21c1 |
|
BLAKE2b-256 | cf856c7ae08dc1a45f1252591f2824ff7a6a89c57f7ee61e69d5670611eb0bd4 |