mcpField
T
testing-integration

testing-integration

github.com/yonatangross测试与质量299125 次安装今天更新
代码仓库

关于 testing-integration

testing-integration 是一套集成与契约测试指导技能,面向编写 API、组件、数据库及跨服务测试的开发者。它汇集规则、参考资料、清单和示例,并提供测试工具的选择指引。

主要功能

  1. 01API 端点集成测试指导
  2. 02React 组件集成测试指导
  3. 03数据库层测试与隔离模式
  4. 04Pact 契约及 Hypothesis 属性测试
  5. 05Zod schema 边界验证

适用场景

  1. 01为 API 端点编写集成测试
  2. 02验证跨团队 API 契约
  3. 03使用真实依赖测试数据库层
  4. 04规划新功能的测试策略

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/yonatangross/orchestkit --skill testing-integration

安装后在 Claude Code 中输入/testing-integration确认已加载。

包含的文件

除 SKILL.md 外,安装时一起下载的参考资料和脚本。

test-cases.json
checklists/
contract-testing-checklist.md
property-testing-checklist.md
examples/
orchestkit-test-strategy.md
references/
consumer-tests.md
ork-delta.md
strategies-guide.md
rules/
_sections.md
emulate-stateful-testing.md
integration-api.md
integration-component.md
integration-database.md
validation-zod-schema.md
verification-contract.md
verification-stateful.md
verification-techniques.md
scripts/
create-integration-test.md

详细介绍

集成与契约测试

聚焦 API 边界、跨服务契约、组件集成、数据库层、基于属性的验证和架构验证的测试模式。

快速参考

对于复杂的 emulate 配置(生成完整配置、Webhook HMAC、CI 每个工作进程的端口隔离),请委派给 emulate-engineer 子 Agent。可与 emulate-seed 技能配合使用。

领域规则 / 参考影响
有状态 API 测试(emulate)rules/emulate-stateful-testing.md高
API 端点测试rules/integration-api.md高
React 组件集成rules/integration-component.md高
数据库层测试rules/integration-database.md高
Zod 架构验证rules/validation-zod-schema.md高
Pact 契约测试rules/verification-contract.md中
有状态测试(Hypothesis)rules/verification-stateful.md中
证据与基于属性的测试rules/verification-techniques.md中
参考资料
主题文件
上游文档未涵盖的项目规则references/ork-delta.md
消费者端 Pact 测试references/consumer-tests.md
Hypothesis 策略指南references/strategies-guide.md
检查清单
检查清单文件
契约测试准备情况checklists/contract-testing-checklist.md
基于属性的测试checklists/property-testing-checklist.md
脚本与模板
脚本文件
创建集成测试scripts/create-integration-test.md
示例
示例文件
完整测试策略examples/orchestkit-test-strategy.md

上游覆盖范围(请勿重复说明)

此技能封装了 Pact、Pact Broker 和 FastAPI 测试。仅包含 OrchestKit 的增补内容。对于以下主题,请查阅供应商文档,而不要在此重复说明。

主题一手资料
Pact Broker 发布 CLI、Pact 版本控制和标记标志https://docs.pact.io/pact_broker/publishing_and_retrieving_pacts
can-i-deploy、record-deployment、record-releasehttps://docs.pact.io/pact_broker/can_i_deploy
部署和发布记录语义https://docs.pact.io/pact_broker/recording_deployments_and_releases
契约变更时触发提供者构建的 Broker Webhookhttps://docs.pact.io/pact_broker/webhooks
消费者版本选择器语法(mainBranch、deployedOrReleased、matchingBranch)https://docs.pact.io/pact_broker/advanced_topics/consumer_version_selectors
待处理 Pact 语义https://docs.pact.io/pact_broker/advanced_topics/pending_pacts
提供者状态设置钩子和状态端点接线https://docs.pact.io/getting_started/provider_states
FastAPI TestClient 和用于测试数据库的 dependency_overrideshttps://fastapi.tiangolo.com/advanced/testing-dependencies/
GitHub Actions 作业排序(needs:)和分支筛选器https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions
QA 测试计划文档(时间安排、角色、缺陷生命周期、签署表)由 Tracker 管理;没有单一供应商页面涵盖全部四项。缺陷生命周期:https://docs.github.com/en/issues/tracking-your-work-with-issues/about-issues 。时间安排、角色和签署属于组织流程,并非有文档说明的产品功能。已退役模板中的内部质量预算保留在 references/ork-delta.md 中。

这些主题的内部子集仍保留在此技能中,不会被转到其他地方:rules/verification-contract.md 保留了 ork 门禁要求的两条 Broker 命令以及选择器默认值;checklists/contract-testing-checklist.md 保留了 CI/CD、隔离和安全复选项;references/consumer-tests.md 保留了业务语言的提供者状态命名约定和匹配器表。没有任何上游对应内容的规则位于 references/ork-delta.md 中。


有状态 API 测试(模拟 — 首选)

对于 GitHub、Vercel 和 Google API 集成测试,模拟是首选方案。它提供可模拟真实 API 行为的完整状态机,而非静态模拟。

工具最适用场景
emulate有状态 API 测试(GitHub/Vercel/Google)——首选
Pact跨团队契约验证
MSW前端 HTTP 模拟(简单请求/响应)
NockNode.js 单元级 HTTP 拦截

请参阅 rules/emulate-stateful-testing.md,了解完整的决策矩阵、seed-start-test-assert 模式以及错误/正确示例。


Testcontainers(CI 中的真实依赖项)

当契约测试和 emulate 不够用时,例如需要针对真实的 Postgres、Redis、Kafka 或兼容 S3 的存储进行测试,可使用 Testcontainers 为每项测试启动临时 Docker 容器,并在测试后将其销毁。上方的 path_patterns 已匹配 **/testcontainers/**;请在其中使用这些模式。

目标版本:testcontainers >= 11.0.0(Node)——v11(2026 年第一季度)新增了命名网络自动清理、通过 .withReuse() 复用容器以及一流的 Podman 支持。

Node.js(testcontainers-node)
import { PostgreSqlContainer } from '@testcontainers/postgresql'
import { describe, beforeAll, afterAll, test, expect } from 'vitest'

describe('UserRepository integration', () => {
  let container: Awaited<ReturnType<PostgreSqlContainer['start']>>
  let repo: UserRepository

  beforeAll(async () => {
    container = await new PostgreSqlContainer('postgres:16-alpine')
      .withDatabase('test')
      .withUsername('test')
      .withPassword('test')
      .withReuse()  // v11+ — reuse across runs to speed CI
      .start()

    repo = new UserRepository(container.getConnectionUri())
    await repo.migrate()
  }, 30_000)

  afterAll(async () => {
    await container.stop()
  })

  test('persists and retrieves a user', async () => {
    const created = await repo.create({ email: '[email protected]' })
    const found = await repo.findById(created.id)
    expect(found?.email).toBe('[email protected]')
  })
})
Python(testcontainers-python)
from testcontainers.postgres import PostgresContainer
import pytest

@pytest.fixture(scope="session")
def postgres():
    with PostgresContainer("postgres:16-alpine") as pg:
        yield pg.get_connection_url()

def test_user_repo(postgres):
    repo = UserRepository(postgres)
    repo.migrate()
    user = repo.create(email="[email protected]")
    assert repo.find_by_id(user.id).email == "[email protected]"

决策矩阵:

场景选择
第三方 API(GitHub、Vercel、Google)emulate
跨团队 API 契约Pact
真实的 Postgres / Redis / Kafka 集成Testcontainers
仅在前端测试中模拟 HTTPMSW

快速开始:API 集成测试

TypeScript(Supertest)
import request from 'supertest';
import { app } from '../app';

describe('POST /api/users', () => {
  test('creates user and returns 201', async () => {
    const response = await request(app)
      .post('/api/users')
      .send({ email: '[email protected]', name: 'Test' });

    expect(response.status).toBe(201);
    expect(response.body.id).toBeDefined();
    expect(response.body.email).toBe('[email protected]');
  });

  test('returns 400 for invalid email', async () => {
    const response = await request(app)
      .post('/api/users')
      .send({ email: 'invalid', name: 'Test' });

    expect(response.status).toBe(400);
    expect(response.body.error).toContain('email');
  });
});
Python(FastAPI + httpx)
import pytest
from httpx import ASGITransport, AsyncClient
from app.main import app

@pytest.fixture
async def client():
    async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as ac:
        yield ac

@pytest.mark.asyncio
async def test_create_user(client: AsyncClient):
    response = await client.post(
        "/api/users",
        json={"email": "[email protected]", "name": "Test"}
    )
    assert response.status_code == 201
    assert response.json()["email"] == "[email protected]"

覆盖率目标

领域目标
API 端点70%+
服务层80%+
组件交互70%+
契约测试所有消费者使用的端点
属性测试所有编码/解码、幂等函数

核心原则

  1. 在边界处测试——API 输入、数据库查询、服务调用、外部集成
  2. 每项测试使用全新状态——内存数据库、事务回滚、不共享可变状态
  3. 在契约中使用匹配器——使用 Like()、EachLike()、Term(),而非精确值
  4. 使用基于属性的测试验证不变量——往返、幂等性、交换性属性
  5. 在边界处验证架构——在每个 API 边界调用 Zod .safeParse()
  6. 以证据支持完成状态——退出代码为 0、覆盖率报告、时间戳

何时使用此技能

  • 编写 API 端点测试(Supertest、httpx)
  • 配置带有 providers 的 React 组件集成测试
  • 创建隔离的数据库集成测试
  • 实现 Pact 消费者/提供者契约测试
  • 使用 Hypothesis 添加基于属性的测试
  • 在 API 边界验证 Zod 架构
  • 为新功能或服务规划测试策略

相关技能

  • ork:testing-unit — 单元测试模式、夹具、模拟
  • ork:testing-e2e — 端到端 Playwright 测试
  • ork:emulate-seed — 为 emulate providers 编写种子配置
  • ork:database-patterns — 数据库架构和迁移模式
  • ork:api-design — 用于端点测试的 API 设计模式

评论

支持 Markdown