Does pytest support markers by default?

I use pytest to test python models for embedded systems. The features tested are platform dependent. (I use the "platform" in this context to indicate the type of embedded system, not the type of OS).

The easiest way to organize my tests was to allocate them to directories based on the platform type.

/platform1
/platform2
/etc.

pytest / platform1

This quickly became difficult to maintain, as many features overlap between platforms. Since then, I have moved the tests to the same directory with checks for each functional area assigned to a single file name (for example, test_functionalityA.py). Then I use pytest tokens to indicate which tests in the file apply to this platform.

@pytest.mark.all_platforms
def test_some_functionalityA1():
    ...

@pytest.mark.platform1
@pytest.mark.platform2
def test_some_functionlityA2():
    ...

"conftest", , , , .

pytest -m "(platform1 all_platforms)"

: (!)

pytest , , , '-m' ?

: pytest -m "platform1"

@pytest.mark.platform1, , @pytest.mark.all_platforms, @pytest.mark ?

, @pytest.mark.all_platforms .

+6
2

. , conftest.py , , ( , , ). sys.platform, , .

# content of conftest.py
#
import sys
import pytest

ALL = set("osx linux2 win32".split())

def pytest_runtest_setup(item):
    if isinstance(item, item.Function):
        plat = sys.platform
        if not hasattr(item.obj, plat):
            if ALL.intersection(set(item.obj.__dict__)):
                pytest.skip("cannot run on platform %s" %(plat))

:

# content of test_plat.py

import pytest

@pytest.mark.osx
def test_if_apple_is_evil():
    pass

@pytest.mark.linux2
def test_if_linux_works():
    pass

@pytest.mark.win32
def test_if_win32_crashes():
    pass

def test_runs_everywhere_yay():
    pass

::

$ py.test -rs

::

:

$ py.test -rs # this option reports skip reasons
=========================== test session starts ============================
platform linux2 -- Python 2.7.3 -- pytest-2.2.5.dev1
collecting ... collected 4 items

test_plat.py s.s.
========================= short test summary info ==========================
SKIP [2] /home/hpk/tmp/doc-exec-222/conftest.py:12: cannot run on platform linux2

=================== 2 passed, 2 skipped in 0.01 seconds ====================

, -, this:

$ py.test -m linux2
=========================== test session starts ============================
platform linux2 -- Python 2.7.3 -- pytest-2.2.5.dev1
collecting ... collected 4 items

test_plat.py .

=================== 3 tests deselected by "-m 'linux2'" ====================
================== 1 passed, 3 deselected in 0.01 seconds ==================

. , .

+9

, , .

: -m, conftest.py

def pytest_collection_modifyitems(items, config):
    # add 'always_run' marker to all unmarked items
    for item in items:
        if not any(item.iter_markers()):
            item.add_marker("always_run")
    # Ensure the 'always_run' marker is always selected for
    markexpr = config.getoption("markexpr", 'False')
    config.option.markexpr = f"always_run or ({markexpr})"
0

All Articles