Skip to main content

PyPI - Python Version GitHub deployments

pytest-broadcaster

A plugin to write pytest collect output to various destinations.

Available destinations:

  • JSON file
  • JSON lines file.
  • HTTP URL (only POST request)

Additional destinations can be added in the future, and users can also implement their own destinations.

Project state

This project is in early development. The plugin is functional, but the API is not stable yet. The plugin is tested with Python 3.8, 3.9, 3.10, 3.11, and 3.12.

If you find a bug, please open an issue. Contributions are welcome.

Install

pip install pytest-broadcaster

Motivation

If you ever wanter to build a tool that needs to parse the output of pytest --collect-only, you may have noticed that the output is not very easy to parse. This plugin aims to provide a more structured output that can be easily parsed by other tools.

Historically, this project only parsed the output of pytest --collect-only, but it has been extended to parse the output of pytest in general.

JSON schemas are provided for clients to help them parse the output of the plugin.

Usage

  • Use the --collect-report to generate a JSON file:
pytest --collect-report=collect.json
  • Use the --collect-log to generate a JSON lines file:
pytest --collect-log=collect.jsonl
  • Use the --collect-url to send session result to an HTTP URL:
pytest --collect-url=http://localhost:8000/collect
  • Use the --collect-log-url to send each session event to an HTTP URL:
pytest --collect-log-url=http://localhost:8000/collect

JSON Schemas

The plugin provides JSON schemas to validate the output of the plugin. Generated schemas are located in the schemas directory, while the original schemas are located in the src/pytest_broadcaster/schemas directory.

SessionResult

The JSON output produced by the plugin follows the SessionResult JSON Schema.

Python tools can also use the SessionResult dataclass to parse the JSON file.

SessionEvent

The JSON lines output produced by the plugin follows the SessionEvent JSON Schema.

This schema is the union of the different events that can be emitted by the plugin:

Python tools can also use the SessionEvent dataclass to parse the JSON lines file, as well as the differnt event classes:

Hooks

pytest_broadcaster_add_destination

The plugin provides a hook that can be used by users to add custom destinations. For example, in your conftest.py you can add the following code to write the collect output to a JSON file and a JSON lines file:

from pytest_broadcaster import JSONFile, JSONLinesFile


def pytest_broadcaster_add_destination(add):
    add(JSONFile("collect.json"))
    add(JSONLinesFile("collect.jsonl"))

pytest_broadcaster_set_reporter

The plugin provides a hook that can be used by users to set a custom reporter. For example, in your conftest.py you can add the following code to use a custom reporter (well the default reporter in this case):

from pytest_broadcaster import DefaultReporter


def pytest_broadcaster_set_reporter(set):
    set(DefaultReporter())

Alternatives

  • pytest-json-report: This plugin predates pytest-broadcaster, has been used by several organizations, and works well. However, there is no JSON schema to validate the output, nor JSON lines output. Also, it does not allow adding custom destinations as pytest-broadcaster does.

  • pytest-report-log: This package provides both JSON and JSON lines output, but it does not provide a JSON schema to validate the output. Also, it does not allow adding custom destinations as pytest-broadcaster does.

Credits

  • pytest: Well, this is a pytest plugin.
  • pytest-report-log: This package was heavily inspired by the report-log plugin.
  • pytest-json-report: The pytest-json-report plugin was also a source of inspiration.
  • pytest-csv: The pytest-csv plugin was also a source of inspiration.
  • datamodel-code-generator: The dataclasses generation from JSON schemas is performed using datamodel-code-generator.
  • rye: Project management is easy thanks to rye. It is also used to lint and format the code.
  • hatch-vcs: Python project version control is easy thanks to hatch-vcs.
  • pyright: pyright is used to check the code and find bugs sooner.

License

This project is licensed under the terms of the MIT license. See LICENSE for more information.

Metadata

Release files for pytest-broadcaster 0.11.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytest-broadcaster 0.11.0
File Size Uploaded
pytest_broadcaster-0.11.0.tar.gz 139.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pytest-broadcaster 0.11.0
File Interpreter ABI Platform
pytest_broadcaster-0.11.0-py3-none-any.whl Python 3 none any Details

Total release size: 172.0 kB

Release files / pytest_broadcaster-0.11.0.tar.gz

Download URL pytest_broadcaster-0.11.0.tar.gz
Size 139.7 kB
Tags Source
SHA-256 checksum
How to use checksums
f480c51a4a5bfa9a0d7f17f166352cf3faf1ea2d8aff4173910bca52a60b09d9
BLAKE2b-256 checksum
How to use checksums
a28113ba21818a6435d37903b708b5b4b4d8c0f26b9088c9715b80bbaf4b8f66
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.6.3

Release files / pytest_broadcaster-0.11.0-py3-none-any.whl

Download URL pytest_broadcaster-0.11.0-py3-none-any.whl
Size 32.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
72edad4a90569733de03b201561e24a627ec1764c4668976670f395ff4d5bc8e
BLAKE2b-256 checksum
How to use checksums
432b1fb662150a19e96ffa9101da0c86dc93ac2a9b8419e91052131595deba81
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.6.3

Release history Release notifications | RSS feed

This release

0.11.0 This release

2 release files

0.9.0

2 release files

0.7.0

2 release files

0.6.0

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page