|
| 1 | +# testing/test_match_markexpr.py |
| 2 | +from __future__ import annotations |
| 3 | + |
| 4 | +from _pytest.config.exceptions import UsageError |
| 5 | +from _pytest.mark.expression import ParseError |
| 6 | +import pytest |
| 7 | + |
| 8 | + |
| 9 | +def test_public_api_present_match_markexpr() -> None: |
| 10 | + assert hasattr(pytest, "match_markexpr") |
| 11 | + assert callable(pytest.match_markexpr) |
| 12 | + |
| 13 | + |
| 14 | +def test_strings_markexpr() -> None: |
| 15 | + assert pytest.match_markexpr("smoke or slow", "slow") |
| 16 | + assert not pytest.match_markexpr("smoke and slow", "smoke") |
| 17 | + |
| 18 | + |
| 19 | +def test_iterables_markexpr() -> None: |
| 20 | + assert pytest.match_markexpr("smoke and not slow", ["smoke"]) |
| 21 | + assert not pytest.match_markexpr("smoke and not slow", ["smoke", "slow"]) |
| 22 | + assert pytest.match_markexpr("a and b", ["a", "b"]) |
| 23 | + assert not pytest.match_markexpr("a and b", ["a"]) |
| 24 | + |
| 25 | + |
| 26 | +def test_invalid_expression_raises() -> None: |
| 27 | + with pytest.raises( |
| 28 | + ParseError, match="expected not OR left parenthesis OR identifier; got and" |
| 29 | + ): |
| 30 | + pytest.match_markexpr("smoke and and slow", ["smoke"]) |
| 31 | + |
| 32 | + |
| 33 | +def test_type_error_for_unsupported_target() -> None: |
| 34 | + with pytest.raises( |
| 35 | + UsageError, |
| 36 | + match="target must be an Item-like object, a marker name string, or an iterable of marker names", |
| 37 | + ): |
| 38 | + pytest.match_markexpr("smoke", 123) |
| 39 | + |
| 40 | + |
| 41 | +def test_item_matching_with_request_node(request: pytest.FixtureRequest) -> None: |
| 42 | + request.config.addinivalue_line("markers", "smoke: test marker") |
| 43 | + request.config.addinivalue_line("markers", "slow: test marker") |
| 44 | + |
| 45 | + # Use the current test item; add/remove marks dynamically. |
| 46 | + request.node.add_marker("smoke") |
| 47 | + assert pytest.match_markexpr("smoke and not slow", request.node) |
| 48 | + assert not pytest.match_markexpr("slow", request.node) |
| 49 | + |
| 50 | + # Add another mark and re-check |
| 51 | + request.node.add_marker("slow") |
| 52 | + assert not pytest.match_markexpr("smoke and not slow", request.node) |
| 53 | + assert pytest.match_markexpr("smoke and slow", request.node) |
0 commit comments