Codementor Events

How I learned Postman Tests

Published Jun 21, 2019
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:

  1. Create new book (Method: POST; endpoint: /books). When someone wants to create with the same id, the service should return 409 Error
  2. Delete book by id (Method: DELETE; endpoint: /books/{id})
  3. Read book by id (Method: GET; endpoint: /books/{id}). When someone wants to read book with unexistent id, the service should return 400 Error
  4. 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.

  1. Create Book
    Screen Shot 2019-06-20 at 5.54.02 PM.png
    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:
    Screen Shot 2019-06-21 at 3.23.32 PM.png

  2. Get Book By id
    Screen Shot 2019-06-21 at 4.04.59 PM.png
    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));
        });
    
  3. Update exist book
    Screen Shot 2019-06-21 at 4.56.52 PM.png
    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"));
      });
    
  4. Create Book with Existent Id
    Screen Shot 2019-06-21 at 5.10.56 PM.png
    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");
    });
    
  5. Get Book By non-existent id

    1. Response code equals 404;
    2. Response body has to contain error: "Not Found" and message:"Book with current id is not exist"

    You can try make your own Postman tests.

  6. Update non-existent book

    1. Response code equals 404;
    2. Response body has to contain error: "Not Found" and message:"Book with current id is not exist"
  7. Delete Book

    1. 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!!!

  1. Click button in red below
    Screen Shot 2019-06-21 at 5.29.55 PM.png

  2. Click Run;
    Screen Shot 2019-06-21 at 5.26.43 PM.png

  3. Click Run Book
    Screen Shot 2019-06-21 at 5.27.48 PM.png
    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.

Discover and read more posts from Yurii Kachmar
get started
post commentsBe the first to share your opinion
ERICKA GONZALEZ
4 years ago

I wish I had seen your post before starting testing. Great thanks!

Show more replies