pytest-metadata is a plugin for pytest that provides access to test session metadata. It collects system information, environment variables (especially from CI systems), and allows users to inject custom data. Currently at version 3.1.1, it is actively maintained by the pytest-dev team, with releases typically aligning with pytest compatibility and new feature additions.
pip install pytest-metadataVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to add custom metadata using a `pytest_configure` hook in `conftest.py`, how to modify the final metadata using the `pytest_metadata` hook, and how to access the collected metadata within a test function via the `metadata` fixture. It also shows how to add metadata via command-line arguments.
Upgrade to Python 3.8+ or use an older version of `pytest-metadata` (e.g., `pip install 'pytest-metadata<2.0.0'`).
Review the `pytest --help` output for the new grouped CLI options and update scripts accordingly. For example, `--metadata KEY VALUE` and `--metadata-from-json-file PATH`.
Use `from pytest_metadata.plugin import metadata_key` and access/modify metadata using `config.stash[metadata_key]['your_key'] = 'your_value'` in your `pytest_configure` hook.
Configure your `tox.ini` to pass through relevant CI environment variables using the `passenv` directive, e.g., `[testenv] passenv = CI BUILD_NUMBER` etc.
Always include `-v` or `--verbose` in your `pytest` command if you expect to see the collected metadata in the terminal report.
Remove any `import pytest_metadata` statements from your code. The plugin's functionality is accessed through pytest fixtures (like `request`) or hooks.
Ensure the `pytest-metadata` plugin is installed in your environment using `pip install pytest-metadata`. Verify your pytest setup allows plugin discovery (e.g., it's not disabled in `pytest.ini`).
Ensure all custom data added to `pytest_metadata` consists of JSON-compatible types (strings, numbers, booleans, lists, dictionaries). Convert complex objects to their serializable representations (e.g., `str()`, `repr()`, or a dictionary representation) before adding them.