How I learned Postman Tests
About me
I am a Java backend developer with 3 years of experience
Why I wanted to learn Postman Tests
It is a good solution for QA engineers when they want to test the backend API, they can create test cases and run test suite. For backend devs, It is a good possibility to test microservices when you rewrite microservices from one to another. I wanted to learn Postman tests, because I have to write the same apps using different languages.
How I approached learning Postman Tests
Firstly before creating tests, we need the server app with API or we can design API and using the mock server in postman. I show you an example model of API below:
- Create new book (Method: POST; endpoint: /books). When someone wants to create with the same id, the service should return 409 Error
- Delete book by id (Method: DELETE; endpoint: /books/{id})
- Read book by id (Method: GET; endpoint: /books/{id}). When someone wants to read book with unexistent id, the service should return 400 Error
- Update book by id (Method: PUT; endpoint: /books/{id}).When someone wants to update book with unexistent id, the service should return 400 Error
The model of Book:
{
"id": string,
"title": string,
"author": string,
"publisher": string
}
The Test Cases
For every test case we will create separate request in Postman. It will give us a possibility to run a collection like a list of test cases.
-
Create Book
You can see that for the test case, we have 2 Postmans tests (old approach):tests["Status code is 200"] = responseCode.code === 200; tests["Body is correct"] = responseBody === pm.environment.get("body")
On Tests Tab we have an access to specific variables like
tests
,responseCode
,responseBody
.tests
- array of tests, where key is a name of our Postman test;responseCode
- object where one parameter is HTTP response code;responseBody
- string which contains response object;
Test Results:
-
Get Book By id
You can see that for the test case, we have 4 Postmans tests ( different approach):var body = {"id":"123", "title": "Harry Potter", "author": "Rowling", "publisher": "ababahalamaha"}; pm.test("Status code is 200", function () { pm.response.to.have.status(200); }); pm.test("Status code is ok", function () { pm.response.to.have.status("OK"); }); pm.test("Content-Type is present", function () { pm.response.to.have.header("Content-Type"); }); pm.test("Body is correct", function () { pm.response.to.have.body(JSON.stringify(body)); });
-
Update exist book
You can see that for the test case, we have 4 Postmans tests ( different approach, using response.to.be):// example using pm.response.to.be* pm.test("Status code is 200y", function () { // assert that the status code is 200 pm.response.to.be.ok; // info, success, redirection, clientError, serverError, are other variants }); pm.test("response must be with Body", function () { // assert that the response has a valid JSON body pm.response.to.be.withBody; }); pm.test("response must be json", function () { pm.response.to.be.json; // this assertion also checks if a body exists, so the above check is not needed }); pm.test("Body is correct", function () { pm.response.to.have.body(pm.environment.get("body")); });
-
Create Book with Existent Id
You can see that for the test case, we have 3 Postmans tests ( different approach, using pm.expect)var result = JSON.parse(responseBody); tests["Status code is 409"] = responseCode.code === 409; pm.test("Error is correct", function () { var jsonData = pm.response.json(); pm.expect(jsonData.error).to.eql("Conflict"); }); pm.test("Message is correct", function () { pm.expect(pm.response.text()).to.include("The book with the same id was created before"); });
-
Get Book By non-existent id
- Response code equals 404;
- Response body has to contain
error: "Not Found"
andmessage:"Book with current id is not exist"
You can try make your own Postman tests.
-
Update non-existent book
- Response code equals 404;
- Response body has to contain
error: "Not Found"
andmessage:"Book with current id is not exist"
-
Delete Book
- Response code equals 200;
How to Run All Test Cases
OK, seriously I had big problem with it, collection didn't start!!!))) Seriously)
Don't forgot to save your requests!!!
-
Click button in red below
-
Click Run;
-
Click Run Book
And Whoallya!!! Our Result!!!
Tips and advice
You need to have basic of JS and don't forget to save your requests))
Final thoughts and next steps
I think in the future you can try to integrate Postman Tests in your CI/CD or you can try to write tests in BDD style.
I wish I had seen your post before starting testing. Great thanks!