“测试1小时,改Bug一整天”, 传统手动测试不仅耗时费力,还容易遗漏关键场景,导致线上事故频发。目前,全球Top 10的科技公司中,90%的团队早已抛弃手动测试,转向更智能的解决方案——自动化测试!
不过,许多人对自动化测试的认知仍停留在“高门槛”“复杂代码”的误区中。
自从上手Apipost,发现并非如此: 无需写一行代码,三步操作即可实现全流程自动化测试,小白也能10分钟上手。
打开Apipost,点击“新建测试用例”,给你的测试任务起个名字,比如“用户登录接口测试”。
在测试用例中,你可以像搭积木一样,把接口拖拽进来。比如,先添加“用户登录接口”,再添加“获取用户信息接口”。
1.1 上传测试数据文件
username
和 password
字段。 username,password
test01@apipost.cn,123456
test02@apipost.cn,789012
1.2 环境隔离:不同环境不同数据
local_users.csv
。prod_users.csv
。2.1 参数化接口请求
{{username}}
和 {{password}}
。
2.2 循环控制器与测试数据的结合
{{username}}
和 {{password}}
引用数据。
2.3 动态数据提取与再利用
{
"code": 200,
"success": true,
"msg": "Data generated successfully",
"timestamp": "2023-09-20T14:30:00Z",
"data": [
{
"id": "e3f5a7b1-9c2d-4f8a-b6c0-1d8e9f2a7b3c",
"username": "alice_m2023",
"password": "Tp9@xQvL!",
"created_at": "2023-09-20T14:00:00Z"
},
{
"id": "a2b4c6d8-e0f1-4a3b-9c5d-7e9f1a2b3c4d",
"username": "maxwell_7",
"password": "8nG#r2sKd",
"created_at": "2023-09-20T14:05:00Z"
},
{
"id": "5d7f9a1b-3c8e-4a2d-9b0f-6e1a3b5c7d9e",
"username": "zara.frost",
"password": "qW6$bY9pA",
"created_at": "2023-09-20T14:10:00Z"
},
{
"id": "8e1a3b5c-7d9e-4f2a-b6c0-9d8e7f1a2b3c",
"username": "t_robot92",
"password": "Lm3^cF8zN",
"created_at": "2023-09-20T14:15:00Z"
},
{
"id": "1a2b3c4d-5e6f-7a8b-9c0d-1e2f3a4b5c6d",
"username": "kira_nexus",
"password": "R@4vP7hXy",
"created_at": "2023-09-20T14:20:00Z"
}
]
}
$.data
提取数组。
{{username}}
和 {{password}}
引用数据。
性能问题是接口测试的“隐形杀手”。Apipost的性能测试功能,能帮你快速定位瓶颈!
1.1 安装 CLI 工具
npm install -g apipost-cli-pro
apipost-cli
,请先卸载: npm uninstall -g apipost-cli
1.2 生成 CICD 命令
apipost run "https://open.apipost.net/open/ci/automated_testing?ci_id=xxx&token=xxx" -r html
2.1 Jenkins 集成
pipeline {
agent any
tools {nodejs "node16.14"} //替换替换服务器支持的node版本。node版本>16
stages {
stage('Install Apipost CLI') {
steps {
sh 'npm install -g apipost-cli'
}
}
stage('Running Test Scenario') {
steps {
sh 'apipost run "https://open.apipost.net/open/ci/automated_testing?ci_id=xxx&token=xxx" -r html'
}
}
}
}
2.2 GitHub Actions 集成
.github/workflows/test.yml
文件。 name: Automated API Tests
on: [push, pull_request]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup Node.js environment
uses: actions/setup-node@v2
with:
node-version: '14'
- name: Install Apipost CLI
run: npm install -g apipost-cli
- name: Running Test Scenario
run: apipost run "https://open.apipost.net/open/ci/automated_testing?ci_id=xxx&token=xxx" -r html
假设我们有一个用户管理系统,包含以下四个接口:
我们将使用Apipost对这些接口进行自动化测试,确保系统的稳定性和可靠性。
打开Apipost,点击“新建测试用例”,给测试任务起个名字,比如“用户完整流程测试”。
按照以下顺序将接口拖拽到测试用例中:
{
"username": "test_user",
"email": "test_user_@example.com",
"password": "Test@123"
}
"status": "success"
。userId
并存储为全局变量。
{
"username": "test_user",
"password": "Test@123"
}
"token"
字段。token
并存储为全局变量。
http
Authorization: Bearer {{token}}
{
"userId": "{{userId}}",
"amount": 100.00,
"currency": "CNY"
}
"transactionId"
字段。
http
Authorization: Bearer {{token}}
http
userId={{userId}}
点击“保存并执行”,Apipost会自动生成一份详细的测试报告,包括接口响应时间、断言结果、失败原因等。
在测试过程中,我们使用了动态数据提取功能,将注册接口返回的userId
和登录接口返回的token
存储为全局变量,并在后续接口中引用这些变量。这种方式不仅减少了重复输入数据的工作量,还确保了测试数据的一致性和准确性。
为了验证接口在高并发场景下的性能,我们可以在登录接口上进行性能测试:
将测试用例集成到CI/CD流程中,确保每次代码提交后自动触发测试:
.github/workflows/test.yml
文件,添加安装Apipost CLI和运行测试命令的步骤。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有