API Testing using Postman
Testing an API end-to-end (E2E) using Postman involves several steps, from setting up the environment to creating and running test cases. Here’s a step-by-step guide to help you through the process:
Step 1: Install Postman
- Download and Install Postman: Visit the Postman website and download the application suitable for your operating system. Install it following the on-screen instructions.
https://www.postman.com/downloads/
Step 2: Set Up Your API Environment
- Create an Environment:
- Open Postman and click on the
Environment Quick Look
icon (an eye symbol) in the top right corner. - Click
Add
to create a new environment. - Name your environment (e.g., “Development”, “Production”) and add variables like
host_url
,access_token
, etc.
Step 3: Create API Requests
- Create a New Collection:
- Click on the
New
button and selectCollection
. - Name your collection (e.g., “E2E API Tests”) and add a description if needed.
- Add Requests to the Collection:
- Inside the collection, click
Add Request
. - Set the request method (GET, POST, PUT, DELETE, etc.).
- Enter the request URL. Use environment variables for dynamic URLs (e.g.,
{{base_url}}/api-resource
). - Add necessary headers, parameters, and body data as required by your API.
Login Request (Post Method)
Body: Username and Password in Json format where username and password are set in env variable.
Click on send button to send the Request and Server sends the response back
Step 4: Write Test Scripts
- Add Tests to Requests:
- Go to the
Tests
tab of a request. - Use JavaScript to write tests that will validate the response. Here’s an example of basic test scripts:
// Check if the response status code is 200
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
// Check if the response has a specific field
pm.test("Response has 'id' field", function () {
var jsonData = pm.response.json();
pm.expect(jsonData).to.have.property('id');
});
// Validate the response time
pm.test("Response time is less than 200ms", function () {
pm.expect(pm.response.responseTime).to.be.below(200);
});
Step 5: Chain Requests
- Set Up Pre-request Scripts:
- Go to the
Pre-request Script
tab of a request. - Use JavaScript to set up variables or manipulate data before the request is sent.
// Set a variable before making the request
pm.environment.set("username", "john.doe@gmail.com");
pm.environment.set("password", "password123");
2. Use Data from Previous Requests:
- In the
Tests
tab, extract data from the response and store it in a variable.
var jsonData = pm.response.json();
//get the Project Name and UAN for patch Request
pm.environment.set('project_name',responseData.name)
Step 6: Run the Collection
- Use the Collection Runner:
- Click on the
Runner
icon in the top left corner. - Select your collection and environment.
- Configure the number of iterations, delay between requests, and data file (if needed).
Click
Start Run
to execute the requests.
2. Analyze the Results:
- After the run completes, review the results in the Collection Runner.
- Check the status of each request and ensure all tests pass.
Conclusion
By following these steps, you can set up and execute comprehensive end-to-end API tests using Postman. This process ensures your API functions correctly under various scenarios and helps catch issues early in the development cycle.