Tips and Tricks - Create Custom Pytest Markers

Pytest provides you with the capability to create custom markers in order to select or deselect tests more gradually. Suppose you want to mark all command-line interface-related tests with a custom cli marker. To do so, you need to register the marker first in your pytest.ini as follows:

[pytest]
markers =
    cli: mark a test as cli-related.

Now, you can use the marker cli to mark all command-line interface related tests with it as follows:

import pytest


@pytest.mark.cli
def test_some_cli_test():
    pass

Selecting marked tests is as easy as running:

$ pytest -m cli

... or deselecting them by using pytest's "not"-keyword:

$ pytest -m "not cli"

Reference: Marking test functions and selecting them for a run

Groups: pytest