Package: pytest-metadata
pytest-metadata is a plugin forpytestthat provides
access to test session metadata.RequirementsYou will need the following in order to use pytest-metadata:Python 3.8+ or PyPy3InstallationTo install pytest-metadata:$pipinstallpytest-metadataContributingWe welcome contributions.To learn more, seeDevelopmentAvailable metadataThe following metadata is gathered by this plugin:KeyDescriptionExamplePythonPython version3.6.4PlatformPlatformDarwin-17.4.0-x86_64-i386-64bitPackagespytest packages{‘py’: ‘1.5.2’, ‘pytest’: ‘3.4.1’}Pluginspytest plugins{‘metadata’: ‘1.6.0’}Additional metadataYou can provide your own metadata (key, value pair) by specifying--metadataon the commandline:pytest --metadata foo barNote: You can provide multiple sets of--metadata:pytest --metadata foo bar --metadata baz zooThere’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.jsonContinuous integrationWhen 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.AppVeyorBitbucketCircleCIGitLab CIJenkinsTaskClusterTravis CINote that if you’re usingToxto run your tests
then you will need topass down any additional environment variablesfor these to be picked up.Viewing metadataIf you pass--verboseon 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.0Including metadata in Junit XMLPytest-metadata provides the session scoped fixtureinclude_metadata_in_junit_xmlthat you may use to include any metadata in Junit XML aspropertytags.
For example the following test moduleimportpytestpytestmark=pytest.mark.usefixtures('include_metadata_in_junit_xml')deftest():passwhen called withpytest--metadataDaffyDuck--junit-xml=results.xmlwould produce the following XML<?xml version="1.0" encoding="utf-8"?><testsuites><testsuitename="pytest"errors="0"failures="0"skipped="0"tests="1"time="0.009"timestamp="2020-11-27T06:38:44.407674"hostname="sam"><properties><propertyname="Daffy"value="Duck"/>...Accessing metadataTo add/modify/delete metadata at the end of metadata collection, you can use thepytest_metadatahook:importpytest@pytest.hookimpl(optionalhook=True)defpytest_metadata(metadata):metadata.pop("password",None)To access the metadata from a test or fixture, you can use themetadatafixture:deftest_metadata(metadata):assert'metadata'inmetadata['Plugins']To access the metadata from a plugin, you can use thestashattribute of
theconfigobject. This can be used to read/add/modify the metadata:defpytest_configure(config):metadata=config.pluginmanager.getplugin("metadata")ifmetadata:frompytest_metadata.pluginimportmetadata_keyconfig.stash[metadata_key]['foo']='bar'Plugin integrationsHere’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.ResourcesRelease NotesIssue TrackerCode