<legend id='dws61'><style id='dws61'><dir id='dws61'><q id='dws61'></q></dir></style></legend>
    <tfoot id='dws61'></tfoot>
  • <i id='dws61'><tr id='dws61'><dt id='dws61'><q id='dws61'><span id='dws61'><b id='dws61'><form id='dws61'><ins id='dws61'></ins><ul id='dws61'></ul><sub id='dws61'></sub></form><legend id='dws61'></legend><bdo id='dws61'><pre id='dws61'><center id='dws61'></center></pre></bdo></b><th id='dws61'></th></span></q></dt></tr></i><div id='dws61'><tfoot id='dws61'></tfoot><dl id='dws61'><fieldset id='dws61'></fieldset></dl></div>

      <small id='dws61'></small><noframes id='dws61'>

          <bdo id='dws61'></bdo><ul id='dws61'></ul>

        如何编写邮递员测试以将响应 json 与另一个 json 进行比较?

        How to write a postman test to compare the response json against another json?(如何编写邮递员测试以将响应 json 与另一个 json 进行比较?)
          <legend id='LPnjh'><style id='LPnjh'><dir id='LPnjh'><q id='LPnjh'></q></dir></style></legend>
            <tfoot id='LPnjh'></tfoot>

              • <bdo id='LPnjh'></bdo><ul id='LPnjh'></ul>
                  <tbody id='LPnjh'></tbody>

                1. <small id='LPnjh'></small><noframes id='LPnjh'>

                  <i id='LPnjh'><tr id='LPnjh'><dt id='LPnjh'><q id='LPnjh'><span id='LPnjh'><b id='LPnjh'><form id='LPnjh'><ins id='LPnjh'></ins><ul id='LPnjh'></ul><sub id='LPnjh'></sub></form><legend id='LPnjh'></legend><bdo id='LPnjh'><pre id='LPnjh'><center id='LPnjh'></center></pre></bdo></b><th id='LPnjh'></th></span></q></dt></tr></i><div id='LPnjh'><tfoot id='LPnjh'></tfoot><dl id='LPnjh'><fieldset id='LPnjh'></fieldset></dl></div>

                2. 本文介绍了如何编写邮递员测试以将响应 json 与另一个 json 进行比较?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  在运行 Rest API 的 postMan 测试后,我得到以下 json 响应:

                  I have the below json response after running a postMan test of a Rest API:

                      {
                      "glossary": {
                          "title": "example glossary",
                          "GlossDiv": {
                              "title": "S",
                              "GlossList": {
                                  "GlossEntry": {
                                      "ID": "SGML",
                                      "SortAs": "SGML",
                                      "GlossTerm": "Standard Generalized Markup Language",
                                      "Acronym": "SGML",
                                      "Abbrev": "ISO 8879:1986",
                                      "GlossDef": {
                                          "para": "A meta-markup language, used to create markup languages such as DocBook.",
                                          "GlossSeeAlso": ["GML", "XML"]
                                      },
                                      "GlossSee": "markup"
                                  }
                              }
                          }
                      }
                  }
                  

                  现在我想将上面的 json 与预定义的 json 进行比较.说,和上面一样.

                  Now I would like to compare the above json against a predefined json. Say, its the same as above.

                  如何通过 Postman 测试比较两个 json?

                  How can I compare two jsons via the Postman test?

                  推荐答案

                  我有一个类似的问题要解决,只是我的 JSON 还包含一个对象数组.我使用了以下可以修改的技术来处理您问题中的简单字符串数组.我创建了一个名为assert"的全局函数数组,其中包含areEqual"和areArraysOfObjectsEqual"等辅助函数并将它们保存在我的测试顶部文件夹级别的测试"选项卡.

                  I had a similar problem to solve except that my JSON also contained an array of objects. I used the following technique that can be modified to deal with the simple array of strings in your question.I created an array of global functions called "assert", which contained helper functions such as "areEqual" and "areArraysOfObjectsEqual" and saved these under the "Tests" tab at a top folder level of my tests.

                  assert = {
                      areEqual: (actual, expected, objectName) => {
                          pm.test(`Actual ${objectName} '` + actual + `' matches Expected ${objectName} '` + expected + `'`, () => {
                              pm.expect(_.isEqual(actual, expected)).to.be.true;
                          });
                      },
                      areArraysOfObjectsEqual: (actual, expected, objectName) => {
                          if (!_.isEqual(actual, expected)) {
                  
                              // Arrays are not equal so report what the differences are
                              for (var indexItem = 0; indexItem < expected.length; indexItem++) {
                                  assert.compareArrayObject(actual[indexItem], expected[indexItem], objectName);
                              }
                          }
                          else
                          {
                              // This fake test will always pass and is just here for displaying output to highlight that the array has been verified as part of the test run
                              pm.test(`actual '${objectName}' array matches expected '${objectName}' array`);
                          }
                      },
                      compareArrayObject: (actualObject, expectedObject, objectName) => {
                          for (var key in expectedObject) {
                              if (expectedObject.hasOwnProperty(key)) {
                                  assert.areEqual(expectedObject[key], actualObject[key], objectName + " - " + key);
                              }
                          }
                      }
                  };
                  

                  您用于测试的预请求脚本"将设置您的预期对象

                  Your "Pre-request Script" for a test would set your expected object

                   const expectedResponse =
                      {
                          "id": "3726b0d7-b449-4088-8dd0-74ece139f2bf",
                          "array": [
                              {
                                  "item": "ABC",
                                  "value": 1
                              },
                              {
                                  "item": "XYZ",
                                  "value": 2
                              }
                          ]
                      };
                  
                      pm.globals.set("expectedResponse", expectedResponse); 
                  

                  您的测试将单独或在数组级别测试每个项目,如下所示:

                  Your Test would test each item individually or at the array level like so:

                  const actualResponse = JSON.parse(responseBody);
                  const expectedResponse = pm.globals.get("expectedResponse");
                  
                  assert.areEqual(
                      actualResponse.id,
                      expectedResponse.id,
                      "id");
                  
                  assert.areArraysOfObjectsEqual(
                      actualResponse.myArray,
                      expectedResponse.myArray,
                      "myArrayName");
                  

                  此技术将提供很好的属性名称实际值与预期值匹配"输出,并适用于作为被比较 JSON 一部分的对象数组.

                  This technique will give nice "property name actual value matches expected value" output and works with arrays of objects being part of the JSON being compared.

                  更新:要测试您的字符串数组GlossSeeAlso",只需在您的任何测试中调用提供的全局帮助器方法,如下所示:

                  Update: To test your array of strings "GlossSeeAlso", simply call the supplied global helper method in any of your tests like so:

                  assert.compareArrayObject(
                      actualResponse.glossary.GlossDiv.GlossList.GlossEntry.GlossDef.GlossSeeAlso,       
                      expectedResponse.glossary.GlossDiv.GlossList.GlossEntry.GlossDef.GlossSeeAlso,
                      "glossary.GlossDiv.GlossList.GlossEntry.GlossDef.GlossSeeAlso");
                  

                  JSON 键值对中的原始类型可以这样测试:

                  Primitive types in JSON key value pairs can be tested like so:

                  assert.areEqual(
                      actualResponse.glossary.title,
                      expectedResponse.glossary.title,
                      "glossary.title");
                  

                  这篇关于如何编写邮递员测试以将响应 json 与另一个 json 进行比较?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Toggle HTML radio button by clicking its label(通过单击标签来切换 HTML 单选按钮)
                  Javascript how to change radio button label text?(Javascript如何更改单选按钮标签文本?)
                  JavaScript radio button confirmation(JavaScript 单选按钮确认)
                  How can I automatically select specific radio buttons with Greasemonkey?(如何使用 Greasemonkey 自动选择特定的单选按钮?)
                  AngularJs. Is it possible to deselect HTML “radio” input by click?(AngularJs.是否可以通过单击取消选择 HTML“收音机输入?)
                  Checking Value of Radio Button Group via JavaScript?(通过 JavaScript 检查单选按钮组的值?)

                  <small id='2F1Ex'></small><noframes id='2F1Ex'>

                  <legend id='2F1Ex'><style id='2F1Ex'><dir id='2F1Ex'><q id='2F1Ex'></q></dir></style></legend>
                            <tbody id='2F1Ex'></tbody>
                          <i id='2F1Ex'><tr id='2F1Ex'><dt id='2F1Ex'><q id='2F1Ex'><span id='2F1Ex'><b id='2F1Ex'><form id='2F1Ex'><ins id='2F1Ex'></ins><ul id='2F1Ex'></ul><sub id='2F1Ex'></sub></form><legend id='2F1Ex'></legend><bdo id='2F1Ex'><pre id='2F1Ex'><center id='2F1Ex'></center></pre></bdo></b><th id='2F1Ex'></th></span></q></dt></tr></i><div id='2F1Ex'><tfoot id='2F1Ex'></tfoot><dl id='2F1Ex'><fieldset id='2F1Ex'></fieldset></dl></div>
                            <bdo id='2F1Ex'></bdo><ul id='2F1Ex'></ul>

                            <tfoot id='2F1Ex'></tfoot>