在Linux下使用Postman进行自动化测试,可按以下步骤操作:
安装依赖工具
ln -s
命令创建全局符号链接,方便终端调用。npm install -g newman
安装命令行工具,用于批量运行测试集合。创建测试集合与脚本
pm.test("Status code is 200", function() {
pm.response.to.have.status(200);
});
pm.test("Response time is less than 500ms", function() {
pm.expect(pm.response.responseTime).to.be.below(500);
});
运行自动化测试
newman run my-collection.json -e my-environment.json -d data.csv -r html
集成到CI/CD流程
.yml
工作流文件,调用Newman命令自动运行测试,例如:jobs:
test-api:
runs-on: ubuntu-latest
steps:
- name: Run Postman tests
run: newman run ./collections/my-api-tests.json --reporters cli,html
高级功能(可选)
pm.iterationData.get('变量名')
引用。{{变量名}}
动态切换配置。-r html
参数生成可视化报告,或集成Allure等专业工具。参考来源: