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:
Basic boolean logic expressions (
and,or,not) and their combinations.Syntax edge cases and error handling for malformed expressions.
Validation of identifier names in expressions.
Handling of keyword argument expressions related to pytest marks.
Ensuring that the mark expression parser correctly interprets string, numeric, and special character inputs.
Integration with
MarkMatcherobjects which represent collections of pytest marks, testing real-world matching scenarios.
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
Purpose: Parses and evaluates a mark expression string against a matcher function.
Parameters:
input: A string representing the mark expression to evaluate.matcher: A callable that accepts an identifier string and returns a boolean indicating if the identifier matches.
Returns: A boolean result of the evaluated expression.
Usage:
matcher = {"true": True, "false": False}.__getitem__
result = evaluate("true and not false", matcher) # returns True
Implementation Detail: This function compiles the expression using
Expression.compile()and then evaluates it with the provided matcher, casting the matcher toMatcherCallto align with expected types.
Test Functions
The file contains multiple test functions using `pytest` with detailed parameterized test cases.
1. test_empty_is_false()
Tests that empty or whitespace-only expressions evaluate to
Falseregardless of the matcher.
2. test_basic(expr: str, expected: bool)
Parameterized tests for basic boolean expressions using
trueandfalseidentifiers.Covers unary
not, binaryand/or, and parenthetical grouping.
3. test_syntax_oddities(expr: str, expected: bool)
Tests expressions with unusual spacing, excessive parentheses, and chained
notoperators.
4. test_backslash_not_treated_specially()
Verifies that backslashes (
\) in identifiers are treated as normal characters, not escape sequences.Checks that literal newlines cause parse errors, but escaped newlines (
\n) do not.
5. test_syntax_errors(expr: str, column: int, message: str)
Parameterized tests for various malformed expressions.
Validates that parsing throws
ParseErrorwith correct error messages and column positions.
6. test_valid_idents(ident: str)
Tests identifiers containing Unicode characters, digits, punctuation, and mixed content.
Validates that these are accepted as valid identifiers in expressions.
7. test_invalid_idents(ident: str)
Tests that identifiers containing special symbols (e.g.,
^,*,=, etc.) raise parsing errors.
8. test_invalid_kwarg_name_or_value(expr: str, expected_error_msg: str, mark_matcher: MarkMatcher)
Tests invalid keyword argument expressions in mark expressions.
Checks for reserved keywords, invalid identifiers, missing quotes, unsupported escaping, and unexpected characters.
Uses a pytest fixture
mark_matcherfor context.
9. test_keyword_expressions_with_numbers(expr: str, expected: bool, mark_matcher: MarkMatcher)
Tests keyword argument expressions with numeric values.
Validates correct matching against number values in marks.
10. test_builtin_matchers_keyword_expressions(expr: str, expected: bool, mark_matcher: MarkMatcher)
Tests keyword expressions against "builtin" marks with boolean and
Nonevalues.
11. test_str_keyword_expressions(expr: str, expected: bool, mark_matcher: MarkMatcher)
Tests keyword expressions with string values, including Unicode strings and empty strings.
Fixtures
mark_matcher() -> MarkMatcher
A pytest fixture scoped to the session.
Returns a
MarkMatcherinstance created from a list of pytest marks with various attributes.Used to test keyword expression matching against real mark objects.
Important Implementation Details
The tests rely heavily on
Expression.compile(input)from_pytest.mark.expressionto parse the mark expressions into an AST-like structure, which is then evaluated.MatcherCallis a type cast to indicate the matcher callable interface expected by the evaluation function.The system uses
ParseErrorexceptions to signal syntax problems in expressions, which are caught and asserted in tests.The test suite validates both syntactical correctness and semantic interpretation of mark expressions.
The use of
pytest.mark.parametrizeprovides extensive coverage of various input scenarios in a concise manner.The file tests both low-level expression syntax (identifiers, operators) and higher-level integration with pytest mark objects and their attributes.
Interaction with Other System Components
This file tests functionality from the
_pytest.mark.expressionmodule, which implements the parsing and evaluation logic for pytest mark expressions.It also indirectly interacts with
_pytest.mark.MarkMatcher, which represents collections of marks that can be matched against expressions.The tests ensure that the mark expression language used in pytest's mark selection and filtering works correctly.
This file is part of the pytest testing framework's internal testing suite, ensuring the quality and correctness of mark expression parsing and evaluation.
It depends on
pytestfor the test runner, fixtures, and marker syntax.
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.