pytest-dev / pytest-dev/pytest

Public way to test if a matchexpr will match a marker string or Item

Open
#13,759 2 comments 0 reactions 1 assignee View on GitHub

@gomri15 is already working on this.

Since Sep 29, 2025.

topic: marks type: proposal
Dominant language
Python
Stars
14.5k
Forks
3.4k
Avg merge
2d 9h
Merged PRs (30d)
35

Description

I have a handful of tools in and outside of a pytest run that care about whether the matchexpr (i.e. what gets passed to pytest -m ...) will match a specific marker (i.e. the string representation of a mark, a la @pytest.mark.my_mark) or an Item decorated with such a marker.

Currently I've solved this problem with something like

def matchmark(markexpr: str, markers: str | Sequence[str] | pytest.Item) -> bool:
    from pytest import Item
    from _pytest.mark import Expression, MarkMatcher

    expression = Expression.compile(markexpr)

    if isinstance(markers, Item):
        matcher = MarkMatcher.from_item(markers)
    elif isinstance(markers, str):
        matcher = MarkMatcher([markers])
    else:
        matcher = MarkMatcher(set(markers))

    return expression.evaluate(matcher)

But this is reaching in to private stuff you can and will break at any time (I think this code breaks after pytest 8.2). It would be nice if there were a supported public method to answer this question.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.