Trying to assert that the JSON body (or XML) in an API response is correct, which approach do you use?
Iāve noticed that some people use full expected response body and compare it to what they get back:
assertEquals(expectedResponse, actualResponse)
This seems a bit too much for me, since the response can change and youāll have to update your test.
Another approach I noticed is to assert if just a part of the response contains an expected value, like:
assertTrue.contatins("expected-value")
The second approach seems like it will require less maintenance in the future, especially if you assert on something that you know will not change in the response.
What are you thoughts on this? Any other ideas? Iām really curious!
I assert on the json values themselves, which results into easier debugging afterwards and cleaner reporting. Letās see an example (API Testing in Postman)
Entire JSON assert
var completeJsonBody = pm.environment.get("completeJsonBody")
var jsonData = pm.response.json();
pm.test("Check if jsonBody matches", function () {
pm.expect(jsonData).to.eql(completeJsonBody);
});
Single JSON asserts
var Id= pm.environment.get("Id")
var jsonData = pm.response.json();
pm.test("Check if ID matches", function () {
pm.expect(jsonData.id).to.eql(Id);
});
pm.test("Check if name matches", function () {
pm.expect(jsonData.name).to.eql("Kristof");
});
pm.test("Check if age matches", function () {
pm.expect(jsonData.age).to.eql(29);
});
I just prefer the second option because you can instantly see which test failed and which value is giving you an error. Itās also more clear for reporting to see what tests you do. If on all API request you note ābody is okā compared to āCheck to see if age matchesā ⦠it says a lot more I think.
Maintenance wise, I believe itās cleaner also, because you donāt always have to assert on each value on each request and if something changes or a value changes, itās an easy fix. + you can actually see what you assert on.
Question: How are you going to compare a GET /lists/ ? What if I make another item that is added towards that list, or someone adds an item manually through the UI?
Looking forward to seeing other peopleās answers!
I use different strategies depending on what Iām testing. Iāll use exact response matching if the response isnāt dynamic, or can be generated strictly from the input (i.e. maybe a JSON object that returns the external ID I provided and a status of submitted or something).
Once it moves to anything dynamic (timestamps, server assigned IDs, etc), I tend to do field level validations, using various levels of checks depending on the risk/criticality. i.e. for date/timestamps, I might check the format and then verify the value is within 5 seconds of the submission, but for some fields I might just check that itās a string or a number, or ignore them completely.
Iām personally not a fan of JSON schema checks since we donāt develop based on schemas, and the auto generated ones like that shared in the video by @restertest tend to be too loose to be of much value, and Iām likely going to be having to write field level validations anyway. If we did have full schemas as a practice, Iād likely push for contract testing using PACT or similar.
I donāt like validating the whole json response even itās easier to add single assertion for it, I was also wondering about others thoughts so I am thankful for finding this post with these awesome answers! @mirza curious to know what do you recommend after more than a year of posting this.