/claude-kit:testing-conventionsfrom Claude Kit SDLC
Provides testing conventions for Python/FastAPI and React projects: pytest async fixtures, Vitest setup, security-regression tests (authz, tenant isolation), and test quality review.
Ships in Claude Kit SDLC. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
Testing patterns and conventions derived from production backend and frontend services, with honest assessment of coverage gaps.
Single event loop per session: Define event_loop fixture with scope="session" to avoid loop creation overhead. (reference service patterns)
Pattern:
@pytest.fixture(scope="session")
def event_loop():
loop = asyncio.new_event_loop()
yield loop
loop.close()
Alternative: Use asyncio.get_event_loop() for session scope, but always close it in finally. (alternative pattern)
Separate test database + Redis DB: Override env vars before any app imports to ensure test isolation. Use dedicated test DB (e.g., app_test_db) and Redis DB 1. (integration test pattern)
Migration setup: Run Alembic migrations once per session via autouse=True fixture. (integration test pattern)
Cleanup pattern: Use autouse=True cleanup fixture that flushes Redis and truncates all tables with RESTART IDENTITY CASCADE. Also drop tenant schemas created during tests. (integration test pattern)
Lightweight mocking alternative: For unit tests, use dependency overrides with UnittestConnectionManager singleton that provides isolated async engine + session factory. No real DB setup needed for pure unit tests. (unit test pattern)
Authenticated client fixture: Register sys_admin user, login, attach cookies to client, yield, then logout. (integration test pattern)
HTTP client fixture: Use httpx.AsyncClient with ASGITransport(app=app) for FastAPI testing. Always close in finally. (common pattern)
Dependency override registration: Override get_connection_handler_for_app and get_connection_handler_for_func at module level in conftest. (dependency injection pattern)
Factory fixtures: Use factory.Factory with factory.LazyAttribute and factory.Faker for generating test data schemas. (factory pattern)
Per-module conftest: Domain-specific fixtures (e.g., tests/unit/webhook/conftest.py) provide mocked DAOs, services, and sample data factories. (domain-specific fixture pattern)
Function-scoped DAO fixtures: Wrap DAO instantiation in async context manager from db_connection session factory. (DAO test pattern)
Pattern:
@pytest.fixture
async def dao(self, db_connection):
async with db_connection() as session:
yield TenantDAO(session)
Parametrized fixtures: Use @pytest.fixture(params=[...]) for multiple test cases (e.g., template resolution, condition evaluation, HTTP methods). (parametrized fixture pattern)
AsyncMock for Kafka: Mock emit, emit_batch, close as AsyncMock(). Mock consumer with start, stop, and __aiter__. (Kafka mock pattern)
AsyncMock for Temporal: Mock client methods (start_workflow, get_workflow_handle, health_check) as AsyncMock(). Mock activity/workflow info with MagicMock(). (Temporal mock pattern)
unittest.mock.patch for HTTP/config: Use @patch("module.path.function") for Slack/HTTP publish functions and config objects. (patch pattern)
Mock DAO/service pattern: Create MagicMock() with all methods stubbed as AsyncMock() for dependency injection. (mock DAO pattern)
@pytest.mark.asyncio: Required for all async test functions. (standard pytest-asyncio practice)
Async with httpx: All FastAPI route tests use async def test_ and await client.post/get(...). (FastAPI test pattern)
Vitest config: Use jsdom environment, globals mode, setup file for test utilities. (standard Vitest pattern)
Coverage thresholds: Set aggressive thresholds (90% lines/functions/branches/statements) but exclude generated files, test files, and specific uncovered components. (example: excludes hard-to-test components like complex sidebars)
Include pattern: src/test/**/*.test.{ts,tsx} for test discovery. (standard Vitest pattern)
Coverage focus: Prioritize src/lib/** and src/hooks/**; exclude API clients and module-specific views from baseline coverage. (focused coverage strategy)
Reference service A (high coverage): 18 test files (17 test_*.py + conftest + load_test); comprehensive coverage of auth, multi-tenancy, access control, enterprise features. Strength: integration tests for entire feature sets.
Reference service B (moderate coverage): ~40 domain modules, most have 3-file test structure (test_dao.py, test_service.py, test_route.py) but many are thin or absent. Strength: factory fixtures + mocking patterns.
Reference service C (minimal coverage): 3-4 test files (test_contracts.py, test_sdk.py, test_workflow_execution.py E2E, + conftest) covering JSON schema validation and basic E2E. Gap: No comprehensive DAO/service layer tests, no Temporal workflow tests beyond mocks.
Reference service D (zero coverage): 0 test files found. Gap: No test infrastructure.
Multiple other services: Minimal to zero test coverage (0-2 test files). Gap: No test baselines.
Reference frontend: Vitest configured with aggressive thresholds; 31 test files found (14 hook tests, 11 component tests, 6 module/lib tests). Strength: Good coverage of hooks (useToast, usePagination, useFilters, etc.) and task module components. Coverage includes config, utils, permissions, API clients.
Path-based triggers: Limit workflow runs to relevant changes using paths: filters on backend/, frontend/, or schema-specific directories. Prevents unnecessary CI runs when unrelated modules change. (path filtering pattern)
Service containers with health checks: Define postgres and redis as services: with health checks (pg_isready, redis-cli ping). CI waits for healthy state before running tests. Set --health-interval 10s, --health-timeout 5s, --health-retries 5. (service health pattern)
Job dependencies (needs:): Chain test jobs after lint/typecheck using needs: [lint] to fail fast on style/type errors. Run multiple test suites (unit, api, integration, e2e) in parallel after linting completes. (job dependency pattern)
Coverage enforcement: Use pytest --cov=src --cov-report=xml --cov-fail-under=<threshold> to enforce minimum coverage percentage. Fails CI if coverage drops below threshold. (coverage gating pattern)
Artifact upload: Upload test results (--junitxml=test-results.xml), coverage reports (coverage.xml), and E2E traces/screenshots with actions/upload-artifact@v4. Persist on if: always() or if: failure() for debugging. (artifact pattern)
Codecov integration: Upload coverage XML to Codecov with flags (backend/frontend) using codecov/codecov-action@v4. Set fail_ci_if_error: false to avoid blocking on Codecov API issues. (codecov pattern)
Contract testing pipeline: Generate JSON fixtures from Pydantic schemas, verify fixture validity (error count == 0), then run frontend Vitest contract tests against fixtures. Separate job ensures schema changes don't break frontend types. (contract testing pattern)
E2E with background services: Start backend (uvicorn) and frontend (vite dev) in background (&), wait for health endpoints with timeout 30 bash -c 'until curl -s http://localhost:8000/_healthz; do sleep 1; done', then run Playwright tests. Upload traces on failure. (E2E orchestration pattern)
Pytest markers for test categories: Separate api-tests and integration-tests jobs using pytest -m api and pytest -m integration. Allows running subsets of tests with targeted service dependencies (api-tests skip redis). (pytest marker pattern)
Pattern:
on:
pull_request:
paths:
- 'backend/**'
- '.github/workflows/backend-tests.yml'
jobs:
test:
needs: [lint]
services:
postgres:
image: postgres:15
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
ports: ['5432:5432']
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- run: pytest --cov=src --cov-fail-under=70 --junitxml=test-results.xml --cov-report=xml
- uses: actions/upload-artifact@v4
if: always()
with:
name: test-results
path: backend/test-results.xml
- uses: codecov/codecov-action@v4
with:
file: backend/coverage.xml
flags: backend
integration-tests:
needs: [lint]
services:
postgres: {...}
redis: {...}
steps:
- run: pytest -m integration -v
The "Contract testing pipeline" above validates one repo's schema against its own frontend. When a
provider serves multiple independent consumers (other services, mobile clients, partner APIs),
generalize it to consumer-driven contracts (CDC): each consumer publishes the exact subset of the
provider's responses it actually depends on (a pact), and the provider replays every consumer's
pact in its own CI. The provider then learns it broke a downstream consumer before it deploys —
without standing up a full end-to-end environment — and stays free to change anything no consumer's
contract pins. CDC is the test-suite counterpart to the cross-service planning in
planning-and-task-breakdown and the expand/contract rollout in deprecation-and-migration: green
pacts are how you know the "contract" (removal) phase is actually safe. (cross-service test pattern)
Per Martin Fowler / Ian Robinson, "Consumer-Driven Contracts" (martinfowler.com). Stack- and tool-agnostic — Pact is one implementation; the discipline (consumer defines, provider verifies in CI) is the point.
Functional tests prove a feature works; security-regression tests prove it can't be abused. They are the test class most often missing (see the coverage audit) and the cheapest insurance against a re-broken authz/tenant boundary. Treat each confirmed authz/isolation bug as a permanent test.
Negative authorization (per protected route): assert the chain rejects, not just that it accepts. For every role-gated endpoint, test: no auth → 401; authenticated but insufficient role → 403; correct role → 2xx. Don't test only the happy path. (security-regression pattern)
Cross-tenant isolation / RLS: create two tenants A and B with data in each; with tenant A's
credentials, attempt to read/update/delete tenant B's resource → expect 404 (prefer 404 over 403 so
existence doesn't leak). For RLS, also assert the DAO layer returns nothing for the wrong tenant
context (SET LOCAL app.tenant_id), so isolation holds even if a handler forgets to scope. (see
multi-tenancy-patterns)
IDOR (object-reference): with a valid user, request another user's / another tenant's object by id (sequential, guessed, or captured) → expect 404/403, never the object. (security-regression pattern)
Login lockout & rate limit: drive N failed logins → assert lockout/429/locked_until; assert a
correct password during lockout still fails; assert the limiter keys by IP (unauthenticated) and user
(authenticated). (see auth-and-rbac)
Signed-header / forwarded-identity trust: send a request to a behind-the-gateway service with no
x-gateway-signature → 401; with a forged signature → 401; with a valid signature but a role
insufficient for the action → 403. Prevents naked-header-trust regressions. (see
edge-to-service-trust-boundary)
Input-boundary rejection: oversized payloads, wrong content types, and injection-shaped strings
(' OR 1=1, ../../etc/passwd, <script>) are rejected at the schema boundary (422), not executed.
(security-regression pattern)
Wire into CI as a dedicated gate: mark these @pytest.mark.security and run a security-tests job
(or pytest -m security) that must pass — separate from unit/integration so a security regression is
an unmissable red signal, not buried in a large suite. (security CI pattern)
# Security-regression tests (mark with @pytest.mark.security; run as a dedicated CI gate)
import pytest
@pytest.mark.security
class TestAccessControl:
async def test_requires_auth(self, client):
resp = await client.get("/v1/admin/users")
assert resp.status_code == 401 # no auth -> 401
async def test_member_forbidden_on_admin_route(self, member_client):
resp = await member_client.get("/v1/admin/users")
assert resp.status_code == 403 # wrong role -> 403
async def test_admin_allowed(self, admin_client):
resp = await admin_client.get("/v1/admin/users")
assert resp.status_code == 200
@pytest.mark.security
class TestTenantIsolation:
async def test_cannot_read_other_tenant_resource(self, tenant_a_client, seed_tenant_b_order):
resp = await tenant_a_client.get(f"/v1/orders/{seed_tenant_b_order.id}")
assert resp.status_code == 404 # cross-tenant -> 404 (no existence leak)
async def test_rls_blocks_at_dao_layer(self, db_connection, seed_tenant_b_order):
async with db_connection() as session:
await session.execute(text("SET LOCAL app.tenant_id = :tid"), {"tid": str(TENANT_A_ID)})
order = await OrderDAO(session).get_by_id(seed_tenant_b_order.id)
assert order is None # isolation holds even if handler forgets
@pytest.mark.security
class TestLoginLockout:
async def test_lockout_after_repeated_failures(self, client):
for _ in range(5):
await client.post("/v1/auth/login", json={"email": "[email protected]", "password": "wrong"})
resp = await client.post("/v1/auth/login", json={"email": "[email protected]", "password": "correct"})
assert resp.status_code in (423, 429) # locked / rate-limited even with right password
@pytest.mark.security
class TestGatewayTrust:
async def test_missing_signature_rejected(self, raw_client):
resp = await raw_client.get("/v1/internal/profile", headers={"x-user-role": "admin"})
assert resp.status_code == 401 # naked header, no signature -> 401
# conftest.py (integration test setup)
import os
os.environ["DATABASE_URL"] = os.environ.get(
"TEST_DATABASE_URL",
"postgresql+asyncpg://user:pass@postgres:5432/app_test_db",
)
os.environ["REDIS_URL"] = "redis://redis:6379/1"
import pytest
import httpx
from httpx import ASGITransport
@pytest.fixture(scope="session")
def event_loop():
loop = asyncio.new_event_loop()
yield loop
loop.close()
@pytest.fixture(scope="session", autouse=True)
async def _run_migrations():
result = subprocess.run(["alembic", "upgrade", "head"], ...)
if result.returncode != 0:
raise RuntimeError(f"Migration failed: {result.stderr}")
yield
@pytest.fixture
async def client(app):
transport = ASGITransport(app=app)
async with httpx.AsyncClient(transport=transport, base_url="http://test") as ac:
yield ac
@pytest.fixture
async def authenticated_client(client: httpx.AsyncClient):
await client.post("/v1/auth/register", json={...})
resp = await client.post("/v1/auth/login", json={...})
client.cookies.update(resp.cookies)
yield client
await client.post("/v1/auth/logout")
@pytest.fixture(autouse=True)
async def cleanup():
yield
# Flush Redis
redis = Redis.from_url(settings.REDIS_URL)
await redis.flushdb()
await redis.aclose()
# Truncate all tables + drop tenant schemas
engine = create_async_engine(settings.DATABASE_URL)
async with engine.begin() as conn:
await conn.execute(text("TRUNCATE TABLE ... RESTART IDENTITY CASCADE"))
await engine.dispose()
# conftest.py (unit test mocks)
from tests.utils import UnittestConnectionManager, get_test_connection_handler_for_app
@pytest.fixture(scope="session", autouse=True)
def event_loop():
loop = asyncio.get_event_loop()
try:
yield loop
finally:
loop.close()
@pytest_asyncio.fixture
async def client():
async with httpx.AsyncClient(app=test_app, base_url="http://testserver/v1.0") as http_client:
try:
yield http_client
finally:
await http_client.aclose()
@pytest_asyncio.fixture(autouse=False)
async def db_connection():
cm = UnittestConnectionManager()
try:
engine = cm.get_db_engine()
await db_setup(engine) # Create all tables
yield cm.get_session_factory()
await db_cleanup(engine) # Drop all tables
finally:
await cm.close_connections()
test_app.dependency_overrides[get_connection_handler_for_app] = get_test_connection_handler_for_app
# Domain-specific conftest (webhook/temporal)
import factory
from unittest.mock import AsyncMock, MagicMock
class WebhookDeliveryFactory(factory.Factory):
class Meta:
model = WebhookDeliveryCreate
webhook_chain_step_id = factory.LazyAttribute(lambda n: uuid4())
url = factory.Faker("url")
http_method = factory.Faker("random_element", elements=["GET", "POST", "PUT"])
status_code = 200
delivery_status = "success"
@pytest.fixture
def mock_webhook_delivery_dao():
dao = MagicMock()
dao.get_by_pk = AsyncMock()
dao.create_delivery = AsyncMock()
return dao
@pytest.fixture
async def mock_temporal_client():
client = AsyncMock()
client.start_workflow = AsyncMock(return_value="test_workflow_id")
return client
@pytest.fixture
async def mock_kafka_producer():
producer = AsyncMock()
producer.emit = AsyncMock()
producer.close = AsyncMock()
return producer
# Test example (DAO layer)
class TestTenantDao:
@pytest.fixture
async def dao(self, db_connection):
async with db_connection() as session:
yield TenantDAO(session)
async def test_get_records_by_id(self, dao, db_connection):
records = await dao.get_record_by_id(1)
assert records is None
async with db_connection() as session:
tenant = await populate_tenant_by_size(session)
record = await dao.get_record_by_id(tenant.id)
assert record.id == tenant.id
# Test example (async route with mocking)
@patch("src.config.docker_config.loaded_config", MockConfig)
@patch("src.utils.slack.alerts.publish_slack_alert_push_event")
@pytest.mark.asyncio
async def test_raise_slack_alert_success(mock_publish_event, valid_slack_alert_schema):
mock_publish_event.return_value = True
response = await raise_slack_alert(valid_slack_alert_schema)
assert response["success"]
mock_publish_event.assert_called_once()
# Vitest config (frontend)
import { defineConfig } from 'vitest/config';
export default defineConfig({
test: {
globals: true,
environment: 'jsdom',
setupFiles: ['./src/test/setup.ts'],
include: ['src/test/**/*.test.{ts,tsx}'],
coverage: {
provider: 'v8',
include: ['src/lib/**', 'src/hooks/**'],
exclude: ['src/test/**', 'src/**/*.test.{ts,tsx}'],
thresholds: { lines: 90, functions: 90, branches: 90 },
},
},
});
@pytest_asyncio.fixture or @pytest.fixture with async def.@pytest.fixture with async def works in most cases, @pytest_asyncio.fixture is explicit but requires pytest-asyncio mode configuration.@pytest.mark.security and run a dedicated gate so a security regression is an unmissable red, not a single assertion lost among hundreds.When coverage is near-zero, establish this pragmatic baseline before adding features:
create, get_by_id, get_by_field, update, delete for each entity.Do not aim for 80% coverage initially — aim for 40-60% on the right surface (DAO, service contracts, auth).
README.md (opens in a new tab)references/async-and-mocking.md (opens in a new tab)references/coverage-gap-and-recommendations.md (opens in a new tab)references/github-actions-test-orchestration.md (opens in a new tab)references/pytest-and-fixtures.md (opens in a new tab)references/pytest-asyncio-modes.md (opens in a new tab)references/repo-evidence.md (opens in a new tab)references/security-regression-tests.md (opens in a new tab)references/vitest-frontend-testing.md (opens in a new tab)What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
pytest, vitest, async-fixtures, test-isolation, security-regression
npx claudepluginhub ajyadav013/claude-kit --plugin claude-kit127 skills in this plugin
/accessibility-reviewUI accessibility (WCAG) review — keyboard operability, focus, ARIA semantics, color contrast, touch targets. Returns prioritized fixes. Distinct from performance/visual review./alembic-migrationsAlembic migration setup and patterns for async SQLAlchemy — async engine + run_sync in env.py, autogenerate, RLS/multi-tenant migrations. Use for alembic init, writing migrations, or debugging migration errors./anthropic-vertex-integrationClaude on Vertex AI via the AnthropicVertex SDK — lazy ADC client init, env-based project/region resolution, async helpers, backoff retry. Use when calling Claude via GCP Vertex AI (not direct Anthropic API).Other plugins' skills for the same job