pytest-suite

GitHub

指导编写和扩展后端测试套件,遵循项目约定。适用于新增服务、路由或仓库的测试,覆盖缺失场景。利用 mocked-session 和 httpx AsyncClient 实现无数据库运行,提供异步测试模式、Fixtures 使用及命名规范。

template/{{cookiecutter.project_slug}}/.claude/skills/pytest-suite/SKILL.md vstorm-co/full-stack-ai-agent-template

Trigger Scenarios

添加新服务/路由/仓库的测试 测试覆盖率不足时补充测试 被要求对特定功能进行测试

Install

npx skills add vstorm-co/full-stack-ai-agent-template --skill pytest-suite -g -y
More Options

Non-standard path

npx skills add https://github.com/vstorm-co/full-stack-ai-agent-template/tree/main/template/{{cookiecutter.project_slug}}/.claude/skills/pytest-suite -g -y

Use without installing

npx skills use vstorm-co/full-stack-ai-agent-template@pytest-suite

指定 Agent (Claude Code)

npx skills add vstorm-co/full-stack-ai-agent-template --skill pytest-suite -a claude-code -g -y

安装 repo 全部 skill

npx skills add vstorm-co/full-stack-ai-agent-template --all -g -y

预览 repo 内 skill

npx skills add vstorm-co/full-stack-ai-agent-template --list

SKILL.md

Frontmatter
{
    "name": "pytest-suite",
    "description": "Write or extend the backend test suite following this project's conventions. Use when adding tests for a new service\/route\/repository, when coverage is missing, or when asked to test a feature. Knows the mocked-session + httpx AsyncClient setup so tests run with no database."
}

Backend Tests (pytest + anyio)

Tests live in backend/tests/, mirror the source layout (app/services/user.py → tests/services/test_user.py), and run with no real database — conftest.py overrides get_db_session with an AsyncMock via FastAPI dependency_overrides.

Key fixtures (tests/conftest.py)

  • client — httpx.AsyncClient over ASGITransport(app=app) (use this, not Starlette TestClient)
  • mock_db_session — AsyncMock standing in for AsyncSession
  • mock_redis (when Redis is enabled), api_key_headers (when API keys are enabled)

Patterns

Async tests use anyio, not @pytest.mark.asyncio:

import pytest

pytestmark = pytest.mark.anyio  # at module top, or mark per-test

Service test — stub what the repo/session returns, assert behavior + exceptions:

async def test_get_user_not_found_raises(monkeypatch, mock_db_session):
    service = UserService(mock_db_session)
    monkeypatch.setattr(user_repo, "get_by_id", AsyncMock(return_value=None))
    with pytest.raises(NotFoundError):
        await service.get_by_id(UUID("00000000-0000-0000-0000-000000000000"))

API test — drive the route through client, override auth deps with a mock user:

async def test_create_user_returns_201(client: AsyncClient):
    app.dependency_overrides[get_current_user] = lambda: mock_user
    resp = await client.post("/api/v1/users", json={"email": "a@b.com", "password": "secret123"})
    assert resp.status_code == 201

Naming & rules

  • test_<action>_<scenario>_<expected_result> (e.g. test_create_user_with_duplicate_email_raises_already_exists_error)
  • One behavior per test; plain assert (pytest rewrites it)
  • Use factory fixtures for data, not raw dicts; each test independent
  • Clear app.dependency_overrides after the test (the client fixture already does this for the DB/Redis overrides)

Run

cd backend && uv run pytest                 # all
uv run pytest tests/services/test_user.py -v
uv run pytest --cov=app                      # coverage

After writing tests, run uv run ruff check . --fix && uv run ruff format . and confirm uv run pytest is green.

Version History

  • 0.2.16 Current 2026-07-25 10:05

Same Skill Collection

template/{{cookiecutter.project_slug}}/.claude/skills/agent-tool/SKILL.md
template/{{cookiecutter.project_slug}}/.claude/skills/alembic-migration/SKILL.md
template/{{cookiecutter.project_slug}}/.claude/skills/background-task/SKILL.md
template/{{cookiecutter.project_slug}}/.claude/skills/billing-stripe/SKILL.md
template/{{cookiecutter.project_slug}}/.claude/skills/channel-bot/SKILL.md
template/{{cookiecutter.project_slug}}/.claude/skills/frontend-feature/SKILL.md
template/{{cookiecutter.project_slug}}/.claude/skills/rag-knowledge/SKILL.md

Metadata

Files
0
Version
0.2.19
Hash
d61d0088
Indexed
2026-07-25 10:05

ホーム - Wiki
Copyright © 2011-2026 iteam. Current version is 2.155.2. UTC+08:00, 2026-10-05 14:13
浙ICP备14020137号-1