Skip to content

How To Do Api Automation Testing Using Postman

API Testing Blog

API Automation Testing Using Postman

Postman is a popular tool for API testing and development. It provides a user-friendly interface for sending API requests, verifying responses, and automating tests.

Setting Up Postman for API Automation Testing

  1. Install Postman: Download and install Postman from https://www.postman.com/.
  2. Create a Workspace: Workspaces help organize your tests and collaborate with team members. Create a workspace for your API testing project.
  3. Import API Definitions (Optional): If your API has a definition (Swagger, OpenAPI, RAML), you can import it into Postman to automatically generate requests and test cases.

Creating Your First API Test

Example API endpoint: https://reqres.in/api/users

  1. Create a Request: In the Postman app, click on the “New” button and select “Request”.
  2. Set the Request Details:
    • Method: Select “GET” for this example.
    • URL: Enter the API endpoint: https://reqres.in/api/users
    • Headers: If required, add headers (e.g., Authorization, Content-Type).

Example GET Request in Postman:

{
"method": "GET",
"url": "https://reqres.in/api/users",
"header": [
{
"key": "Content-Type",
"value": "application/json"
}
]
}
  1. Send the Request: Click the “Send” button.

  2. Verify the Response: Examine the response in the “Body” tab.

Writing Assertions using Postman Tests

Postman provides a built-in scripting language (JavaScript) to add test assertions to your requests.

  1. Navigate to Tests Tab: After sending a request, click on the “Tests” tab.

  2. Write Test Scripts: Use the pm.test() function to create test assertions.

Example Test Script:

pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
pm.test("Response body has a data field", function () {
pm.expect(pm.response.json().data).to.be.an('array');
});

Creating API Test Collections

Collections in Postman help organize your API tests, group them into logical units, and enable running multiple tests in a sequence.

  1. Create a Collection: Click the “Collections” button and create a new collection. Give it a meaningful name (e.g., “User API Tests”).

  2. Add Requests: Drag and drop your API requests into the collection.

  3. Add Test Suites: You can create test suites within a collection to organize your tests further.

Running API Tests and Generating Reports

  1. Run a Single Test: Click the “Send” button on a request to run a single test.

  2. Run a Collection: To execute an entire collection, select the collection and click “Run”.

  3. Generate Reports: Postman provides a built-in reporting feature. Go to “Run” -> “Generate Report” to create a detailed report of your test execution.

Running API Tests in a CI/CD Pipeline

  1. Postman CLI (Command-Line Interface): The Postman CLI allows you to run tests from your CI/CD pipeline script.

  2. Newman (Postman’s CLI Runner): Newman is a command-line tool for running Postman collections and generating reports.

Example Newman Command:

Terminal window
newman run "your_collection_name.json" -e "your_environment.json" -r html -o report.html

Best Practices for Postman API Automation Testing

  • Use Data-Driven Testing: Employ variables and data files to run tests with different input values.

  • Parameterize Your Tests: Define parameters in your requests to make them reusable.

  • Create Reusable Modules: Use JavaScript functions to create reusable modules for common test tasks.

  • Use Pre-request Scripts: Run scripts before each request to set up data or perform other operations.

  • Leverage Assertions: Write robust assertions to ensure your API is behaving as expected.

  • Integrate with CI/CD: Automate your testing process and integrate with your CI/CD pipeline for continuous testing.

API Testing Blog