pytest-reportlog
pytest-reportlog is a pytest plugin that provides a modern, extensible replacement for the deprecated `--resultlog` option. It focuses on simplicity and extensibility by writing a stream of machine-readable JSON line objects (JSONL) representing test session events to a specified file. Each line contains a self-contained JSON object corresponding to a testing event, ensuring real-time processing capabilities as the file is guaranteed to be flushed after each line. The current version is 1.0.0, and it is maintained by the pytest-dev team, with releases typically tied to major pytest version compatibility or feature enhancements.
Warnings
- breaking Version 1.0.0 of pytest-reportlog dropped support for Python 3.7, 3.8, and 3.9. Users on these Python versions must either upgrade their Python environment or use an older version of the plugin.
- deprecated pytest-reportlog replaces the `--resultlog` option, which has been deprecated in pytest since version 4.0 and was removed in pytest 6.0. Users of `--resultlog` should migrate to `--report-log` for continued functionality and improved extensibility.
- gotcha The JSON output generated by `--report-log` does not adhere to a strictly defined, versioned schema. While event types are identified by the `$report_type` key, consumers of the log file should be prepared to ignore unknown properties/keys within JSON objects and unrecognized report types for future compatibility.
- gotcha There have been reported issues regarding the 'weird' handling of unserializable data within `user_properties` when attempting to serialize test reports to JSON. This can lead to unexpected output or serialization errors if complex, non-JSON-serializable objects are passed.
Install
-
pip install pytest-reportlog
Imports
- pytest-reportlog
This is a pytest plugin, used via command-line options; no direct Python imports are typically needed by users.
Quickstart
# 1. Create a test file (e.g., test_example.py):
# ---
# # content of test_example.py
# def test_ok():
# assert 5 + 5 == 10
#
# def test_fail():
# assert 4 + 4 == 1
# ---
# 2. Run pytest with the --report-log option:
# pytest test_example.py --report-log=log.jsonl
# 3. Inspect the generated log file (e.g., with Python):
import json
import os
log_file_path = "log.jsonl"
# This part simulates running pytest and creating the file.
# In a real scenario, you'd run the `pytest` command above in your shell.
# For quickstart execution, we create a dummy file if it doesn't exist.
if not os.path.exists(log_file_path):
with open(log_file_path, "w") as f:
f.write('{"pytest_version": "8.0.0", "$report_type": "SessionStart"}\n')
f.write('{"nodeid": "test_example.py::test_ok", "duration": 0.0001, "outcome": "passed", "$report_type": "TestReport"}\n')
f.write('{"nodeid": "test_example.py::test_fail", "duration": 0.0002, "outcome": "failed", "$report_type": "TestReport"}\n')
f.write('{"exitstatus": 1, "$report_type": "SessionFinish"}\n')
print(f"Reading first few lines from {log_file_path}:")
with open(log_file_path, "r") as f:
for i, line in enumerate(f):
if i >= 3: # Print first 3 events for brevity
break
print(json.loads(line))
# Clean up the dummy file
os.remove(log_file_path)