DEV Community

Saloni Saraiya
Saloni Saraiya

Posted on

How to Automate API Testing Using Postman

postman-api-testing

Hey, How are you? Must be tired of testing the Same API again and again, I guess that’s why you are here🙊️.

Well, No worries. I have good news for you, Let’s automate it. 😁️

automation-gif

APIs are the heart of any backend application. Whether it is service-based or monolithic architecture, it requires robust and efficient APIs to support business requirements.

In most applications, APIs contain business logic and database operations so a loophole in a single API can crash the application. As requirements keep adding, the application grows and it’s hard to test each and every developed API.

Most of the developers (Including me 😛️) becomes lazy, when writing the unit-test cases. But when requirements grows, these test-cases becomes boon to fast and secure development. And we just have to write it once so DO IT.

Automating API testing to ensure fast development and code quality would be a great help for an API developer.
Postman is a collaborative API development and testing tool which takes care of both manual and automated API testing and is widely used by many developers.

In this blog, I will take some use-cases to demonstrate how you can setup tests for your APIs through the visual interface as well as CLI.

Getting Started 🚀

For a better understanding, let’s take an example of a user record create API, and we have to login into the system to use the API. So the scenario is that every time you need to test user-create API, you will first call the login API, store the token to process further. Let’s understand our use-case

Scenario: Store JWT token from the Login API’s result into the environment and then call user-create API, with that token.

Pre-request Script: A section to set up test data for APIs. Here we will set up user-name and password for our login API and user information for creat-API.

Test: A section where we can test/store API responses. Here we will check the status code and store the token in the postman environment for our Login API. Also, we will add some validation test cases for user-created API.

Automating testing with Postman GUI.

Steps:

  • First, we will create a Login API request in postman.

step1

  • Now, we will add a pre-request Script for setting the username and password.

step2

As shown. a variable is set in the pre-request script. now we will use that in the body of our request.

step3

Pre-request script syntax is similar to the test cases that we write programmatically (for eg. jest) So if you know it, bonus 😉

For more, Pre-request scripts in postman.

  • Next, We will add a test to save a token to the environment on a 200-OK response.

stpe4

Token will be saved in the currently selected environment.

  • Now we will set that environment variable in Authentication as Bearer token to call user-create API.

  • For that, we can use a pre-request script(useful when running collection through CLI). or we can directly use the Authorization tab of the postman. We can set it from the headers tab as well. (Postman is so cool 🤩)

  • We will use the Authorization tab

This way we can set different validations tests in Test to check the API response.

Example

  1. Unique username of User

example

Now we will save all the requests in a postman collection and we will run it.

run collection

You can set the sequence of a requests while running the collection.

  • Test Results

Test Results

Automating testing Using CLI

If you are a CLI lover, Postman got you covered there too. 😎️

Postman CLI package is a npm package, and requires node to be installed in your device to run.

Check the Detailed Information about Newman.

Let’s dive right into it.

Steps:

  1. for running tests from CLI, you need to have a postman collection. So I suggest you to setup requests, tests, environment and export the collection and environment file from postman to process further.

  2. Install newman in your device using npm.
    npm install newman

  3. Run the collection
    newman run ${collection_file_path}

  4. It will generate a report in your Console that will look like this.

report1

report2

The report contains API responses and test results.

There’s one catch of using this package is that, you can not change the sequence of the API requests.

I hope now you know, How to unit test all your APIs in automate mod️e and won't crash your server.😉️

testing

References:
Postman

About me:
I am a Javascript Developer at DhiWise. It’s pro-code devtool that generates production ready code for 6 technologies. Check it out if you are tech-geek 😋️

You can find me on LinkedIn and we can talk about cool devtools like the one I am developing.

Keep coding👩‍💻️, keep testing. Bye. 👋️

Top comments (1)

Collapse
 
mmfalcao87s profile image
Marcel F. Falcão

Try using Newman and the syntax from postman as well