freshcrate
Home > Frameworks > pytest-json-report

pytest-json-report

A pytest plugin to report test results as JSON files

Description

# Pytest JSON Report [![CI](https://github.com/numirias/pytest-json-report/actions/workflows/main.yml/badge.svg)](https://github.com/numirias/pytest-json-report/actions/workflows/main.yml) [![PyPI Version](https://img.shields.io/pypi/v/pytest-json-report.svg)](https://pypi.python.org/pypi/pytest-json-report) [![Python Versions](https://img.shields.io/pypi/pyversions/pytest-json-report.svg)](https://pypi.python.org/pypi/pytest-json-report) This pytest plugin creates test reports as JSON. This makes it easy to process test results in other applications. It can report a summary, test details, captured output, logs, exception tracebacks and more. Additionally, you can use the available fixtures and hooks to [add metadata](#metadata) and [customize](#modifying-the-report) the report as you like. ## Table of contents * [Installation](#installation) * [Options](#options) * [Usage](#usage) * [Metadata](#metadata) * [Modifying the report](#modifying-the-report) * [Direct invocation](#direct-invocation) * [Format](#format) * [Summary](#summary) * [Environment](#environment) * [Collectors](#collectors) * [Tests](#tests) * [Test stage](#test-stage) * [Log](#log) * [Warnings](#warnings) * [Related tools](#related-tools) ## Installation ``` pip install pytest-json-report --upgrade ``` ## Options | Option | Description | | --- | --- | | `--json-report` | Create JSON report | | `--json-report-file=PATH` | Target path to save JSON report (use "none" to not save the report) | | `--json-report-summary` | Just create a summary without per-test details | | `--json-report-omit=FIELD_LIST` | List of fields to omit in the report (choose from: `collectors`, `log`, `traceback`, `streams`, `warnings`, `keywords`) | | `--json-report-indent=LEVEL` | Pretty-print JSON with specified indentation level | | `--json-report-verbosity=LEVEL` | Set verbosity (default is value of `--verbosity`) | ## Usage Just run pytest with `--json-report`. The report is saved in `.report.json` by default. ```bash $ pytest --json-report -v tests/ $ cat .report.json {"created": 1518371686.7981803, ... "tests":[{"nodeid": "test_foo.py", "outcome": "passed", ...}, ...]} ``` If you just need to know how many tests passed or failed and don't care about details, you can produce a summary only: ```bash $ pytest --json-report --json-report-summary ``` Many fields can be omitted to keep the report size small. E.g., this will leave out keywords and stdout/stderr output: ```bash $ pytest --json-report --json-report-omit keywords streams ``` If you don't like to have the report saved, you can specify `none` as the target file name: ```bash $ pytest --json-report --json-report-file none ``` ## Advanced usage ### Metadata The easiest way to add your own metadata to a test item is by using the `json_metadata` [test fixture](https://docs.pytest.org/en/stable/fixture.html): ```python def test_something(json_metadata): json_metadata['foo'] = {"some": "thing"} json_metadata['bar'] = 123 ``` Or use the `pytest_json_runtest_metadata` [hook](https://docs.pytest.org/en/stable/reference.html#hooks) (in your `conftest.py`) to add metadata based on the current test run. The dict returned will automatically be merged with any existing metadata. E.g., this adds the start and stop time of each test's `call` stage: ```python def pytest_json_runtest_metadata(item, call): if call.when != 'call': return {} return {'start': call.start, 'stop': call.stop} ``` Also, you could add metadata using [pytest-metadata's `--metadata` switch](https://github.com/pytest-dev/pytest-metadata#additional-metadata) which will add metadata to the report's `environment` section, but not to a specific test item. You need to make sure all your metadata is JSON-serializable. ### A note on hooks If you're using a `pytest_json_*` hook although the plugin is not installed or not active (not using `--json-report`), pytest doesn't recognize it and may fail with an internal error like this: ``` INTERNALERROR> pluggy.manager.PluginValidationError: unknown hook 'pytest_json_runtest_metadata' in plugin <module 'conftest' from 'conftest.py'> ``` You can avoid this by declaring the hook implementation optional: ```python import pytest @pytest.hookimpl(optionalhook=True) def pytest_json_runtest_metadata(item, call): ... ``` ### Modifying the report You can modify the entire report before it's saved by using the `pytest_json_modifyreport` hook. Just implement the hook in your `conftest.py`, e.g.: ```python def pytest_json_modifyreport(json_report): # Add a key to the report json_report['foo'] = 'bar' # Delete the summary from the report del json_report['summary'] ``` After `pytest_sessionfinish`, the report object is also directly available to script via `config._json_report.report`. So you can access it using some built-in hook: ```python def pytest_sessionfinish(session): report = session.config._json_report.report

Release History

VersionChangesUrgencyDate
1.5.0Imported from PyPI (1.5.0)Low4/21/2026
v1.5.0### Added - `deselected` count in summary ### Changed - Supported Python versions are now 3.7 - 3.10 ### Fixed - #73: Fix `collected` count in summary (previously reported number of *selected* items) - #75: Handle xdist worker crashes gracefullyLow3/15/2022
v1.4.1# Changes - Fix #69Low9/24/2021
v1.4.0# Changes - More control over terminal output verbosity (`--json-report-verbosity`)Low6/18/2021
v1.3.0# Changes - Lower pytest version requirement - Capture user properties (see [`record_property()`](https://docs.pytest.org/en/6.2.x/usage.html#record-property-example))Low5/17/2021
v1.2.4# Changes - Drop 2.7 supportLow10/23/2020
v1.2.3# Changes - 3.9 supportLow10/23/2020
v1.2.2# Changes - #55Low10/23/2020
v1.2.1# Changes - Added `category` to warnings.Low11/30/2019
v1.2.0# Changes - Added `collected` key to summary. - Added `deselected` key to collector result.Low11/5/2019
v1.1.0# Changes - Added `pytest_json_runtest_stage` and `pytest_json_runtest_metadata` hooks (#43)Low7/6/2019
v1.0.5# Changes - Fix #41Low6/28/2019
v1.0.4# Changes - Fix #31, #37Low5/31/2019
v1.0.3# Changes - Fix teststatus hook signature (following https://github.com/pytest-dev/pytest/commit/5b09eb1d742a36b15318988df783204d4dee25eb)Low2/4/2019
v1.0.2# Changes - Using the `json_metadata` fixture without `--json-report` doesn't raise internal errors, but returns a dummy `dict`.Low1/28/2019
v1.0.1This release is not backwards-compatible with `0.x.x` releases! # Changes - Major internal refactoring - Renamed and restructured command line arguments - Ported to Python 2.7 - Minor changes to JSON format (please refer to the format documentation) - Made compatible with pytest-xdist - Bug fixesLow1/23/2019
v0.7.0# Changes - Enabled use with direct `pytest.main()` invocationLow3/16/2018
v0.6.0# Changes - Added log capturing - Added option to pretty-print JSONLow2/28/2018

Dependencies & License Audit

Loading dependencies...

Similar Packages

pre-commitA framework for managing and maintaining multi-language pre-commit hooks.v4.6.0
azure-core-tracing-opentelemetryMicrosoft Azure Azure Core OpenTelemetry plugin Library for Pythonazure-template_0.1.0b6187637
spdx-toolsSPDX parser and tools.0.8.5
lacesDjango components that know how to render themselves.0.1.2
django-tasksA backport of Django's built in Tasks framework0.12.0