Writing Tests in Postman
In previous blog post, we saw how to use BDD format for writing test cases in postman. Most important part of writing tests in postman is understanding various features available. Let us explore various options available . The examples specified in postman documentation, have lot of information about how to setup postman bdd, use chai http assertions, create custom assertions and use before and after hooks. Please import them into postman and try that by yourself to familiarise with postman BDD. Below is only few examples from them.
Postman BDD makes use of Chai Assertion Library and Chai-Http. We have access to both libray and postman scripting environment for writing test cases. Chai has two types of assertion styles.
Expect/should
for BDDAssert
for TDD
Both styles support chainable language to construct assertions. We can use both of them to write postman test assertions. If you need details of all chainable constructs, please refer to their documentation. Major ones which we may use in postman tests are
Chains
to be been is that which and has have with at of same but does
Not - Negates all conditions
- any -
- all -
- inlcude
- OK
- true
- false
- null
- undefined
- exist
- empty
- match(re[, msg])
Chai-Http module provide various assertions. Read through their documentation here to know details. Below are main commands at our disposal for validation .
- .status(code)
- .header (key[, value])
- .headers
- .ip
- .json / .text / .html
- .redirect
- .param
- .cookie
Postman bdd provide response
object on which we do most of assertions. It will have all information like response.text, response.body, response.status, response.ok , response.error. Postman BDD will automatically parse JSON and XML responses and hence there is no need to call JSON.parse() or xml2json(). response.text will have unparsed content. It also have automatic error handling , which will allow to continue with other test even if something fails.
Examples for various assertions done on response object are below
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
|
If we use above assertions in proper BDD format, it will look like below
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
|