如何编写邮递员测试以将响应json与另一个json进行比较? [英] How to write a postman test to compare the response json against another json?

查看:161
本文介绍了如何编写邮递员测试以将响应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进行比较?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆