Preconditions and Postconditions in Postman - Notes By ShariqSP
Preconditions and Postconditions in Postman for Randomized User Registration
Scenario Overview
This example demonstrates setting up preconditions and postconditions for a Register User API endpoint, where user details need to be unique for each test. In this scenario, preconditions generate random usernames and emails to avoid conflicts, while postconditions ensure the cleanup of test data by deleting the newly created user. This approach maintains a consistent test environment.
1. Register User Endpoint
Endpoint: POST {{base_url}}/api/users/register
Description: This endpoint creates a new user by accepting user details (username, email, mobile, and password) in the request body.
Precondition: Generating Random User Data
To prevent duplicate user entries, we’ll generate random values for username and email in the Pre-Request Script of the registration request. This ensures that each test instance has a unique user profile.
// Pre-Request Script to generate random user details
// Generate a random number for unique usernames and emails
const randomId = Math.floor(Math.random() * 10000);
const username = `user_${randomId}`;
const email = `user_${randomId}@example.com`;
// Set environment variables for the generated data
pm.environment.set("username", username);
pm.environment.set("email", email);
pm.environment.set("mobile", "1234567890");
pm.environment.set("password", "password123");
// Log the generated details for verification
console.log("Generated Username:", username);
console.log("Generated Email:", email);
In this script:
- A random number (
randomId) is generated to ensure unique user details. - Using this
randomId, we create a uniqueusernameandemailfor each test. - These values are saved in environment variables (
username,email,mobile,password), which are referenced in the request body.
Request Body Setup
In the Register User request, use the pre-set environment variables in the JSON body:
{
"username": "{{username}}",
"email": "{{email}}",
"mobile": "{{mobile}}",
"password": "{{password}}"
}
Postcondition: Cleanup of Test User Data
Once the registration test completes, it’s important to delete the test user to avoid buildup of test data. In the Tests tab of the Register User request, we can add a Postcondition script that sends a DELETE request to remove the newly created user based on their userId, obtained from the registration response.
// Postcondition Script to delete the test user
// Retrieve userId from registration response
const userId = pm.response.json().data.id;
// Send a DELETE request to remove the test user
pm.sendRequest({
url: `{{base_url}}/api/users/delete/${userId}`,
method: "DELETE",
header: {
"Content-Type": "application/json",
"Authorization": "Bearer {{auth_token}}" // Use an admin or appropriate token if required
}
}, function (err, res) {
if (err) {
console.log("Error deleting test user:", err);
} else {
console.log("Test user deleted successfully.");
}
});
// Clear environment variables used for user registration
pm.environment.unset("username");
pm.environment.unset("email");
pm.environment.unset("mobile");
pm.environment.unset("password");
In this postcondition script:
- The
userIdis extracted from the response of the registration request. - A
DELETErequest is sent to remove the test user, ensuring a clean environment for future tests. - All relevant environment variables are cleared after the test to reset the environment.
Benefits of Using Preconditions and Postconditions in This Scenario
Implementing preconditions and postconditions in this way has several advantages:
- Preconditions: Guarantee that unique user data is generated dynamically, avoiding issues with duplicate usernames or emails in subsequent test runs.
- Postconditions: Ensure the system is cleaned up after each test by deleting test users, preventing data clutter and potential interference with future tests.
Using this approach in Postman provides a robust and isolated testing environment, allowing reliable and reusable tests for the Register User endpoint in the user management module.