Skip to content

Instantly share code, notes, and snippets.

@fitzy1321
Last active January 10, 2023 02:50
Show Gist options
  • Save fitzy1321/0cf4c625aa58a5f11d8124062d73bd30 to your computer and use it in GitHub Desktop.
Save fitzy1321/0cf4c625aa58a5f11d8124062d73bd30 to your computer and use it in GitHub Desktop.
Pytest slow test marker

Mark a test as slow and skip it

# In pyprojct.toml
[tool.pytest.ini-options]
markers = ["slow: mark tests as slow (deselect with '--runslow')"]

Add the following to your conftest.py file:

def pytest_addoption(parser):
    parser.addoption(
        "--runslow", action="store_true", default=False, help="run slow tests"
    )
    
def pytest_collection_modifyitems(config, items):
    if config.getoption("--runslow"):
        return
    skip_slow = pytest.mark.skip(reason="need --runslow option to sun")
    for item in items:
        if "slow" in item.keywords:
            item.add_marker(skip_slow)

In your test file:

import pytest

@pytest.mark.slow
def test_something_slow():
    pass

Run pytest without the --runslow flag. Any marked test will be skipped.

To run slow tests, run pytest --runslow .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment