pytest-reportlog

1.0.0 · active · verified Sun Apr 12

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

Install

Imports

Quickstart

To get started, you simply install the plugin and use the `--report-log=FILE` command-line option when running `pytest`. This will generate a JSONL file containing one JSON object per line, representing various test events.

# 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)

view raw JSON →