Search RU
Tasks / pytest / Selecting test cases by pytest markers
Statement easy

Selecting test cases by pytest markers

`select_by_marker(cases, marker)` selects test cases by markers.

`cases` is a list of dicts:

```python
{"name": "test_login", "markers": ["smoke", "api"]}
```

`marker` is a string or a collection of strings (`list`, `tuple`, `set`). The
function returns a **new** list of cases in their original order.

Rules:

- markers are compared case-insensitively;
- when several markers are requested, a case must have **all** of them
(logical AND);
- a repeated marker in the request does not change the result;
- a case without the `markers` key is treated as a case with no markers;
- an empty collection of markers returns `[]`;
- if nothing matches, return `[]`;
- the input list and the cases themselves are not mutated;
- an invalid `marker` (neither a string nor a collection of strings) raises
`TypeError`.

**Example:**

```python
cases = [
{"name": "login", "markers": ["smoke", "api"]},
{"name": "health", "markers": ["smoke"]},
]

select_by_marker(cases, "smoke")
→ [{"name": "login", ...}, {"name": "health", ...}]

select_by_marker(cases, ["smoke", "api"])
→ [{"name": "login", ...}]
```

Write tests for: a single marker, the intersection of two markers, case
sensitivity, a case without markers, an empty request, no matches, and an
invalid argument type.

Python
pytestmarkersfilteringedge cases
test_solution.py Python 3.14
AI mentor

The AI mentor is available after you log in: it sees your attempt history and does not spend resources on anonymous requests.

Log in and ask
Hints 1 of 3 unlocked · attempts: 0
Hint 1

What does "the case matches" mean when several markers are requested: is one enough, or are all of them required? And what is the marker set of a case that has no `markers` key at all?

Hint 2 — unlocks after 1 attempt
Hint 3 — unlocks after 3 attempts