温馨提示×

Linux Postman如何编写自动化脚本

小樊
48
2025-08-11 01:51:44
栏目: 智能运维

在Linux下使用Postman编写自动化脚本主要有以下两种方式:

  • 使用Postman内置脚本功能
    • 编写测试脚本:在Postman请求的“Tests”选项卡中,用JavaScript编写脚本,如pm.test("Status code is 200", function() { pm.response.to.have.status(200); });用于验证状态码。
    • 设置环境变量:可在“Pre - request Script”选项卡中使用pm.environment.set("变量名", "值");设置环境变量,在请求中使用{{变量名}}引用。
  • 使用Newman命令行工具
    • 安装Newman:在终端运行npm install -g newman
    • 编写脚本:创建JavaScript文件,如run - tests.js,内容为const newman = require('newman'); newman.run({ collection: 'path/to/your/collection.json' }, function(err, summary) { if (err) { console.error(err); return; } console.log(summary); });
    • 运行脚本:在终端执行node run - tests.js

0