以下是基于Node.js技术栈的契约测试(Contract Tests)实现示例,以用户加入封闭小组的审批流程为例,使用Pact工具进行消费者驱动契约测试:
场景说明
- 消费者服务:
前端应用
(通过API Gateway调用Auth Service) - 提供者服务:
Auth Service
(处理封闭小组的加入审批) - 关键接口:
POST /approvals
(提交加入申请)和GET /approvals/{id}
(查询审批状态)
步骤1:消费者端定义契约(前端测试)
1 | // tests/contract/auth-service.consumer.spec.js |
运行测试后会生成契约文件(webfrontend-authservice.json
)
步骤2:提供者端验证契约(Auth Service测试)
1 | // tests/contract/auth-service.provider.spec.js |
关键验证点说明
请求匹配:
1
2
3
4
5
6
7
8
9
10// 消费者定义的请求必须精确匹配
withRequest: {
method: 'POST',
path: '/approvals',
headers: { 'Content-Type': 'application/json' },
body: {
userId: 'user123',
groupId: 'group456'
}
}- 提供者的路由必须存在
POST /approvals
- 请求体必须包含
userId
和groupId
字段
- 提供者的路由必须存在
响应匹配:
1
2
3
4
5
6
7willRespondWith: {
status: 201,
body: {
id: Matchers.uuid('d1f0a3d4'), // 验证UUID格式
status: 'PENDING' // 必须返回精确值
}
}- 提供者必须返回201状态码
- 响应必须包含符合UUID格式的
id
字段
测试执行流程
消费者测试生成契约:
1
2
3
4# 在前端项目中运行
npm test -- tests/contract/auth-service.consumer.spec.js
# 生成契约文件并发布到Pact Broker
pact-broker publish ./pacts --consumer-app-version=1.0.0 --broker-base-url=http://broker提供者验证契约:
1
2# 在Auth Service项目中运行
npm test -- tests/contract/auth-service.provider.spec.js持续集成配置(示例GitHub Actions):
1
2
3
4
5
6
7
8
9
10
11
12jobs:
consumer-tests:
runs-on: ubuntu-latest
steps:
- run: npm test -- contracts/
- run: pact-broker publish ./pacts --auto-detect-version-properties
provider-tests:
needs: consumer-tests
runs-on: ubuntu-latest
steps:
- run: npm test -- provider.contracts.spec.js
契约测试的核心价值
接口稳定性:
- 当Auth Service修改API时(如字段重命名),契约测试会立即失败
- 防止提供者意外破坏现有消费者
文档即测试:
1
2
3
4
5
6
7
8// 生成的契约文件成为权威文档
"response": {
"status": "PENDING",
"body": {
"id": "d1f0a3d4-...",
"status": "PENDING"
}
}跨团队协作:
- 前端团队可独立定义期望的接口行为
- 后端团队通过验证契约确保实现符合预期
通过这种方式,当Auth Service
升级到v2时,只需运行契约测试即可验证是否与现有消费者兼容,显著降低微服务间的集成风险。