API Testing with Postman: Practical Examples

Explore practical examples of using Postman’s built-in tools for effective API testing.
By Jamie

Introduction

Postman is a powerful tool for API testing that comes equipped with built-in features designed to simplify the process. These features allow developers and testers to streamline their workflow, ensuring that APIs function as intended. In this article, we will explore three diverse examples of using Postman’s built-in tools for API testing, showcasing how they can be applied in various scenarios.

Example 1: Validating API Responses with Tests

Context

In this example, we’ll validate the response of a GET request to ensure that the API returns the expected data structure and values. This is crucial for ensuring that the API behaves as documented.

You can create tests in Postman to automatically verify that the API response meets certain criteria.

pm.test("Status code is 200", function () {
    pm.response.to.have.status(200);
});

pm.test("Response contains user ID", function () {
    const jsonData = pm.response.json();
    pm.expect(jsonData).to.have.property('userId');
});

pm.test("Response time is less than 200ms", function () {
    pm.expect(pm.response.responseTime).to.be.below(200);
});

This example checks that the response status code is 200, verifies that the response contains a user ID, and ensures that the response time is under 200 milliseconds.

Notes

  • Variations can include checking for specific values or additional properties in the response object.
  • You can also use Postman’s built-in snippets to quickly generate test scripts.

Example 2: Automating API Tests with Collections

Context

Using Postman Collections allows you to group related requests and run them together. This is particularly useful for testing an entire API endpoint or simulating a user journey through the API.

In this example, we will create a collection that tests various endpoints of a user management API.

  1. Create a new collection named “User Management API Tests.”
  2. Add requests for endpoints such as:

    • GET /users
    • POST /users
    • PUT /users/{id}
    • DELETE /users/{id}
  3. For each request, add tests similar to the first example to validate the responses.
  4. Use the Collection Runner to execute all tests in sequence.

Notes

  • The Collection Runner allows you to run tests with different sets of data by importing a CSV or JSON file.
  • Postman also enables you to monitor collections for continuous integration.

Example 3: Using Environment Variables for Dynamic Testing

Context

Environment variables in Postman are invaluable for testing APIs with different configurations. They allow you to easily switch between development, testing, and production environments without changing the requests manually.

In this example, we will set up an environment variable to store the API base URL and use it in requests.

  1. Create a new environment called “Development.”
  2. Add a variable named baseUrl with the value https://api.dev.example.com.
  3. Use this variable in your requests as follows:

    • For a GET request, set the URL to {{baseUrl}}/users.
    • For a POST request, set the URL to {{baseUrl}}/users and use the following body:

      {
        "name": "John Doe",
        "email": "john@example.com"
      }
      
  4. After testing, switch to a different environment (e.g., Production) by changing the baseUrl variable.

Notes

  • Environment variables help manage sensitive data such as API keys and tokens securely.
  • Use Postman’s pre-request scripts to dynamically set variables before each request.

By leveraging these examples of using Postman’s built-in tools for API testing, you can enhance your testing process, ensuring your APIs are reliable and efficient.