CI/CD API testing using postman and bitbucket pipeline

You are here:
< Back

Postman is a powerful tool. Everyone that has worked with or developed an API has probably used postman and acknowledged its power.

It has a couple of features that I particularly like:

Shared collection โ€” The more complex an API gets with all the different endpoints, params, and headers, the more the importance of this feature is revealed. Not everyone on the team is involved in developing of all the features of your product, so it is not mandatory to have all the different calls off the top of their head. Having a shared repository of the most common endpoint calls in your project in a centralized place is an efficient way of having everyone on the team know how to use your own API, or the API your product uses.

Testing โ€” You can write tests for every request on your shared collection and run all the tests in a batch. I am not going to explain the benefits of having tests, that is common sense. Postman is packed with tv4 which makes it easy to write tests to verify that your API responses are compliant with the JSON schema.

Topics to be covered:

In order to start testing your APIs using Postman, you will have to,

  • Download the the postman desktop client
  • Sign up with a new account if you don’t already have one (optional but recommended)
  • Create a collection
  • Add requests to the collection
  • Set and update your global variables
  • Use your global variables
  • Write your test cases
  • Export collection
  • Setup bitbucket pipeline to run the test

In this tutorial, I won’t go through downloading the postman client and creating an account since it’s a pretty straight forward thing, hence, I will start from creating a collection.

 

Create a collection:

The term collection is used to group all your API requests under one central place. You can think of a collection as a folder that has multiple files inside, the only difference is a collection won’t have files, but requests instead.

In order to create a collection in the Postman tool, you simply have to click on the new button or the + sign shown in the below screenshot. Once clicked, a window will pop up asking you to give your collection a name and description(optional) and then click on create.

 

 

Add requests to the collection

A collection is nothing without requests, once your collection is created, it’s time to start adding in your API requests. To add a new request to the collection, all you have to do is clicking on the 3 dots button and then choose add request from the menu shown in the below picture.

 

 

When new request is added, the below window will be displayed. Here you just need to specify the API URL, the request method, params if any, headers and finally the request body.

 

 

 

Set and update global variables

By clicking on the gear icon shown in the side of the above picture, a pop up window will be shown, clicking on the global button will allow the user to set new or update existing global variables. The below screenshot shows the global variables I have set for this tutorial.

As the name suggest, variables are values that keep on changing and need to be updated at anyย  point of time. You can either update them manually following the exact same steps when you first created them, or programmatically by navigating to the tests tab in the add request section and then follow the code snippet shown in the screenshot below.

This will update the tgt global variable with the new value received as a response of the sent request.

 

Use your global variables

Global variables allows you to capture a value from one request and passing it to a different request in the same collection/environment. To get the variable value, you need to bind the variable name within double curly brackets as following:

{{variable_name}}

The below picture is showing how I appended the tgt variable captured from a previous request to the current request by supplying it as {{tgt}} in the request URL.

Write your test cases

After finishing setting up the environment by following all the previous steps, it is time to start with writing our test cases. By navigating to the tests tab, we can see that Postman is providing very useful test case templates that can be used. The below snippet is highlighting a sample of the postman ready test templates.

In the request shown in the above snippet, the test case is just written to test for the desired status code which is 201 for this case, but custom test cases can be written too.

Note: Test scripts are written in JavaScript, and are run after the response is received.

 

Export collection

In order to run the collection from the postman CLI (newman), you either have to publish the collection to public using the share collection button and then navigate to the get link tab to generate a unique URL or export the collection as a local JSON file using the export option. Once either way has been completed, we can move to the next step and run the collection from the postman CLI.

 

Setup bitbucket pipeline to run the test

In order to run the collection from the bitbucket pipeline, first thing the cticti/aws-cli:newman-alpine image is the one to be pulled. The newman-alpine image is a custom image that has nodeJs and newman pre-installed. It’s because the newman tool requires a nodeJs environment to execute.

As shown in the below screenshot,

“newman run collection.json –bail”

is the extra step added to the pipeline which is the one used to run the early created collection. As explained in the export collection section, we can either generate a public URL which is not recommended if you don’t want to share your collection with public, or we export the collection as a local JSON file. I have taken the second approach which is exporting my collection locally.

If you want to run a collection from a remote URL, you can replace the collection.json with the collection url as following:

“newman run https://www.getpostman.com/collections/collection_id –bail”

The –bail option is used to force pipeline to fail immediately if any of the test cases failed.

Note: Don’t forget to push the collection.json file to the bitbucket repository ๐Ÿ™‚

 

The End ^_^

 


Did not find what you were looking for? Open a ticket with our Help Centre and we will get back to you resolve your issue.Click Here.
Was this article helpful?
How can we improve this article?
Need help?