Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
766 views
in Technique[技术] by (71.8m points)

javascript - How to write a postman test to compare the response json against another 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"
                }
            }
        }
    }
}

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

How can I compare two jsons via the Postman test?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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.

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

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...