Skip to main content
Security Status https://img.shields.io/badge/code%20style-black-000000.svg

Using pytest markers to track functional coverage and filtering of tests


This pytest plugin was generated with Cookiecutter along with @hackebrot’s cookiecutter-pytest-plugin template.

Features

  • Definition of CoverageMarkers© in YAML format

  • Support for applying CoverageMarkers© to tests

  • Filtering of tests based on CoverageMarkers©

  • Inclusion of CoverageMarkers© in JSON report

Installation

You can install “pytest-coveragemarkers” from PyPI:

$ pip install pytest-coveragemarkers
# or
$ poetry add pytest-coveragemarkers

Usage

Step 1: Define your coverage markers yaml.

Using the format:

markers:
  - name: <marker_name>
    allowed:
      - <marker_value_1>
      - <marker_value_2>
  - name: <marker2_name>
    allowed:
      - <marker2_value_1>
      - <marker2_value_2>

Then decorate your tests with them

import pytest

@pytest.mark.marker_name(['value1', 'value2'])
@pytest.mark.marker2_name(['value1', 'value2'])
def test_x():
    ...

@pytest.mark.marker2_name(['value1', 'value2'])
def test_y():
    ...

Then when the tests are executed with

pytest --json-report --markers-location=/full/path/to/coverage_markers.yml

Then the JSON Test Report output from the test execution contains:

"tests": [
{
  "nodeid": "...",
  "metadata": {
    "cov_markers": {
      "marker_name": {
        "value1": true,
        "value2": true
      },
      "marker2_name": {
        "value1": true,
        "value2": true
      }
    }
  }
},
...
]

This can then be used to generate test coverage details based on the coverage markers. A nice demo will be produced to give examples of usage.

But wait there is another benefit:

We can filter tests for execution based on their coverage markers

pytest \
    --filter='"value1" in marker_name' \
    --json-report \
    --markers-location=/full/path/to/coverage_markers.yml

The above command run against the tests defined above would select ‘test_x’ and deselect ‘test_y’ for execution

Other examples of filters are:

You can also supply the path to a file containing your filter. Use argument –filter-location or key FilterLocation in the pytest.ini file.

Mandatory Coverage Markers

Coverage markers can be detailed as mandatory by including the mandatory attribute.

E.g.

markers:
  - name: <marker_name>
    mandatory: True
    allowed:
      - <marker_value_1>
      - <marker_value_2>

Dependent Coverage Markers

Coverage markers can be detailed as a dependency on another marker. This ensures that if a marker is specified all dependencies of this marker in the chain must also be specified.

E.g.

markers:
  - name: <marker_name>
    dependents:
      - <marker_name...>
      - <marker_name...>
    allowed:
      - <marker_value_1>
      - <marker_value_2>

Coverage Marker Argument Format

The arguments supplied to Coverage Markers can follow multiple formats which allows the user to define the format that best suites them.

E.g.

import pytest

@pytest.mark.marker_1('value1')                 # single string argument
@pytest.mark.marker_2('value1', 'value2')       # multiple string arguments
@pytest.mark.marker_3(['value1', 'value2'])     # list of arguments
@pytest.mark.marker_4(('value1', 'value2'))     # tuple of arguments
def test_x():
    ...

Testing

Nox is used by this project to execute all tests. To run a specific set of tests execute the below line:

$ poetry run nox -s <session_name>

Where session_name can be one of the following

Nox Sessions

Session Name

Session Details

unit_tests

Execute all tests marked as unit

functional_tests

Execute all tests marked as functional

Thought Process

  • The pytest_docs talks about using markers to set metadata on tests and use the markers to select required tests for execution.

  • For the markers I want to add, I also want to specify a list of values that go along with that marker. E.g. If the marker was ‘colour’ then supported values may be ‘Red’, ‘Green’, ‘Gold’.

  • I also want the list of values validated against supported values so no unsupported values can be added. E.g. If the marker was ‘colour’ then a value of ‘Panda’ would not be allowed.

  • Then all this meta data I want to come out in the junit json report.

  • Next I want to use these markers and their supported values to filter tests. For this I need a more powerful filter engine.

Documentation

To build the docs run:

poetry run mkdocs serve

License

Distributed under the terms of the MIT license, “pytest-coveragemarkers” is free and open source software

Issues

If you encounter any problems, please file an issue along with a detailed description.

Future Changes

  • Type-Hints

  • Full Test Coverage

  • Full Documentation

Metadata

Release files for pytest-coveragemarkers 3.3.3

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-coveragemarkers 3.3.3
File Size Uploaded Provenance
pytest_coveragemarkers-3.3.3.tar.gz 12.8 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for pytest-coveragemarkers 3.3.3
File Interpreter ABI Platform
pytest_coveragemarkers-3.3.3-py3-none-any.whl Python 3 none any Details

Total release size: 28.8 kB

Release files / pytest_coveragemarkers-3.3.3.tar.gz

Download URL pytest_coveragemarkers-3.3.3.tar.gz
Size 12.8 kB
Tags Source
SHA-256 checksum
How to use checksums
c4142a5d5eff9e255fedd10e55a6dc3679c2a3a78cffba4b253aeaf560719953
BLAKE2b-256 checksum
How to use checksums
6c4c7c6408393b9486a694b66d074edfdb7a175f7523db8ee5310f15f41aebdf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.1.1 CPython/3.12.3 Darwin/22.6.0

Release files / pytest_coveragemarkers-3.3.3-py3-none-any.whl

Download URL pytest_coveragemarkers-3.3.3-py3-none-any.whl
Size 16.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
416a483e92551dca5903c7acf11e27d0fce5059e893f536cee37e575d7ffe1e7
BLAKE2b-256 checksum
How to use checksums
0e27443c4c1bb220cf572b76d238d5ba614e178cabcf35278b258f3955f9de27
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.1.1 CPython/3.12.3 Darwin/22.6.0

Security for pytest-coveragemarkers 3.3.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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