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.
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.
pip install pytest-broadcasterIf 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.
- 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/collectThe 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.
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.
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
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"))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())-
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.
- 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.
This project is licensed under the terms of the MIT license. See LICENSE for more information.