@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 