it is showing up as pass, but when i enter
ExpectedSitesTodisplay variable within the console, its saying undefined,
and if i change something in the pre requisite script and click send it should fail but it still passes…
Your test isn’t testing the variable you set in your prerequest script.
pm.test(“Verify Expected Results sites match the Actual Results of all sites for Darlene”), function(){
const expectedSitesToDisplay = pm.environment.get('ExpectedSitesTodisplay');
pm.expect(savedData.response).to.equal(expectedSitesToDisplay);
};
I did try this and am getting it to pass, but lets say if i change one parameter or letter in the pre requisite script it should fail but its not failing … so meaning its not comparing actual and expected.
I was trying to get the full response body through the SavedData (actual response body) and compare it to the pre requisite variable (ExpectedSitesTodisplay) (expected) (pre requisite)
when i do this it passes, but if i change anything on the pre requsite script it still passes which it should not.
Hi, for the pre requesite script i have copied the full response body … and pasted it within the pre requisite script and then i want to verify the pre requisite script response body matches the response body within the get Request .
Just one GET request (Actual) , Pre req of that get Request (Expected) …
Then you should not be doing this in a pre-request script. You should be setting the variable in the test scripts which run AFTER the request is executed.
Are you wanting to validate only the schema or do you also want to validate the data for this specific request?
I added comments to parts of this snippet that you have provided.
change last line to
pm.expect(response).to.deep.equal(ExpectedSitesTodisplay);
or
pm.expect(response).to.deep.include(ExpectedSitesTodisplay);
Postman use the CHAI assertion library - Expect / Should - Chai
Read the examples there and this should help.
If you need to check also data by hand I can share this script
Save test function as variable
Save in Collection, Directory, folder some other test
pm.globals.set('helper', function loadhelper() {
let helper = {};
helper.test = (model, response) => {
// you can manually provide nested object instead of whole response.
if(response == null)
response = pm.response.json()
for (var x in model) { //It's list of keys of model json - the first level. any nested objects are check deeply.
// the X is single field from model to be validated. . You can do it from response to model. I prefer to check that way. As response may have audit fields like created by created date and other
try {
if(model[x] == null){
pm.test("Model null in respons - " + x, function () {
pm.expect(response[x]).to.be.null;
});
} else if (x.includes("date") || x.includes("Date")) {
// dates must be in same format to be compared
let res = new Date(response[x]).toISOString();
let mod = new Date(model[x]).toISOString();
pm.test("Model match response - date of " + x, function () {
pm.expect(mod).to.equal(res);
});
} else if (x.includes("id") || x.includes("Id")) {
//for debug if failed
// console.log("id " + x )
// console.log("id slice " + x.slice(0,-2) )
// if you have nested response
if (x === "OtherUnitId") { // If Model and respons name does not equal but data should be checked. This is for exceptions that are constant rather than unexpected
pm.test("Model match response - Id of object " + x, function () {
// ParseInt is necessary if you run test from newman. There everything is converted to string.... don't ask why ... but parse is necessary
pm.expect(parseInt(model[x])).to.equal(response["thatUnit"].id);
});
// if you have flat response
} else {
pm.test("Model match respons - Id of " + x, function () {
pm.expect(parseInt(model[x])).to.equal(parseInt(response[x]));
});
} else { // this 'else' or above must be deleted.
// any other nested 'ID check' field if you have flat response remove the slice and '.id'
pm.test("Model match respons - Id of " + x, function () {
pm.expect(parseInt(model[x])).to.equal(response[x.slice(0, -2)].id);
});
}
}
else if (typeof(model[x]) == typeof({}) && model[x] != null ){
pm.test("Model deep match respons - Field " + x, function (){
pm.expect(model[x]).to.deep.include(response[x]);
});
// here you can call recursion to this method and check deeply nested object. But it's danger zone :)
}
else { // any other field, as strings.... mostly stings
pm.test("Model match respons - Field " + x, function () {
pm.expect(model[x]).to.equal(response[x]);
});
}
}
catch (e) { // debugging purpose
pm.test("ERR No such field in response " + x, function () {
pm.expect.fail("Err:" + e);
});
}
}
}
return helper;
} + '; loadhelper();');
Usage in test
var model = JSON.parse(pm.collectionVariables.get("XYZ_Model"));
// Use helper function from variables
const helper = eval(pm.globals.get("helper"));
pm.test("Validate Model", helper.test(model));
pm.test("Validate Model", helper.test(model.nestedResource, pm.response.json().nestedResource)); // instead of recursion in main helper function as it is danger zone.