mark.rst

Overview

The [mark.rst](/projects/286/67332) file serves as a comprehensive guide and reference documentation for the **marking system** in the pytest testing framework. It explains how to apply metadata to test functions using pytest's `mark` feature, which is primarily used to tag tests with attributes such as `slow`, `skip`, `xfail`, or custom user-defined markers.

This documentation covers:

This file is primarily a user-facing documentation resource written in reStructuredText (reST) format, intended to help pytest users understand and utilize test marking effectively.


Detailed Explanation

What is a pytest Mark?

A **mark** in pytest is metadata attached to test functions or test classes to modify their behavior or provide additional information. For example, marks can be used to skip tests, run tests conditionally, or categorize tests for selective execution.


Builtin Markers

pytest provides several builtin markers, including but not limited to:

Users can list all available markers (builtin and custom) via the command line:

pytest --markers

Registering Custom Marks

To avoid warnings and enable better integration, custom marks should be **registered** in pytest’s configuration.

Via pytest.ini

Add a `markers` section:

[pytest]
markers =
    slow: marks tests as slow (deselect with '-m "not slow"')
    serial

Via pyproject.toml

[tool.pytest.ini_options]
markers = [
    "slow: marks tests as slow (deselect with '-m \"not slow\"')",
    "serial",
]

Programmatically in pytest_configure hook

def pytest_configure(config):
    config.addinivalue_line(
        "markers", "env(name): mark test to run only on named environment"
    )

Registered marks appear in pytest's help and suppress warnings about unknown marks.


Handling Unknown Marks

Unregistered marks used as decorators (e.g., `@pytest.mark.unknown`) generate warnings by default to prevent silent errors caused by typos or misconfiguration.

To enforce strict validation and convert these warnings to errors, use the `--strict-markers` flag:

[pytest]
addopts = --strict-markers
markers =
    slow: marks tests as slow (deselect with '-m "not slow"')
    serial

This ensures only registered marks are allowed, improving test suite robustness.


Usage Examples

import pytest

@pytest.mark.slow
def test_long_running():
    ...

@pytest.mark.skip(reason="Not implemented yet")
def test_feature():
    ...

@pytest.mark.parametrize("input,expected", [(1, 2), (3, 4)])
def test_addition(input, expected):
    assert add_one(input) == expected

Here, `slow` and `skip` are markers applied to tests, while `parametrize` runs the test multiple times with different inputs.


Important Implementation Details


Interactions with Other Parts of the System


Mermaid Diagram: pytest Marking System Structure

classDiagram
    class Mark {
        +name: str
        +description: str
        +apply(test_function)
    }

    class BuiltinMark {
        +usefixtures()
        +filterwarnings()
        +skip()
        +skipif(condition)
        +xfail(condition)
        +parametrize(params)
    }

    class CustomMark {
        +register(name, description)
    }

    class PytestConfig {
        +add_marker(name, description)
        +enable_strict_markers()
    }

    Mark <|-- BuiltinMark
    Mark <|-- CustomMark
    PytestConfig --> CustomMark : registers >
    PytestConfig --> BuiltinMark : manages >

Summary

The [mark.rst](/projects/286/67332) file is a vital reference documenting how pytest's marking system works. It educates users on the available builtin markers, guides on creating and registering custom markers, and clarifies the behavior when unknown markers are used. It also explains how markers integrate with pytest's configuration and testing lifecycle, enabling robust and flexible test management.

This documentation is essential for pytest users aiming to organize, categorize, and control test execution effectively using marks.