pytest mark slow skip integration
Uses pytest markers to select fast tests, skip unfinished ones, and run integration checks with verbose output.
Requires third-party packages — install first
pip install pytest
Python code
24 linesimport 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
============================= 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
- Use `-m "not slow and not integration"` to exclude both slow and integration tests
- 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
More from Modern tooling
- Build a Recipe Runner Mock in Python easy
- Build a Textual TUI App Skeleton in Python easy
- Configure ruff linter rules in pyproject.toml with Python easy
- Data Conversion Helper Functions in Python easy
- How to Bind and Mock structlog Context in Python medium
- How to Build a Chainable Filter Helper in Python easy
Keep learning
Related tutorials and quizzes for this topic.