最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to write a postman test to compare the response json against another json? - Stack Overflow

programmeradmin3浏览0评论

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 pare the above json against a predefined json. Say, its the same as above.

How can I pare two jsons via the Postman test?

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 pare the above json against a predefined json. Say, its the same as above.

How can I pare two jsons via the Postman test?

Share Improve this question asked Aug 10, 2017 at 14:04 Praveesh PPraveesh P 1,4492 gold badges27 silver badges43 bronze badges 2
  • Praveesh, you should accept one of the answers below is they solve you problem. – Mike Commented May 25, 2020 at 12:57
  • This was an old project I did long back. Dropped it in between. I will try out if I get time. – Praveesh P Commented May 25, 2020 at 14:37
Add a ment  | 

7 Answers 7

Reset to default 3

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.pareArrayObject(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`);
        }
    },
    pareArrayObject: (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 pared.

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

assert.pareArrayObject(
    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");

I got it after a while. Add test into your request and use Runner to run all your requests in the collection.

Postman info: Version 7.10.0 for Mac.

Test scripts:

pm.test("Your test name", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData).to.eql({
        "key1": "value1",
        "key2": 100
    });
});

You can paste this code into your collection or single request tests tab.

What this code do is to save the request into a global variable with a key for that request. You can change your enviroment and hit the same request and if the response are different the test will fail.

const responseKey = [pm.info.requestName, 'response'].join('/');
let res = '';
try {
    res = JSON.stringify(pm.response.json());
} catch(e) {
    res = pm.response.text();
}

if (!pm.globals.has(responseKey)) {
    pm.globals.set(responseKey, res);
} else {    
    pm.test(responseKey, function () {
        const response = pm.globals.get(responseKey);
        pm.globals.unset(responseKey);
        try {
            const data = pm.response.json();
            pm.expect(JSON.stringify(data)).to.eql(response);
        } catch(e) {
            const data = pm.response.text();
            pm.expect(data).to.eql(response);
        }
    });
}

Hope this help.

You can write javascript code inside Tests tab of Postman. Just write simple code to pare and check result in Tests.

var serverData = JSON.parse(responseBody);
var JSONtoCompare = {}; //set your predefined JSON here.
tests["Body is correct"] = serverData === JSONtoCompare;

Looks like the same question asked at POSTMAN: Comparing object Environment variable with response's object which also lists a solution that works, which is to use JSON.stringify() to turn the objects into strings and then pare the strings.

Came across this issue when migrating from a legacy API to a new one and wanting to assert the new API is exactly the same as the old under different scenarios

For context this clones params of the original get request to the legacy endpoint and validates both match up

LEGACY_API_URL should be defined in the environment and the Request is going to the new API

const { Url } = require('postman-collection');

// Setup the URL for the Legacy API
const legacyRequestUrl = new Url({ host: pm.variables.replaceIn("http://{{LEGACY_API_HOST}}/blah")});

// Add All Parameters From the Source Query
legacyRequestUrl.addQueryParams(pm.request.url.query.all());

// Log out the URL For Debugging Purposes
console.log("URL", legacyRequestUrl.toString());

pm.sendRequest(legacyRequestUrl.toString(), function (err, response) {
    pm.test('New API Response Matches Legacy API Response', function () {

        // Log Out Responses for Debugging Purposes
        console.log("New API Response", pm.response.json())
        console.log("Legacy API Response", response.json())

        // Assert Both Responses are Equal
        pm.expect(_.isEqual(pm.response.json(), response.json())).to.be.true
    });
});

Link to an example collection


https://www.getpostman./collections/4ff9953237c0ab1bce99

Write JavaScript code under 'Tests' section. Refer below link for more info.

Click Here

发布评论

评论列表(0)

  1. 暂无评论