Pytest plugin to broadcast pytest output to various destinations
Project description
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-reportto generate a JSON file:
pytest --collect-report=collect.json
- Use the
--collect-logto generate a JSON lines file:
pytest --collect-log=collect.jsonl
- Use the
--collect-urlto send session result to an HTTP URL:
pytest --collect-url=http://localhost:8000/collect
- Use the
--collect-log-urlto 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:
SessionStartJSON SchemaWarningMessageJSON SchemaErrorMessageJSON SchemaCollectReportJSON SchemaTestCaseSetupJSON SchemaTestCaseCallJSON SchemaTestCaseTeardownJSON SchemaTestCaseEndJSON SchemaSessionEndJSON Schema
Python tools can also use the SessionEvent dataclass to parse the JSON lines file, as well as the differnt event classes:
SessionStartdataclassWarningMessagedataclassErrorMessagedataclassCollectReportdataclassTestCaseSetupdataclassTestCaseCalldataclassTestCaseTeardowndataclassTestCaseEnddataclassSessionEnddataclass
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 aspytest-broadcasterdoes. -
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-broadcasterdoes.
Credits
- pytest: Well, this is a pytest plugin.
- pytest-report-log: This package was heavily inspired by the
report-logplugin. - pytest-json-report: The
pytest-json-reportplugin was also a source of inspiration. - pytest-csv: The
pytest-csvplugin was also a source of inspiration. datamodel-code-generator: The dataclasses generation from JSON schemas is performed usingdatamodel-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:
pyrightis 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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pytest_broadcaster-0.11.0.tar.gz.
File metadata
- Download URL: pytest_broadcaster-0.11.0.tar.gz
- Upload date:
- Size: 139.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f480c51a4a5bfa9a0d7f17f166352cf3faf1ea2d8aff4173910bca52a60b09d9
|
|
| MD5 |
c35695acccfc426bd15ff5ac6e5eaee8
|
|
| BLAKE2b-256 |
a28113ba21818a6435d37903b708b5b4b4d8c0f26b9088c9715b80bbaf4b8f66
|
File details
Details for the file pytest_broadcaster-0.11.0-py3-none-any.whl.
File metadata
- Download URL: pytest_broadcaster-0.11.0-py3-none-any.whl
- Upload date:
- Size: 32.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
72edad4a90569733de03b201561e24a627ec1764c4668976670f395ff4d5bc8e
|
|
| MD5 |
41a480f84ab273b7802972284d23e2ba
|
|
| BLAKE2b-256 |
432b1fb662150a19e96ffa9101da0c86dc93ac2a9b8419e91052131595deba81
|