Open In App

Automating API Testing with Postman

Last Updated : 26 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Testing the functionality, dependability, and performance of an API is known as API testing. API testing can be done automatically or manually. The technique of automating the execution of API tests with tools is known as automated API testing. This can save time and effort, as well as ensure that APIs are tested more thoroughly.

Why Automate API Testing?

Some of the benefits of automated API testing are:

  1. Speed: Automated tests can be run much faster than manual tests.
  2. Accuracy: Automated tests are less likely to make mistakes than manual tests.
  3. Repeatability: Automated tests can be performed repeatedly without the need for human interaction.
  4. Scalability: Automated tests can be scaled to run on a large number of APIs.

Benefits of using Postman for API Test Automation

Postman is a widely used tool for testing APIs, it provides a user-friendly interface for creating and running API requests and tests. It is used to manually test the API. Additionally provides features that make it easy to perform automated API testing. Postman also supports a variety of features that make it ideal for automating API testing, such as:

  1. Collections: Grouping relevant API requests together can be done with collections. Running multiple tests simultaneously has been made simple.
  2. Environments: These environments allow variables to be stored and reused in API queries and tests. As a result, it becomes easier to test APIs across multiple environments, including development, staging, and production.
  3. Pre-request scripts: Code can be executed before sending an API request through a pre-request script. It can be used to generate test data, set up variables, and perform additional operations.
  4. Test scripts: Test scripts make it possible to verify the response to an API request. This can be used to review the response body, status codes, and other information.

Steps to Automate API Testing with Postman

To automate API testing with Postman using the JSONPlaceholder API(a freely accessible online sample API). let’s walk through how to automate API testing with Postman.

JSONPlacefolder is a mock online REST API for testing and prototyping. This is a great resource for practicing API testing. The base URL of JSONPlacefolder is https://jsonplacefolder.typicode.com.

If you don’t have Postman installed, download and install it.

Step 1: Open Postman application

Step 2: Create a New Collection

  1. Click on the “Collections” tab in the left sidebar.
  2. Click “New Collection.”
  3. Name the collection, e.g., “JSONPlaceholder API Tests.”

Step 3: Create Requests in the Collection

Request: GET Users

  1. Click on the “JSONPlaceholder API Tests” collection.
  2. Click “Add Request” within the collection.
  3. Name the request, e.g., “Get Users.”
  4. Set the request type to GET.
  5. Enter the URL: https://jsonplaceholder.typicode.com/users.
  6. Click “Save.”

Screenshot-2023-10-18-at-10242-(1)

Request: POST Create a Post

1. Click “Add Request” again.

2. Name the request, e.g., “Create a Post.”

3. Set the request type to POST.

4. Enter the URL: https://jsonplaceholder.typicode.com/posts.

5. In the “Body” tab, select “raw” and enter a sample JSON payload:

{

“title”: “Post from Postman”,

“body”: “This is a test post created using Postman”,

“userId”: 1

}

6. Click “Save.”

Screenshot-2023-10-18-at-11912

Step 4: Write Test Scripts

Now, let’s add test scripts to verify the responses.

For “Get Users” Request:

Javascript




// Check if the status code is 200 (OK)
pm.test("Status code is 200", function () {
    pm.response.to.have.status(200);
});
 
// Check if the response has at least one user
pm.test("Response has users", function () {
    // Parse the JSON response
    const jsonData = pm.response.json();
 
    // Check if the response is an array and has at least one element
    pm.expect(jsonData).to.be.an('array').and.to.have.lengthOf.at.least(1);
});


Screenshot-2023-10-18-at-13903-(1)

For “Create a Post” Request:

Javascript




Create a new post
pm.test('Create a new post', async () => {
  const response = await pm.request('Create a Post');
 
  // Check the status code
  pm.expect(response.code).to.equal(201);
 
  // Check the response body
  pm.expect(response.json()).to.have.property('title');
});


Screenshot-2023-10-18-at-13648

1. Click on the “Runner” tab in the top menu.

2. Select the collection “JSONPlaceholder API Tests.”

3. Click “Run JSONPlaceholder API Tests.”

Screenshot-2023-10-18-at-14342-(1)

4. Run the collection.

5. Review the tests results.

Screenshot-2023-10-18-at-14930

Install Postman:

1. Open a terminal window.

2. Run the following command:

npm install -g newman

Note:

This will install the Postman CLI globally.

Postman CLI

Once Postman CLI is installed, run it from any directory on your machine. To schedule tests to run automatically using the Postman CLI, use the following command:

Running Tests Manually:

newman run <collection-name>

Here the collection is “JSONPlaceholder API Tests.postman_collection.json” so we have entered the command.

newman run “JSONPlaceholder API Tests.postman_collection.json”

Run tests manually

Scheduling Tests to Run Automatically:

newman run <collection-name> –schedule <schedule-expression>

schedule-expression is a cron expression th sets the time the test should execute. The cron expression you provided, 0 0 * * *, will run the test every day at midnight.

newman run “JSONPlaceholder API Tests.postman_collection.json” –schedule “0 0 * * *”

1. Open the Postman app.

Screenshot-2023-10-20-at-10243

2. Enter a name for the monitor and select the collection you want to monitor.

Screenshot-2023-10-20-at-11012

3. Configuring a collection-based monitor.

Screenshot-2023-10-20-at-10363

4. Run the tests.

Screenshot-2023-10-20-at-10392-(1)

Best Practices for Automating API Tests in Postman

The following are best practices for Postman API test automation:

  1. Organize tests into collections and folders. This will make it easier to find and run the necessary tests.
  2. Use variables and environments. This will make tests more reusable and portable.
  3. Write reusable tests. This will save time and effort in the long run.
  4. Implement error handling. This will prevent tests from failing if something unexpected happens.
  5. Log test results. This will make it easier to monitor test results and identify any problems.

Conclusion

Postman is a powerful tool for automating API testing. It has many features that make it easy to create, run, and manage automated API tests. APIs are tested more thoroughly and automating API tests with Postman saves time and effort.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads