postman自动记录执行结果

问题
  1. 解决场景测与没测,结果对与不对纠纷

  1. 避免测试时结果是对的,后期代码改动造成结果错误导致的内容不确定

  1. 同一场景下,历史逻辑、返回结果的反查

极简方案

1、在history tab中,打开save Response 选项,自动记录历史请求和response

完善方案

将每次请求、结果都自动保存到本地文件中,以备查用

  1. https://github.com/sivcan/ResponseToFile-Postman 结合这个本地服务实现保存功能

使用方法:

Put all the requests you want to write the responses for, under this collection.

1、Clone the following repository to your machine - https://github.com/sivcan/ResponseToFile-Postman or use the following command - git clone https://github.com/sivcan/ResponseToFile-Postman

  1. Navigate into the directory and install the dependencies. Use the following command: npm i

如果没有npm,通过安装nodejs一起安装https://nodejs.org/en/

3、Run the local server. Use the following command: node script.js

Now, the responses for every request which is a part of this collection will be written to the Responses folder inside the project repo. You can modify the local server's code to change the file location.

Run your requests through builder / run through collection runner and store your data locally.

上面整个项目入口代码在 script.js文件中

  1. 可以对默认的文件存储路径进行修改

以下修改

/*
    生成年月日用于按日期存储文件
  */
 function getTimeInfo() {
       var date = new Date;
       var year = date.getFullYear(); 
       var month = date.getMonth()+1;
       month = (month<10 ? "0"+month:month); 
       var day = date.getDate(); 
       return (year.toString()+'-'+month.toString()+'-'+day.toString());
  }


 // Modify the folder path in which responses need to be stored
  folderPath = './Responses/'+getTimeInfo()'/',
  1. 修改生成文件的扩展名

postman中配置test脚本
// Please read the documentation on the right side (collection level documentation) to learn about how to use this collection -> 

// The opts for the server, also includes the data to be written to file
let opts = {
    requestName: request.name || request.url,
    fileExtension: 'json',
    mode: 'writeFile', // Change this to any function of the fs library of node to use it.
    uniqueIdentifier: true,
    requestData:pm.request.body.raw  || request.url,
    responseData: pm.response.text()
    
};

pm.sendRequest({
    url: 'http://localhost:3000/write',
    method: 'POST',
    header: 'Content-Type:application/json',
    body: {
        mode: 'raw',
        raw: JSON.stringify(opts)
    }
}, function (err, res) {
    console.log(res);
});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值