Description
# Pytest JSON Report [](https://github.com/numirias/pytest-json-report/actions/workflows/main.yml) [](https://pypi.python.org/pypi/pytest-json-report) [](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
| Version | Changes | Urgency | Date |
|---|---|---|---|
| 1.5.0 | Imported from PyPI (1.5.0) | Low | 4/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 gracefully | Low | 3/15/2022 |
| v1.4.1 | # Changes - Fix #69 | Low | 9/24/2021 |
| v1.4.0 | # Changes - More control over terminal output verbosity (`--json-report-verbosity`) | Low | 6/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)) | Low | 5/17/2021 |
| v1.2.4 | # Changes - Drop 2.7 support | Low | 10/23/2020 |
| v1.2.3 | # Changes - 3.9 support | Low | 10/23/2020 |
| v1.2.2 | # Changes - #55 | Low | 10/23/2020 |
| v1.2.1 | # Changes - Added `category` to warnings. | Low | 11/30/2019 |
| v1.2.0 | # Changes - Added `collected` key to summary. - Added `deselected` key to collector result. | Low | 11/5/2019 |
| v1.1.0 | # Changes - Added `pytest_json_runtest_stage` and `pytest_json_runtest_metadata` hooks (#43) | Low | 7/6/2019 |
| v1.0.5 | # Changes - Fix #41 | Low | 6/28/2019 |
| v1.0.4 | # Changes - Fix #31, #37 | Low | 5/31/2019 |
| v1.0.3 | # Changes - Fix teststatus hook signature (following https://github.com/pytest-dev/pytest/commit/5b09eb1d742a36b15318988df783204d4dee25eb) | Low | 2/4/2019 |
| v1.0.2 | # Changes - Using the `json_metadata` fixture without `--json-report` doesn't raise internal errors, but returns a dummy `dict`. | Low | 1/28/2019 |
| v1.0.1 | This 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 fixes | Low | 1/23/2019 |
| v0.7.0 | # Changes - Enabled use with direct `pytest.main()` invocation | Low | 3/16/2018 |
| v0.6.0 | # Changes - Added log capturing - Added option to pretty-print JSON | Low | 2/28/2018 |
