Pytest document 43 - Metadata usage (pytest metadata)

Posted by chauffeur on Sat, 22 Jan 2022 18:41:46 +0100

preface

What is metadata? Metadata is a description of data, which stores information about data and provides help for people to retrieve information more conveniently.
The metadata in the pytest framework can be implemented using the pytest metadata plug-in. Document address https://pypi.org/project/pytest-metadata/

Pytest metadata environment preparation

Installing pytest metadata using pip

pip install pytest-metadata

View pytest metadata

When using pytest to execute the use case, add the - v parameter (or -- verbose), and metadata will be output in the header of the console output report

>pytest --verbose
============================= test session starts =============================
platform win32 -- Python 3.6.0, pytest-4.5.0, py-1.5.4, pluggy-0.13.1 -- e:\python36\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.6.0', 'Platform': 'Windows-10-10.0.17134-SP0', 'Packages': {'pytest': '4.5.0', 'py': '1.5.4', 'pluggy': '0.13.1'}, 'Plugins': {'allure-pytest': '2.8.6', 'PyTestReport': '0.1.9.3', 'assume': '2.2.1', 'forked': '0.2', 'html': '1.19.0', 'metadata': '1.7.0', 'ordering': '0.6', 'repeat': '0.7.0', 'rerunfailures': '8.0', 'xdist': '1.23.2'}, 'JAVA_HOME': 'D:\\java\\jdk1.8'}
rootdir: D:\soft\code\pytest_api_2020_03
plugins: allure-pytest-2.8.6

Available metadata

KeyDescriptionExample
PythonPython version'3.6.0'
PlatformOperation platform'Windows-10-10.0.17134-SP0'
Packagespytest package related information{'pytest': '4.5.0', 'py': '1.5.4', 'pluggy': '0.13.1'}
Pluginspytest plug-in{'allure-pytest': '2.8.6', 'PyTestReport': '0.1.9.3'}
JAVA_HOMEJAVA environment variables'D:\java\jdk1.8'

Metadata is stored in key value pairs

Add metadata

We can add the metadata of key value pairs on the command line with the -- metadata parameter.
For example, when we finish a project and need to add author information, we can add metadata

pytest --metadata auther yoyo

If you need to add multiple metadata, you can use the -- metadata parameter multiple times to add metadata

pytest --metadata auther yoyo --metadata version v1.0

From the document point of view, it can support json format, transfer multiple groups of metadata at one time, and use -- metadata from json, but I tried it myself and did not support this parameter. This method can be ignored!

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

pytest_metadata hook function

You can also add / modify / delete metadata in the code. We can use pytest_metadata hook function

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

We can use a metadata fixture to access metadata for test cases or fixtures

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

Access metadata in the plug-in, which can be used in the config object_ Metadata property to add / modify / delete metadata

def pytest_configure(config):
  if hasattr(config, '_metadata'):
      config._metadata['foo'] = 'bar'

Plug in integration

The following is a convenient list of plug-ins that either read metadata or contribute to metadata:

  • pytest-base-url - Adds the base URL to the metadata.
  • pytest-html - Displays the metadata at the start of each report.
  • pytest-selenium - Adds the driver, capabilities, and remote server to the metadata.

pytest.ini management metadata

If more metadata is added and it is inconvenient to enter it on the command line, you can enter it in pytest Configure your project metadata in ini configuration

# pytest.ini
# Author - Shanghai youyou QQ exchange group: 717225969
# blog address https://www.cnblogs.com/yoyoketang/

[pytest]
addopts = -v 
  --html=report.html 
  --self-contained-html
  --metadata auther yoyo 
  --metadata version v1.0

Topics: pytest