在Linux上利用Postman进行接口自动化可按以下步骤操作:
安装依赖
sudo apt-get update && sudo apt-get install nodejs npm
npm install -g newman
准备测试集合
// 检查状态码
pm.test("Status code is 200", function() {
pm.response.to.have.status(200);
});
// 校验响应数据
const data = pm.response.json();
pm.test("Data contains expected fields", function() {
pm.expect(data).to.have.property("id");
});
配置环境变量
base_url、token),在请求中使用{{变量名}}动态替换。{{base_url}}/api/login,通过环境变量切换不同环境。导出集合与环境文件
通过Newman运行自动化测试
newman run collection.json --environment=env.json
newman run collection.json --environment=env.json --reporters=html --reporter-html-export=report.html
集成到CI/CD(如Jenkins)
pipeline {
agent any
stages {
stage('API Test') {
steps {
sh 'npm install -g newman'
sh 'newman run ./collections/api-tests.json --environment=./env/dev-env.json --reporters=html'
}
}
}
}
关键工具:
通过以上步骤,可实现Linux环境下Postman接口自动化测试的全流程,支持从单机执行到持续集成系统的集成。