pytest-json
pytest-json is a plugin for the pytest testing framework that generates JSON reports of test results. Last updated in January 2016, its latest version is 0.4.0. The project appears to be unmaintained, with limited Python and pytest version compatibility. It does not have an active release cadence.
Common errors
-
AttributeError: 'Config' object has no attribute '_json_report'
cause This error typically occurs when attempting to access `session.config._json_report` in a `pytest` hook or fixture when the `pytest-json` plugin was not loaded or its internal configuration attribute has changed in a newer `pytest` version.fixEnsure you are running `pytest` with the `--json` flag (e.g., `pytest --json=report.json`). If the error persists, it indicates an incompatibility with your `pytest` version. Downgrade `pytest` to a version compatible with `pytest-json` (e.g., `pytest<3.0` for Python 3.5, even older for Python 2.7). -
json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line X column Y (char Z)
cause This is a general JSON parsing error, but when encountered with `pytest-json` reports, it often means that custom metadata added via `report.stage_metadata` or `request.config._json_environment` in `conftest.py` is not strictly JSON-serializable (e.g., Python objects, single-quoted strings, or unescaped characters).fixVerify that all custom data added to the report is valid JSON, ensuring strings are double-quoted and complex Python objects are converted to their JSON-serializable equivalents (e.g., dictionaries, lists, strings, numbers, booleans, null).
Warnings
- breaking pytest-json has not been updated since January 2016 and officially supports Python versions 2.7, 3.3, 3.4, and 3.5, and py.test 2.7 or newer. It is highly likely to be incompatible with modern Python (>=3.6) and recent pytest versions due to significant changes in both Python and pytest's internal APIs and hook specifications.
- gotcha The plugin relies on internal pytest structures for customization (e.g., `request.config._json_environment` and `report.stage_metadata`). These internal APIs are not part of pytest's public, stable interface and are subject to change without warning in newer pytest releases, leading to `AttributeError` or other unexpected behavior.
- deprecated pytest-json is no longer actively maintained. This means it will not receive new features, bug fixes, or security updates. Using an unmaintained library can introduce vulnerabilities or become a blocker for upgrading other dependencies.
Install
-
pip install pytest-json
Imports
- pytest
import pytest
Quickstart
import pytest
import json
import os
# Create a dummy test file for demonstration (test_example.py)
with open('test_example.py', 'w') as f:
f.write("""
def test_success():
assert True
def test_failure():
assert False
""")
# Define the output report path
report_path = 'report.json'
# Run pytest with the json report plugin
# Ensure pytest-json is installed in the environment
# The exit code can be non-zero if tests fail, so we capture it.
exit_code = pytest.main([f'--json={report_path}', 'test_example.py'])
# Check if the report was generated and read it
if os.path.exists(report_path):
with open(report_path, 'r') as f:
report_data = json.load(f)
print(f"JSON report generated successfully:\n{json.dumps(report_data, indent=2)}")
os.remove(report_path)
os.remove('test_example.py')
else:
print("JSON report was not generated.")
# Example of custom environment data (requires conftest.py)
# conftest.py content:
# @pytest.fixture(scope='session', autouse=True):
# def extra_json_environment(request):
# request.config._json_environment.append(('CI_BUILD_ID', os.environ.get('CI_BUILD_ID', 'local')))