Skip to main content

pytest-metadata

pytest-metadata is a plugin for pytest that provides access to test session metadata.

License PyPI https://img.shields.io/travis/pytest-dev/pytest-metadata.svg Travis Issues Requirements

Requirements

You will need the following in order to use pytest-metadata:

  • Python 3.8+ or PyPy3

Installation

To install pytest-metadata:

$ pip install pytest-metadata

Contributing

We welcome contributions.

To learn more, see Development

Available metadata

The following metadata is gathered by this plugin:

Key

Description

Example

Python

Python version

3.6.4

Platform

Platform

Darwin-17.4.0-x86_64-i386-64bit

Packages

pytest packages

{‘py’: ‘1.5.2’, ‘pytest’: ‘3.4.1’}

Plugins

pytest plugins

{‘metadata’: ‘1.6.0’}

Additional metadata

You can provide your own metadata (key, value pair) by specifying --metadata on the commandline:

pytest --metadata foo bar

Note: You can provide multiple sets of --metadata:

pytest --metadata foo bar --metadata baz zoo

There’s also the possibility of passing in metadata as a JSON string:

pytest --metadata-from-json '{"cat_says": "bring the cat nip", "human_says": "yes kitty"}'

Alternatively a JSON can be read from a given file:

pytest --metadata-from-json-file path/to/valid/file.json

Continuous integration

When run in a continuous integration environment, additional metadata is added from environment variables. Below is a list of the supported continuous integration providers, along with links to the environment variables that are added to metadata if they’re present.

Note that if you’re using Tox to run your tests then you will need to pass down any additional environment variables for these to be picked up.

Viewing metadata

If you pass --verbose on the command line when running your tests, then the metadata will be displayed in the terminal report header:

pytest --verbose
============================ test session starts ============================
platform darwin -- Python 3.6.4, pytest-3.4.1, py-1.5.2, pluggy-0.6.0 -- /usr/bin/python
cachedir: .pytest_cache
metadata: {'Python': '3.6.4', 'Platform': 'Darwin-17.4.0-x86_64-i386-64bit', 'Packages': {'pytest': '3.4.1', 'py': '1.5.2', 'pluggy': '0.6.0'}, 'Plugins': {'metadata': '1.6.0'}}
plugins: metadata-1.6.0

Including metadata in Junit XML

Pytest-metadata provides the session scoped fixture include_metadata_in_junit_xml that you may use to include any metadata in Junit XML as property tags. For example the following test module

import pytest

pytestmark = pytest.mark.usefixtures('include_metadata_in_junit_xml')

def test():
    pass

when called with

pytest --metadata Daffy Duck --junit-xml=results.xml

would produce the following XML

<?xml version="1.0" encoding="utf-8"?>
<testsuites>
  <testsuite name="pytest" errors="0" failures="0" skipped="0" tests="1" time="0.009" timestamp="2020-11-27T06:38:44.407674" hostname="sam">
    <properties>
      <property name="Daffy" value="Duck"/>
...

Accessing metadata

To add/modify/delete metadata at the end of metadata collection, you can use the pytest_metadata hook:

import pytest
@pytest.hookimpl(optionalhook=True)
def pytest_metadata(metadata):
    metadata.pop("password", None)

To access the metadata from a test or fixture, you can use the metadata fixture:

def test_metadata(metadata):
    assert 'metadata' in metadata['Plugins']

To access the metadata from a plugin, you can use the stash attribute of the config object. This can be used to read/add/modify the metadata:

def pytest_configure(config):
  metadata = config.pluginmanager.getplugin("metadata")
  if metadata:
      from pytest_metadata.plugin import metadata_key
      config.stash[metadata_key]['foo'] = 'bar'

Plugin integrations

Here’s a handy list of plugins that either read or contribute to the metadata:

Resources

Release files for pytest-metadata 3.1.1

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-metadata 3.1.1
File Size Uploaded
pytest_metadata-3.1.1.tar.gz 10.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pytest-metadata 3.1.1
File Interpreter ABI Platform
pytest_metadata-3.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 21.4 kB

Release files / pytest_metadata-3.1.1.tar.gz

Download URL pytest_metadata-3.1.1.tar.gz
Size 10.0 kB
Tags Source
SHA-256 checksum
How to use checksums
d2a29b0355fbc03f168aa96d41ff88b1a3b44a3b02acbe491801c98a048017c8
BLAKE2b-256 checksum
How to use checksums
a6858c969f8bec4e559f8f2b958a15229a35495f5b4ce499f6b865eac54b878d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.8

Release files / pytest_metadata-3.1.1-py3-none-any.whl

Download URL pytest_metadata-3.1.1-py3-none-any.whl
Size 11.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c8e0844db684ee1c798cfa38908d20d67d0463ecb6137c72e91f418558dd5f4b
BLAKE2b-256 checksum
How to use checksums
3e437e7b2ec865caa92f67b8f0e9231a798d102724ca4c0e1f414316be1c1ef2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.8

Release history Release notifications | RSS feed

This release

3.1.1 This release

2 release files

3.1.0

2 release files

3.0.0

2 release files

2.0.4

2 release files

2.0.3

2 release files

2.0.2

2 release files

2.0.1

2 release files

2.0.0

2 release files

1.11.0

2 release files

1.10.0

2 release files

1.9.0

2 release files

1.8.0

2 release files

1.7.0

2 release files

1.6.0

2 release files

1.5.1

2 release files

1.5.0

2 release files

1.4.0

2 release files

1.3.0

2 release files

1.2.0

2 release files

1.1.0

2 release files

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