test_mark.py


Overview

[test_mark.py](/projects/286/67332) is a comprehensive test suite for the `pytest` testing framework, focusing primarily on the functionality and behavior of **pytest markers** and **keyword selection** features. Markers in pytest are used to add metadata to test functions and classes, enabling conditional test execution, categorization, and parameterization.

This file contains numerous unit and functional tests that validate:

The tests utilize `pytest`'s own testing utilities, especially the [Pytester](/projects/286/67470) helper fixture, to dynamically create test files, run pytest programmatically, and assert outcomes.


Detailed Explanation of Classes and Functions

Class: TestMark

This class groups tests that check basic marker functionality, such as:

Key Methods:


Function: test_marked_class_run_twice(pytester: Pytester) -> None

Tests a regression where a file containing a class marked with `@pytest.mark.parametrize` runs twice unexpectedly. It creates a test file with a parameterized class and verifies that all tests pass exactly once.


Functions Testing Marker Configuration and Command-line Options


Parameterized Tests for Marker Expression Parsing (-m option)


Class: TestFunctional

Contains more integration/functional style tests verifying marker behavior in more complex scenarios, including:

Notable Methods:


Class: TestKeywordSelection

Tests related to the keyword expression selection feature (`-k` option):


Class: TestMarkDecorator

Tests behavior of marker decorator instances:


Various Independent Test Functions


Important Implementation Details and Algorithms


Interaction with Other System Components


Usage Examples

Example 1: Using Markers in Tests

import pytest

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

@pytest.mark.parametrize("x", [1, 2, 3])
def test_param(x):
    assert x in [1, 2, 3]

Example 2: Running Tests with Marker Expressions

Run only tests marked as `slow`:

pytest -m slow

Run tests marked `car` with `color='red'`:

pytest -m "car(color='red')"

Example 3: Filtering Tests by Keywords (-k)

Run tests whose names match `test_interface` or exclude those:

pytest -k interface
pytest -k "not interface"

Mermaid Class Diagram of TestMark Class and Related Test Classes

classDiagram
    class TestMark {
        +test_pytest_exists_in_namespace_all(attr: str)
        +test_pytest_mark_notcallable()
        +test_mark_with_param()
        +test_pytest_mark_name_starts_with_underscore()
    }

    class TestFunctional {
        +test_merging_markers_deep(pytester)
        +test_mark_decorator_subclass_does_not_propagate_to_base(pytester)
        +test_mark_should_not_pass_to_siebling_class(pytester)
        +test_mark_decorator_baseclasses_merged(pytester)
        +test_mark_closest(pytester)
        +test_mark_with_wrong_marker(pytester)
        +test_mark_dynamically_in_funcarg(pytester)
        +test_no_marker_match_on_unmarked_names(pytester)
        +test_keywords_at_node_level(pytester)
        +test_keyword_added_for_session(pytester)
        +assert_markers(items, **expected)
        +test_mark_from_parameters(pytester)
        +test_reevaluate_dynamic_expr(pytester)
    }

    class TestKeywordSelection {
        +test_select_simple(pytester)
        +test_select_extra_keywords(pytester, keyword)
        +test_keyword_extra(pytester)
        +test_no_magic_values(pytester, keyword)
        +test_no_match_directories_outside_the_suite(pytester, monkeypatch)
    }

    class TestMarkDecorator {
        +test__eq__(lhs, rhs, expected)
        +test_aliases()
    }

Summary

The [test_mark.py](/projects/286/67332) file is a critical part of the pytest test suite that ensures the robustness, correctness, and expected behavior of pytest's marker and keyword selection features. It covers a wide array of scenarios, from basic marker application to complex expression parsing and dynamic marker interactions with fixtures and test collection. This suite helps maintain pytest's flexible and powerful test selection capabilities, crucial for users managing large and complex test bases.


End of Documentation for test_mark.py