pytest mark slow skip integration

Uses pytest markers to select fast tests, skip unfinished ones, and run integration checks with verbose output.

Easy Python 3.9+ Aug 9, 2026 Modern tooling 17 views 0 copies

Requires third-party packages — install first
pip install pytest

Python code

24 lines
Python 3.9+
import pytest

def test_fast():
    assert 1 + 1 == 2

@pytest.mark.slow
def test_slow():
    import time
    time.sleep(1)
    assert 5 * 5 == 25

@pytest.mark.skip(reason="Not ready for production")
def test_skipped():
    assert 2 + 2 == 5

@pytest.mark.integration
def test_integration():
    database = {"users": [{"id": 1, "name": "Alice"}]}
    user = database["users"][0]
    assert user["id"] == 1
    assert user["name"] == "Alice"

if __name__ == "__main__":
    pytest.main([__file__, "-v", "-m", "not slow"])

Output

stdout
============================= test session starts ==============================
platform linux -- Python 3.11.4, pytest-8.0.0, pluggy-1.4.0
rootdir: /path/to/test
collected 4 items

test_demo.py .s.                                                     [100%]

============================== 2 passed, 1 skipped, 1 deselected ==============

How it works

The @pytest.mark.slow and @pytest.mark.integration decorators tag tests with custom markers. Running with -m "not slow" deselects the slow test. The @pytest.mark.skip decorator prevents execution of the skipped test and reports it as skipped. pytest.main() runs the test file programmatically.

Common mistakes

  • Forgetting to register custom markers in pytest.ini to avoid warnings
  • Using `-m slow` without `not` to exclude slow tests
  • Placing `pytest.main()` inside `if __name__ == "__main__"` but not passing the file path
  • Assuming `@pytest.mark.integration` runs only integration tests without `-m`

Variations

  1. Use `-m "not slow and not integration"` to exclude both slow and integration tests
  2. Define markers in `pyproject.toml` under `[tool.pytest.ini_options]` with `markers = [...]`

Real-world use cases

  • Running fast unit tests in CI while deferring slow integration tests to nightly runs.
  • Skipping tests for features not yet deployed by marking them with `skip` and a reason.
  • Tagging API or database tests as `integration` to run them against a live staging environment.

Sponsored

Run locally

This sample needs third-party packages, so it cannot run in the browser IDE. Copy the code above, install the packages shown at the top, then run it in your own Python environment.

More from Modern tooling

Related tutorials and quizzes for this topic.