How To Do Api Automation Testing Using Postman
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
- Install Postman: Download and install Postman from https://www.postman.com/.
- Create a Workspace: Workspaces help organize your tests and collaborate with team members. Create a workspace for your API testing project.
- 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
- Create a Request: In the Postman app, click on the “New” button and select “Request”.
- 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" } ]}
-
Send the Request: Click the “Send” button.
-
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.
-
Navigate to Tests Tab: After sending a request, click on the “Tests” tab.
-
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.
-
Create a Collection: Click the “Collections” button and create a new collection. Give it a meaningful name (e.g., “User API Tests”).
-
Add Requests: Drag and drop your API requests into the collection.
-
Add Test Suites: You can create test suites within a collection to organize your tests further.
Running API Tests and Generating Reports
-
Run a Single Test: Click the “Send” button on a request to run a single test.
-
Run a Collection: To execute an entire collection, select the collection and click “Run”.
-
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
-
Postman CLI (Command-Line Interface): The Postman CLI allows you to run tests from your CI/CD pipeline script.
-
Newman (Postman’s CLI Runner): Newman is a command-line tool for running Postman collections and generating reports.
Example Newman Command:
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.