test_mark_expression.py

Overview

This file contains a comprehensive suite of unit tests for evaluating and verifying the correctness of the **mark expression** parsing and evaluation system used in the pytest framework. The core functionality tested here revolves around parsing logical expressions involving pytest marks and evaluating them against given matchers to determine truth values.

The tests cover:

This testing module directly exercises the [_pytest.mark.expression.Expression](/projects/286/67346) class and related constructs, ensuring robust parsing and evaluation logic for pytest's mark expressions feature.


Key Functions

evaluate(input: str, matcher: Callable[[str], bool]) -> bool

matcher = {"true": True, "false": False}.__getitem__
result = evaluate("true and not false", matcher)  # returns True

Test Functions

The file contains multiple test functions using `pytest` with detailed parameterized test cases.

1. test_empty_is_false()

2. test_basic(expr: str, expected: bool)

3. test_syntax_oddities(expr: str, expected: bool)

4. test_backslash_not_treated_specially()

5. test_syntax_errors(expr: str, column: int, message: str)

6. test_valid_idents(ident: str)

7. test_invalid_idents(ident: str)

8. test_invalid_kwarg_name_or_value(expr: str, expected_error_msg: str, mark_matcher: MarkMatcher)

9. test_keyword_expressions_with_numbers(expr: str, expected: bool, mark_matcher: MarkMatcher)

10. test_builtin_matchers_keyword_expressions(expr: str, expected: bool, mark_matcher: MarkMatcher)

11. test_str_keyword_expressions(expr: str, expected: bool, mark_matcher: MarkMatcher)


Fixtures

mark_matcher() -> MarkMatcher


Important Implementation Details


Interaction with Other System Components


Visual Diagram: Class and Function Overview

flowchart TD
    A[evaluate(input:str, matcher:Callable)] -->|calls| B[Expression.compile(input)]
    B --> C[Expression.evaluate(matcher)]

    subgraph Tests
        D[test_empty_is_false()]
        E[test_basic(expr, expected)]
        F[test_syntax_oddities(expr, expected)]
        G[test_backslash_not_treated_specially()]
        H[test_syntax_errors(expr, column, message)]
        I[test_valid_idents(ident)]
        J[test_invalid_idents(ident)]
        K[test_invalid_kwarg_name_or_value(expr, expected_error_msg, mark_matcher)]
        L[test_keyword_expressions_with_numbers(expr, expected, mark_matcher)]
        M[test_builtin_matchers_keyword_expressions(expr, expected, mark_matcher)]
        N[test_str_keyword_expressions(expr, expected, mark_matcher)]
    end

    subgraph Fixture
        O[mark_matcher() -> MarkMatcher]
    end

    K --> O
    L --> O
    M --> O
    N --> O

Summary

`test_mark_expression.py` is a critical test suite verifying the parsing and evaluation of pytest's mark expression language. It rigorously checks syntax rules, identifier validity, logical operators, error handling, and matching behavior against real pytest marks. By exercising the [_pytest.mark.expression.Expression](/projects/286/67346) class and integrating with `MarkMatcher`, it ensures that mark expressions perform as expected in pytest's mark filtering and selection mechanisms. The extensive parameterized tests provide robust coverage, supporting pytest's reliability and usability for complex test selection criteria.