Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/charbonnierg/pytest-discover


https://github.com/charbonnierg/pytest-discover

Last synced: about 2 months ago
JSON representation

Awesome Lists containing this project

README

        

# pytest-discover

A plugin to write pytest collect output to either a [JSON](https://www.json.org/json-en.html) file or a [JSON lines](https://jsonlines.org/) file.

## 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.

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

## Install

```bash
pip install pytest-discover
```

## Usage

- Use the `--collect-report` together with `--collect-only` option to collect tests and generate a JSON file:

```bash
pytest --collect-only --collect-report=collect.json
```

- Use the `--collect-log` together with `--collect-only` option to collect tests and generate a JSON lines file:

```bash
pytest --collect-only --collect-log=collect.jsonl
```

## Produced JSON output

The JSON file generated by `--collect-report` option follows the [DiscoveryResult JSON Schema](https://github.com/charbonnierg/pytest-discover/tree/main/schemas/discovery_result.json).

Python tools can also use the [`DiscoveryResult` dataclass](https://github.com/charbonnierg/pytest-discover/tree/main/src/pytest_discover/models/discovery_result.py) to parse the JSON file.

## Produced JSON Lines output

The JSON lines file generated by `--collect-log` option contains one JSON object per line. Each line is a JSON object that follows the [DiscoveryEvent JSON Schema](https://github.com/charbonnierg/pytest-discover/tree/main/schemas/discovery_event.json).

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

- [`SessionStart` JSON Schema](https://github.com/charbonnierg/pytest-discover/tree/main/schemas/session_start.json)
- [`WarningMessage` JSON Schema](https://github.com/charbonnierg/pytest-discover/tree/main/schemas/warning_message.json)
- [`ErrorMessage` JSON Schema](https://github.com/charbonnierg/pytest-discover/tree/main/schemas/error_message.json)
- [`CollectReport` JSON Schema](https://github.com/charbonnierg/pytest-discover/tree/main/schemas/collect_report.json)
- [`SessionFinish` JSON Schema](https://github.com/charbonnierg/pytest-discover/tree/main/schemas/session_finish.json)

Python tools can also use the [`DiscoveryEvent` dataclass](https://github.com/charbonnierg/pytest-discover/tree/main/src/pytest_discover/models/discovery_event.py) to parse the JSON lines file, as well as the differnt event classes:

- [`SessionStart` dataclass](https://github.com/charbonnierg/pytest-discover/tree/main/src/pytest_discover/models/session_start.py)
- [`WarningMessage` dataclass](https://github.com/charbonnierg/pytest-discover/tree/main/src/pytest_discover/models/warning_message.py)
- [`ErrorMessage` dataclass](https://github.com/charbonnierg/pytest-discover/tree/main/src/pytest_discover/models/error_message.py)
- [`CollectReport` dataclass](https://github.com/charbonnierg/pytest-discover/tree/main/src/pytest_discover/models/collect_report.py)
- [`SessionFinish` dataclass](https://github.com/charbonnierg/pytest-discover/tree/main/src/pytest_discover/models/session_finish.py)
- [`TestCase` dataclass](./src/pytest_discover/models/test_case.py)
- [`TestDirectory` dataclass](./src/pytest_discover/models/test_directory.py)
- [`TestModule` dataclass](./src/pytest_discover/models/test_module.py)
- [`TestSuite` dataclass](./src/pytest_discover/models/test_suite.py)

> Note: The code generation task is automated using `rye run generate-models` project script.

## Alternatives

- [pytest-json-report](https://github.com/numirias/pytest-json-report): This plugin not only generates a JSON report with collected nodes, but also with test results. It is a more complete solution than `pytest-discover`. However, there is no JSON schema to validate the output, nor JSON lines output.

## Credits

- [pytest-report-log](https://github.com/pytest-dev/pytest-reportlog): This package was heavily inspired by the `report-log` plugin.
- [pytest-csv](https://github.com/nicoulaj/pytest-csv): The `pytest-csv` plugin was also a source of inspiration.
- [`datamodel-code-generator`](https://github.com/koxudaxi/datamodel-code-generator): The dataclasses generation from JSON schemas is performed using `datamodel-code-generator`.
- [rye](https://rye-up.com/): Project management is easy thanks to `rye`.