Linux版Postman导出测试报告步骤(基于Newman命令行工具)
在Postman中完成API测试用例编写(包括请求、测试脚本)后,需先将集合和环境变量导出为JSON文件:
Collection Format v2.1(推荐)导出为collection.json;environment.json。Newman是Postman的命令行伴侣,需通过Node.js安装:
sudo apt-get install nodejs npm),或从官网下载二进制文件解压安装;npm install -g newman(需管理员权限),安装完成后可通过newman --version验证是否成功。打开终端,进入导出文件的目录(如cd /home/user/postman_tests),执行以下命令:
newman run collection.json -e environment.json -r html --reporter-html-export report.html
collection.json:测试集合文件路径;-e environment.json:环境变量文件路径(可选,无环境变量时可省略);-r html:指定生成HTML格式报告;--reporter-html-export report.html:自定义报告文件名(如my-report.html)。report.html文件,用浏览器打开即可查看详细测试结果(包括通过/失败的用例、响应时间、断言详情等)。若需调整报告外观,可通过Newman的htmlextra reporter实现:
npm install -g newman-reporter-htmlextra;newman run collection.json -e environment.json -r htmlextra --reporter-htmlextra-export custom-report.html --reporter-htmlextra-title "API自动化测试报告"
--reporter-htmlextra-title:设置报告标题;--reporter-htmlextra-noSyntaxHighlighting:禁用代码语法高亮(可选)。除HTML外,Newman还支持JSON、JUnit等格式,适用于自动化集成:
newman run collection.json -e environment.json -r json --reporter-json-export result.json
newman run collection.json -e environment.json -r junit --reporter-junit-export junit.xml
environment.json;