-
Notifications
You must be signed in to change notification settings - Fork 2
Validating Schema
When testing you want to validate that the API is returning back the correct data. As a tester you might not always know what the data is but you should at least know what it should be. The api documentation states what the schema is. For an example lets look at SWAPI(Star Wars API) and the Film Documentation.
{
...
"created": "2014-12-10T14:23:31.880000Z",
"director": "George Lucas",
"edited": "2014-12-12T11:24:39.858000Z",
"episode_id": 4,
...
}
From this we can see that created, director, and edited are all strings and that episode_id is a number. Numbers are tricky though because they can be an integer
or a number
. A number
allows for decimals where and integer
doesn't. With SWAPI you might think that since it is the number 4 and not 4.0 that it should be integer
instead of number
. What episode_id
is actually returning is an integer object and not an integer
. This means we have to use number
to validate it.
Below is an example of how you can confirm a schema on various different things. These are the different validation types. pm.expect
is a very powerful tool when it comes to testing.
const jsonData = pm.response.json();
pm.test("Has correct schema", function() {
pm.expect(jsonData.str).to.be.a("string", "This is a message that shown on a fail");
pm.expect(jsonData.numArray).to.be.an("array", "This is a message that shown on a fail");
pm.expect(jsonData.numArray[0]).to.be.a("number", "This is a message that shown on a fail");
pm.expect(jsonData.numArray[1]).to.be.a("integer", "This is a message that shown on a fail");
pm.expect(jsonData.bool).to.be.a("boolean", "This is a message that shown on a fail");
});
I used the Postman Cheatsheet to find the different ways to use pm.expect()
-
pm.expect(jsonData.value).to.eql(100);
- Check to see that the value is equal to input
-
pm.expect(pm.response.code).to.be.oneOf([201,202]);
- Checks to see that the response code is one of the following.
-
pm.expect(pm.cookies.has('sessionId')).to.be.true;
- Checks to see that we have a cookie
-
pm.expect(pm.response.text()).to.include('Order placed.');
- Checks to see if the text has the following within it
-
pm.expect(response.products.0.category).to.eql('Detergent');
- This command is going deeper into the json with the nested data. If you can't or are not using the Tiny Validator this is very useful.
They Tiny Validator was a very powerful tool a while ago but it is no longer being updated and hasn't been since February of 2018. It still works but sometimes it won't fail when it should and sometimes the required section is overlooked. You can use AJV but I have yet to see how that is better then just using pm.expect();
.
var jsonData = JSON.parse(responseBody);
var schema = {
"properties": {
"status": {
"type": "string"
},
"totalResults": {
"type": "number"
},
"articles": {
"type": "array",
"properties": {
"source": {
"id": {
},
"name": {
"type": "string"
}
},
"author": {
"type": "string"
},
"title": {
"type": "string"
},
"description": {
"type": "string"
},
"url": {
"type": "string"
},
"urlToImage": {
"type": "string"
},
"publishedAt": {
"type": "string"
//"format": /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z$/
},
"content": {
"type": "string"
}
}
}
}
};
tests["Schema is valid"] = tv4.validate(JSON.parse(responseBody), schema);