Sign inDemoInstall


Package Overview
File Explorer

Install Socket

Detect and block malicious and high-risk dependencies



pytest plugin for test session metadata




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

.. image:: :target: :alt: License .. image:: :target: :alt: PyPI .. image:: :target: .. image:: :target: :alt: Travis .. image:: :target: :alt: Issues .. image:: :target: :alt: Requirements


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

  • Python 3.8+ or PyPy3


To install pytest-metadata:

.. code-block:: bash

$ pip install pytest-metadata


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.

  • AppVeyor <>_
  • Bitbucket <>_
  • CircleCI <>_
  • GitLab CI <>_
  • Jenkins <>_
  • TaskCluster <>_
  • Travis CI <>_

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 :code: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

.. code-block:: python

import pytest

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

def test(): pass

when called with

.. code-block:: bash

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

would produce the following XML

.. code-block:: xml


Accessing metadata

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

.. code-block:: python

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:

.. code-block:: python

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:

.. code-block:: python

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:

  • pytest-base-url <>_ - Adds the base URL to the metadata.
  • pytest-html <>_ - Displays the metadata at the start of each report.
  • pytest-reporter-html1 <>_ - Presents metadata as part of the report.
  • pytest-selenium <>_ - Adds the driver, capabilities, and remote server to the metadata.


  • Release Notes <>_
  • Issue Tracker <>_
  • Code <>_



Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.


Related posts

SocketSocket SOC 2 Logo


  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc