Hello, I have problem with comparing responses.
Right now Iām using something like below, but that only shows if responses are exactly the same.
In first response I use
pm.globals.set(āresponseā, pm.response.json());
And in the second:
pm.test(āResponse the same as previous.ā, () => {
pm.expect(pm.response.json()).to.deep.equal(pm.globals.get(āresponseā));
});
Sadly in my project there are sometimes responses with different ordering, which according to PO is not an bug, so wonāt be fixed. And that makes my tests fails.
Example what I need:
Lets say I have two json responses:
[
{
āletterā: āaā,
āexampleā: āAgaveā,
āusefulnessā: āAwesomeā
},
{
āletterā: ābā,
āexampleā: āBeerā,
āusefulnessā: āBest letter everā
},
{
āletterā: ācā,
āexampleā: āChipsā,
āusefulnessā: āCoolā
}
]
And
[
{
āletterā: āaā,
āexampleā: āAgaveā,
āusefulnessā: āAwesomeā
},
{
āletterā: ācā,
āexampleā: āChipsā,
āusefulnessā: āCoolā
},
{
āletterā: ābā,
āexampleā: āBeerā,
āusefulnessā: āBest letter everā
}
]
And I would like to create a test that would consider those responses as passed.
Hey @konrad.rafalko
Welcome to the Postman community!
You could take a look at using the ChaiJS .deep.members() assertion?
I added this to the Tests tab to try it out under different scenarios (Object orders, Property ordering in the Object, Remove properties, Changing key/value names etc.)
let dataSetOne = [
{
"letter": "a",
"example": "Agave",
"usefulness": "Awesome"
},
{
"letter": "b",
"example": "Beer",
"usefulness": "Best letter ever"
},
{
"letter": "c",
"usefulness": "Cool",
"example": "Chips"
}
]
let dataSetTwo = [
{
"letter": "a",
"example": "Agave",
"usefulness": "Awesome"
},
{
"letter": "c",
"example": "Chips",
"usefulness": "Cool"
},
{
"letter": "b",
"example": "Beer",
"usefulness": "Best letter ever"
}
]
pm.test("Compare Data Sets", () => {
pm.expect(dataSetOne).to.have.deep.members(dataSetTwo);
})
2 Likes
danny-dainton:
let dataSetOne = [
{
"letter": "a",
"example": "Agave",
"usefulness": "Awesome"
},
{
"letter": "b",
"example": "Beer",
"usefulness": "Best letter ever"
},
{
"letter": "c",
"usefulness": "Cool",
"example": "Chips"
}
]
let dataSetTwo = [
{
"letter": "a",
"example": "Agave",
"usefulness": "Awesome"
},
{
"letter": "c",
"example": "Chips",
"usefulness": "Cool"
},
{
"letter": "b",
"example": "Beer",
"usefulness": "Best letter ever"
}
]
pm.test("Compare Data Sets", () => {
pm.expect(dataSetOne).to.have.deep.members(dataSetTwo);
})
I used this method to compare responses of two same request but got error:
āCompare Data Sets | AssertionError: expected { Object (token, user_summary, ā¦) } to be an arrayā
PFB my test script
var jsonData = pm.response.json();
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
pm.globals.set("token", jsonData.token);
pm.environment.set("schoolId", "jsonData.user_summary.school_id");
const expectedResponse = pm.globals.get("aResponseAsJson");
pm.test("Compare Data Sets", () => {
pm.expect(jsonData).to.have.deep.members(expectedResponse);
})
tests['School ID check'] = pm.environment.get("schoolID") == jsonData.user_summary.school_id;
How do i compare two responses of same request
Hey @rupal.biyani
What are the 2 things that youāre comparing? That might help with the error youāre seeing
That script doesnāt really tell the whole story
MariDubois
(Mari Dubois)
August 11, 2020, 12:18pm
9
danny-dainton:
pm.expect(dataSetOne).to.have.deep.members(dataSetTwo);
Hello,
I need the same thing. However Iām comparing two json with nested objects.
If I apply this on my json response I get "AssertionError: expected { Object (data, dictionaries) } to be an array " because itās an object and not an array.
Could you please help?
Thank you in advance.
Best regards
I am getting this as the error::
[ Array(13) ] to have the same members as [ Array(1) ]
any leads on when and what to do?
Hey @Anumundada
You would need to expand on this and share the full context of what youāre doing.
That error message alone doesnāt give any insight to what you have done to get to this point.