@tohlaymui35
There are many ways to do what you want, here are 2 ways that I do what youâre asking:
1) Create a JSON schema, save it as a variable (you could save this as an environment or collection variable) and then test that the response body matches the JSON schema:
Currently I have one request where all my JSON schemas are defined (Iâve been meaning to move this as collection variables but I havenât gotten around to doing this yet)
const schemaNextAction = {
"type": "object",
"title": "Schema - Next Action",
"additionalProperties": false,
"required": [
"next_action_id",
"next_action_by",
"customer_return_id"
],
"properties": {
"next_action_id": {
"type": "integer",
"minimum": 1
},
"next_action_by": {
"type": "string",
"minLength": 1
},
"customer_return_id": {
"type": "integer",
"minimum": 1
}
}
};
pm.environment.set("schemaNextAction", schemaNextAction);
In my âNext Actionâ request, where I actually want to call and use the schema:
const schemaNextAction = pm.environment.get("schemaNextAction");
pm.test("Expects response to match expected JSON Schema", function() {
pm.response.to.have.jsonSchema(schemaNextAction);
});
2. Use â.to.have.propertyâ in your assertions:
const body = pm.response.json();
pm.test("Expects the customer ID to be 12", function() {
pm.expect(body).to.have.property("customerID", 12);
});
The great thing about the above test is that it checks several things:
- It checks that our response body has the key âcustomerIDâ, if it doesnât we get a nice failure message stating this
- It checks that the value for the âcustomerIDâ (assuming that the key exists), is 12. Again, if itâs not - we get a nice failure message.
- It also does a check on type, so if we get â12â and not the int value 12, it fails and we get a nice failure message.
â.to.have.properyâ is my go-to assertion for most things in Postman.
3. You could use the above assertion inside a âforEachâ to test several things (I personally donât do this anymore but I thought Iâd show anyway)
Letâs say you have the below response body you want to test against:
{
"users": [
{
"species": "human",
"name": "Paul",
"age": 30
},
{
"species": "human",
"name": "David",
"age": 32
},
{
"species": "human",
"name": "Lisa",
"age": 29
},
{
"species": "human",
"name": "Craig",
"age": 26
}
]
}
Letâs write a test to assert that every âuserâ is a âhumanâ:
const body = pm.response.json();
pm.test("Expects every user to be 'human'", function() {
body.users.forEach(function(user) {
pm.expect(user).to.have.property("species", "human");
});
});
Like I said before, Iâm not a massive fan of the above example as the failure message isnât great - each user is also not shown in the test results, unless you write it this way (again, I donât like doing this either):
const body = pm.response.json();
body.users.forEach(function(user) {
pm.test("Expects every user to be 'human'", function() {
pm.expect(user).to.have.property("species", "human");
});
});
The above will show a âExpects every user to be âhumanââ pass/fail state for every user in the test results, but this can look messy.
Hope this helps