pytest-json-report
pytest-json-report is a pytest plugin that generates a comprehensive JSON report of your test results. It provides detailed information on test outcomes, collected items, and environment details, useful for integration with CI/CD systems or custom reporting tools. The current version is 1.5.0, with minor releases occurring every few months to address bugs and add features.
Warnings
- breaking Python 2.x support was dropped in v1.2.4. Python 3.7+ is now required. As of v1.5.0, supported Python versions are 3.7 - 3.10, and pytest versions 3.7 - 7.0.
- gotcha The plugin does not activate automatically. You must explicitly enable it via the command line with `--json-report` or by adding `addopts = --json-report` to your `pytest.ini` configuration file.
- gotcha The structure and content of the JSON report, particularly the `summary` and `collected` counts, have evolved. Specifically, the `collected` count was fixed in v1.5.0 to accurately reflect all items, and the `deselected` count was added to the summary in v1.5.0 (and to collector result in v1.2.0).
- gotcha By default, the report is saved as `report.json` in the current working directory. To customize the output file path or control terminal verbosity, use specific command-line options.
Install
-
pip install pytest-json-report
Quickstart
import pytest
import json
import os
def test_success():
assert True
def test_failure():
assert False
@pytest.mark.skip(reason="demonstrating skip")
def test_skipped():
pass
def test_record_property(record_property):
record_property("my_custom_key", "my_custom_value")
assert True
# Run pytest and generate report.json
# os.system('pytest --json-report') would run it, but for a quickstart example, we'll demonstrate the command.
print("To run this example, save it as `test_example.py` and run:")
print("pytest --json-report")
print("Then inspect the generated `report.json` file.")
# Example of reading the report (would typically be in a separate script or CI/CD)
# if os.path.exists('report.json'):
# with open('report.json', 'r') as f:
# report_data = json.load(f)
# print(json.dumps(report_data['summary'], indent=2))