0


postman自动记录执行结果

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

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

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

极简方案

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);
});

本文转载自: https://blog.csdn.net/kaixindaddy/article/details/129170754
版权归原作者 kaixindaddy 所有, 如有侵权,请联系我们删除。

“postman自动记录执行结果”的评论:

还没有评论