pytest-json

0.4.0 · abandoned · verified Thu Apr 16

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

Warnings

Install

Imports

Quickstart

To use `pytest-json`, create a test file (e.g., `test_example.py`). Then, run `pytest` from your terminal with the `--json` flag, specifying the desired output file. The plugin will generate a JSON report containing the test results. Custom environment data can be added via a `pytest` fixture in `conftest.py`.

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

view raw JSON →