一键导入
python-testing
Python testing strategies using pytest, TDD methodology, fixtures, mocking, parametrization, and coverage requirements.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Python testing strategies using pytest, TDD methodology, fixtures, mocking, parametrization, and coverage requirements.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
ann-benchmarks フレームワーク規約(algos.yaml/HDF5/Pareto frontier分析)と ANN ベクトル検索の SIMD 距離カーネル最適化における落とし穴パターン(AVX2/AVX-512 ディスパッチャ検証・量子化の数学的等価性確認・部分集合とフルスケールの混同防止)。ArcFlare/NGT/NGTAQ 等の ANN R&D 作業時の参照用。実装作業自体は ann-perf-engineer agent に委譲する。
Use this skill to measure performance baselines, detect regressions before/after PRs, and compare stack alternatives.
Claude API / Anthropic Go SDK usage patterns, prompt caching, streaming, tool use, and model selection for Go applications.
C++ coding standards based on the C++ Core Guidelines (isocpp.github.io). Use when writing, reviewing, or refactoring C++ code to enforce modern, safe, and idiomatic practices.
Deployment workflows, CI/CD pipeline patterns, Docker containerization, health checks, rollback strategies, and production readiness checklists for web applications.
非推奨・後方互換用リダイレクト。旧 dig(コードベース深掘り分析→設計インタビュー→実装計画→自律実行)は swarm-loop に完全統合された。/dig と入力された場合は本ファイルの指示に従い、そのまま swarm-loop skill を 同じ目標・同じ引数で起動すること。dig 独自のロジックはここには存在しない。
| name | python-testing |
| description | Python testing strategies using pytest, TDD methodology, fixtures, mocking, parametrization, and coverage requirements. |
| origin | ECC |
Comprehensive testing strategies for Python applications using pytest, TDD methodology, and best practices.
Full code examples for every pattern below live in reference.md (same directory, one level deep).
This file covers when to use each pattern and the core decision criteria; reference.md is the catalog.
Always follow the TDD cycle:
# Step 1: Write failing test (RED)
def test_add_numbers():
result = add(2, 3)
assert result == 5
# Step 2: Write minimal implementation (GREEN)
def add(a, b):
return a + b
# Step 3: Refactor if needed (REFACTOR)
pytest --cov to measure coveragepytest --cov=mypackage --cov-report=term-missing --cov-report=html
Basic test structure (plain assert statements) and the full assertion vocabulary
(equality, truthiness, membership, type checks, pytest.raises for exceptions) —
see "pytest Fundamentals" in reference.md.
Use fixtures to eliminate setup/teardown duplication. Prefer the narrowest scope that
still avoids repeated work: function scope by default, module/session scope only for
expensive shared resources, autouse=True sparingly (only for cross-cutting setup like
config reset), and conftest.py for fixtures shared across multiple test files.
See "Fixtures" in reference.md for: basic usage, setup/teardown with yield,
scopes (function/module/session), parameterized fixtures, using multiple fixtures,
autouse fixtures, and conftest.py examples.
Use @pytest.mark.parametrize instead of looping inside a test or duplicating near-identical
test functions — one assertion path, many input rows. Add ids= when the input values
themselves aren't self-descriptive in test output.
See "Parametrization" in reference.md for: basic parametrization, multiple parameters,
parametrize with IDs, and parametrized fixtures (e.g. running the same test against multiple
DB backends).
Use custom markers (@pytest.mark.slow, @pytest.mark.integration, @pytest.mark.unit)
to let CI and local runs select subsets via pytest -m "...". Always register custom markers
in pytest.ini/pyproject.toml with --strict-markers so typos fail loudly instead of
silently no-op'ing.
See "Markers and Test Selection" in reference.md for marker definitions, selection
commands, and the pytest.ini markers = block.
Mock external dependencies (network calls, DB connections, filesystem) — never let a unit
test depend on a real external service. Prefer autospec=True so mocks fail if the mocked
API's signature doesn't match. Use side_effect to simulate exceptions, not manual
try/except scaffolding in the mock.
See "Mocking and Patching" in reference.md for: mocking functions, mocking return
values, mocking exceptions, mocking context managers (mock_open), autospec, mocking class
instances, and PropertyMock.
Async test functions need @pytest.mark.asyncio (from pytest-asyncio) and async fixtures
need async def + yield. Assert mock calls with assert_awaited_once() / assert_awaited_once_with(),
not the sync assert_called_once() variants.
See "Testing Async Code" in reference.md for async tests, async fixtures, and mocking
async functions.
Prefer pytest.raises(...) as a context manager over manual try/except-and-fail scaffolding.
Use match= for message substrings and exc_info.value to assert on custom exception
attributes.
See "Testing Exceptions" in reference.md for expected-exception and exception-attribute
examples.
For filesystem-touching code, prefer pytest's built-in tmp_path (pathlib) or tmpdir (py.path)
fixtures over manually managed tempfile + try/finally — they're auto-cleaned and test-scoped.
See "Testing Side Effects" in reference.md for temp-file processing, tmp_path, and
tmpdir examples.
Split tests into unit/, integration/, e2e/ directories under tests/, with shared
fixtures in tests/conftest.py. Group related tests into a TestXxx class with an
autouse setup fixture when they share expensive setup.
See "Test Organization" in reference.md for the directory layout and a TestUserService
class example.
test_user_login_with_invalid_credentials_failspytest.raisesReady-to-adapt patterns for testing FastAPI/Flask endpoints, database operations (with a rollback-per-test session fixture), and class-based test suites.
See "Common Patterns" in reference.md for the full API-endpoint, database, and
class-method examples.
Configure testpaths, test discovery globs, --strict-markers, coverage options, and marker
registration in either pytest.ini or pyproject.toml ([tool.pytest.ini_options]) — pick one
per project, don't split config across both.
See "pytest Configuration" in reference.md for complete pytest.ini and pyproject.toml
examples.
# Run all tests
pytest
# Run specific file
pytest tests/test_utils.py
# Run specific test
pytest tests/test_utils.py::test_function
# Run with verbose output
pytest -v
# Run with coverage
pytest --cov=mypackage --cov-report=html
# Run only fast tests
pytest -m "not slow"
# Run until first failure
pytest -x
# Run and stop on N failures
pytest --maxfail=3
# Run last failed tests
pytest --lf
# Run tests with pattern
pytest -k "test_user"
# Run with debugger on failure
pytest --pdb
| Pattern | Usage |
|---|---|
pytest.raises() | Test expected exceptions |
@pytest.fixture() | Create reusable test fixtures |
@pytest.mark.parametrize() | Run tests with multiple inputs |
@pytest.mark.slow | Mark slow tests |
pytest -m "not slow" | Skip slow tests |
@patch() | Mock functions and classes |
tmp_path fixture | Automatic temp directory |
pytest --cov | Generate coverage report |
assert | Simple and readable assertions |
Remember: Tests are code too. Keep them clean, readable, and maintainable. Good tests catch bugs; great tests prevent them.