基于 AWS Lambda + Jenkins 的自动化接口测试平台实践
在项目开发中,我们希望在 服务部署完成后,能够自动执行全量接口测试,并生成可视化报告,同时将测试结果推送到团队的沟通渠道。本文介绍我在实际工作中实现的一套方案:
利用 GitHub(Bruno 测试脚本)、AWS Lambda、API Gateway、S3、Slack、Jenkins,构建了一个轻量级、无服务化的自动化测试平台。
一、架构设计
整体流程如下:
-
测试脚本仓库(GitHub)
- 使用 Bruno 编写接口测试脚本,组织成一个 collection,覆盖所有 API 接口。
- Repo 中提交
.bru
文件即可。
-
Lambda 执行测试
- 从 GitHub 拉取测试脚本
- 执行
bru run collection
- 生成
HTML
报告(存储到 S3 )和JSON
报告(用于分析) - 对 JSON 分析生成总结,推送到 Slack
-
API Gateway
- 暴露三个接口:
/internalToken
:获取服务间调用 token/customerToken
:获取客户 token/s3
:获取测试报告地址
- 暴露三个接口:
-
Jenkins 集成
- post deploy job:调用 Lambda,执行接口测试
- 业务服务 Jenkins job :在部署完成后触发
post deploy job
简单来说就是:代码部署完成 → Jenkins 调用 Lambda → Bruno 测试执行 → 结果存储 + 通知
二、测试脚本(Bruno Collection 示例)
一个典型的 collection
(比如 tests/collection.bru
):
yaml
collection:
name: Service API Tests
requests:
- request:
name: Get User
url: https://api.example.com/user/{{userId}}
method: GET
headers:
Authorization: Bearer {{token}}
assertions:
- expect: response.status
toBe: 200
- expect: response.body.id
toBe: {{userId}}
- request:
name: Create Order
url: https://api.example.com/order
method: POST
headers:
Authorization: Bearer {{token}}
body:
productId: 123
quantity: 1
assertions:
- expect: response.status
toBe: 201
运行 collection:
bash
bru run ./tests/collection.bru --env dev --output report.html --format html --output report.json --format json
三、Lambda 实现
Lambda 的职责:拉取测试代码 → 执行 Bruno → 上传结果 → 分析 JSON → 推送 Slack。
示例 Python 代码:
python
import os
import subprocess
import json
import boto3
import tempfile
import requests
s3 = boto3.client("s3")
S3_BUCKET = os.environ["S3_BUCKET"]
REPO = os.environ["GITHUB_REPO"]
BRANCH = os.environ.get("GITHUB_BRANCH", "main")
SLACK_WEBHOOK = os.environ["SLACK_WEBHOOK"]
def lambda_handler(event, context):
with tempfile.TemporaryDirectory() as tmpdir:
os.chdir(tmpdir)
# 拉取测试脚本
subprocess.run(["git", "clone", "-b", BRANCH, REPO, "."], check=True)
# 运行 Bruno collection
subprocess.run([
"bru", "run", "./tests/collection.bru",
"--output", "report.html", "--format", "html",
"--output", "report.json", "--format", "json"
], check=True)
# 上传结果到 S3
s3.upload_file("report.html", S3_BUCKET, "report.html")
s3.upload_file("report.json", S3_BUCKET, "report.json")
# 读取 JSON 结果并生成总结
with open("report.json") as f:
data = json.load(f)
total = data["summary"]["totalRequests"]
passed = data["summary"]["passed"]
failed = data["summary"]["failed"]
summary = f"""
✅ Bruno Tests Completed
Total: {total}
Passed: {passed}
Failed: {failed}
Report: https://{S3_BUCKET}.s3.amazonaws.com/report.html
"""
# 发送到 Slack
requests.post(SLACK_WEBHOOK, json={"text": summary})
return {"status": "success", "summary": summary}
四、API Gateway 配置
这里我们使用 AWS API Gateway 来暴露三个接口,供测试脚本调用:
GET /internalToken
→ 绑定到get_internal_token
的 LambdaGET /customerToken
→ 绑定到get_customer_token
的 LambdaGET /s3
→ 绑定到get_s3_report
的 Lambda
步骤
- 打开 API Gateway 控制台 → 创建一个新的 API(选择 REST API 或 HTTP API)
- 创建资源与方法:
/internalToken
→GET
→ Lambda Proxy Integration → 选择get_internal_token
/customerToken
→GET
→ Lambda Proxy Integration → 选择get_customer_token
/s3
→GET
→ Lambda Proxy Integration → 选择get_s3_report
- 部署 API(如部署到
dev
stage) - 得到公开的 URL,例如:
bash
https://xxxxxx.execute-api.us-east-1.amazonaws.com/dev/internalToken
这样,Bruno 的测试脚本里就能直接调用这些地址来获取 token 或报告 URL,而 API Gateway 负责调度 Lambda,不需要写额外代码。
五、Jenkins 流程
1. post deploy job(调用 Lambda)
groovy
pipeline {
agent any
stages {
stage("Run API Tests") {
steps {
script {
sh '''
aws lambda invoke --function-name run-bruno-collection --payload '{}' output.json
'''
}
}
}
}
}
2. 业务服务 Jenkins job
在服务的 Jenkinsfile 中添加:
groovy
post {
success {
build job: 'post-deploy-api-tests'
}
}
六、最终效果
- 部署完成后 Jenkins 自动触发 API 测试
- Lambda 执行 Bruno collection,生成 HTML/JSON 报告
- 报告上传到 S3,Slack 自动推送总结:
makefile
✅ Bruno Tests Completed
Total: 20
Passed: 19
Failed: 1
Report: https://my-bucket.s3.amazonaws.com/report.html
- 团队可以随时点击报告查看详细信息
七、总结
通过这一套方案,我们实现了:
- 测试脚本集中管理:GitHub 统一维护 collection
- 无服务化执行:Lambda 按需运行,免维护测试机
- 自动化集成:Jenkins 部署后立即触发测试
- 结果可视化 & 通知:S3 承载报告,Slack 实时推送
这一架构非常轻量,适合微服务环境中的 快速回归验证,能有效提升交付质量。