• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

postman自动记录执行结果

武飞扬头像
kaixindaddy
帮助1

问题

  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. 可以对默认的文件存储路径进行修改

以下修改

  1.  
    /*
  2.  
    生成年月日用于按日期存储文件
  3.  
    */
  4.  
    function getTimeInfo() {
  5.  
    var date = new Date;
  6.  
    var year = date.getFullYear();
  7.  
    var month = date.getMonth() 1;
  8.  
    month = (month<10 ? "0" month:month);
  9.  
    var day = date.getDate();
  10.  
    return (year.toString() '-' month.toString() '-' day.toString());
  11.  
    }
  12.  
     
  13.  
     
  14.  
    // Modify the folder path in which responses need to be stored
  15.  
    folderPath = './Responses/' getTimeInfo()'/',
学新通
  1. 修改生成文件的扩展名

postman中配置test脚本
  1.  
    // Please read the documentation on the right side (collection level documentation) to learn about how to use this collection ->
  2.  
     
  3.  
    // The opts for the server, also includes the data to be written to file
  4.  
    let opts = {
  5.  
    requestName: request.name || request.url,
  6.  
    fileExtension: 'json',
  7.  
    mode: 'writeFile', // Change this to any function of the fs library of node to use it.
  8.  
    uniqueIdentifier: true,
  9.  
    requestData:pm.request.body.raw || request.url,
  10.  
    responseData: pm.response.text()
  11.  
     
  12.  
    };
  13.  
     
  14.  
    pm.sendRequest({
  15.  
    url: 'http://localhost:3000/write',
  16.  
    method: 'POST',
  17.  
    header: 'Content-Type:application/json',
  18.  
    body: {
  19.  
    mode: 'raw',
  20.  
    raw: JSON.stringify(opts)
  21.  
    }
  22.  
    }, function (err, res) {
  23.  
    console.log(res);
  24.  
    });
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgfjiae
系列文章
更多 icon
同类精品
更多 icon
继续加载