如何在Linux系统上使用Postman生成API测试报告
在Linux环境下,Postman本身不直接提供命令行报告生成功能,需通过Newman(Postman的命令行工具)配合实现。以下是详细步骤:
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
pm.test("Response body contains expected data", function () {
const jsonData = pm.response.json();
pm.expect(jsonData.message).to.eql("Success");
});
my-api-tests.json);若有环境变量(如API基础URL、认证信息),同样导出为JSON文件(如my-api-env.json)。sudo apt-get update
sudo apt-get install nodejs npm
npm install -g newman
newman-reporter-html插件:npm install -g newman-reporter-html
newman run my-api-tests.json -e my-api-env.json -r html --reporter-html-export report.html
参数说明:
my-api-tests.json:导出的测试集合文件;-e my-api-env.json:环境变量文件(可选,无环境变量时可省略);-r html:指定报告格式为HTML;--reporter-html-export report.html:设置报告输出文件名。report.html文件,用浏览器打开即可查看详细测试结果(包括通过/失败的用例数、响应时间、错误信息等)。json格式:newman run my-api-tests.json -e my-api-env.json -r html,json --reporter-html-export report.html --reporter-json-export report.json
npm install -g newman
newman run my-api-tests.json -e my-api-env.json -r html --reporter-html-export ${WORKSPACE}/report.html
生成的报告可通过CI工具分享给团队。{{base_url}},环境变量中需定义base_url);