Help us improve
Share bugs, ideas, or general feedback.
From claude-mods
Provides pytest patterns for Python testing: fixtures, parametrization, mocking, markers, and exception testing. Useful when writing or reviewing unit/integration tests.
npx claudepluginhub 0xdarkmatter/claude-mods --plugin claude-modsHow this skill is triggered — by the user, by Claude, or both
Slash command
/claude-mods:python-pytest-opsThis skill is limited to the following tools:
The summary Claude sees in its skill listing — used to decide when to auto-load this skill
Modern pytest patterns for effective testing.
assets/conftest.py.templateassets/pytest.ini.templatereferences/async-testing.mdreferences/coverage-strategies.mdreferences/fixtures-advanced.mdreferences/integration-testing.mdreferences/mocking-patterns.mdreferences/property-testing.mdreferences/test-architecture.mdscripts/generate-conftest.shscripts/run-tests.shpytest testing framework conventions and practices. Invoke whenever task involves any interaction with pytest — writing tests, configuring pytest, fixtures, parametrize, mocking, debugging test failures, or coverage.
Implements advanced pytest patterns for fixtures with scopes and factories, markers, parametrization, parallel execution with xdist, and coverage with pytest-cov. Use for test infrastructure, suite organization, and async testing.
Guides Python testing with pytest: TDD cycle, fixture patterns, mocking, parametrization, and 80%+ coverage targets. Activates when writing Python tests or setting up coverage infrastructure.
Share bugs, ideas, or general feedback.
Modern pytest patterns for effective testing.
import pytest
def test_basic():
"""Simple assertion test."""
assert 1 + 1 == 2
def test_with_description():
"""Descriptive name and docstring."""
result = calculate_total([1, 2, 3])
assert result == 6, "Sum should equal 6"
import pytest
@pytest.fixture
def sample_user():
"""Create test user."""
return {"id": 1, "name": "Test User"}
@pytest.fixture
def db_connection():
"""Fixture with setup and teardown."""
conn = create_connection()
yield conn
conn.close()
def test_user(sample_user):
"""Fixtures injected by name."""
assert sample_user["name"] == "Test User"
@pytest.fixture(scope="function") # Default - per test
@pytest.fixture(scope="class") # Per test class
@pytest.fixture(scope="module") # Per test file
@pytest.fixture(scope="session") # Entire test run
@pytest.mark.parametrize("input,expected", [
(1, 2),
(2, 4),
(3, 6),
])
def test_double(input, expected):
assert double(input) == expected
# Multiple parameters
@pytest.mark.parametrize("x", [1, 2])
@pytest.mark.parametrize("y", [10, 20])
def test_multiply(x, y): # 4 test combinations
assert x * y > 0
def test_raises():
with pytest.raises(ValueError) as exc_info:
raise ValueError("Invalid input")
assert "Invalid" in str(exc_info.value)
def test_raises_match():
with pytest.raises(ValueError, match=r".*[Ii]nvalid.*"):
raise ValueError("Invalid input")
@pytest.mark.skip(reason="Not implemented yet")
def test_future_feature():
pass
@pytest.mark.skipif(sys.platform == "win32", reason="Unix only")
def test_unix_feature():
pass
@pytest.mark.xfail(reason="Known bug")
def test_buggy():
assert broken_function() == expected
@pytest.mark.slow
def test_performance():
"""Custom marker - register in pytest.ini."""
pass
from unittest.mock import Mock, patch, MagicMock
def test_with_mock():
mock_api = Mock()
mock_api.get.return_value = {"status": "ok"}
result = mock_api.get("/endpoint")
assert result["status"] == "ok"
@patch("module.external_api")
def test_with_patch(mock_api):
mock_api.return_value = {"data": []}
result = function_using_api()
mock_api.assert_called_once()
def test_with_mocker(mocker):
mock_api = mocker.patch("module.api_call")
mock_api.return_value = {"success": True}
result = process_data()
assert result["success"]
# tests/conftest.py - Shared fixtures
import pytest
@pytest.fixture(scope="session")
def app():
"""Application fixture available to all tests."""
return create_app(testing=True)
@pytest.fixture
def client(app):
"""Test client fixture."""
return app.test_client()
Run these inside the project env — prefix with uv run (e.g. uv run pytest -v).
Bare pytest is shown below for brevity.
| Command | Description |
|---|---|
pytest | Run all tests |
pytest -v | Verbose output |
pytest -x | Stop on first failure |
pytest -k "test_name" | Run matching tests |
pytest -m slow | Run marked tests |
pytest --lf | Rerun last failed |
pytest --cov=src | Coverage report |
pytest -n auto | Parallel (pytest-xdist) |
./references/fixtures-advanced.md - Factory fixtures, autouse, conftest patterns./references/mocking-patterns.md - Mock, patch, MagicMock, side_effect./references/async-testing.md - pytest-asyncio patterns./references/coverage-strategies.md - pytest-cov, branch coverage, reports./references/integration-testing.md - Database fixtures, API testing, testcontainers./references/property-testing.md - Hypothesis framework, strategies, shrinking./references/test-architecture.md - Test pyramid, organization, isolation strategies./scripts/run-tests.sh - Run tests with recommended options./scripts/generate-conftest.sh - Generate conftest.py boilerplate./assets/pytest.ini.template - Recommended pytest configuration./assets/conftest.py.template - Common fixture patternsRelated Skills:
python-typing-ops - Type-safe test codepython-async-ops - Async test patterns (pytest-asyncio)Testing specific frameworks:
python-fastapi-ops - TestClient, API testingpython-database-ops - Database fixtures, transactions