This tutorial will guide you through 4 different ways (as Random API for Postman) to generate random inputs for your API testing. You can use them for test data generation in Postman. These methods include using built-in Postman features and incorporating an external library (Faker) for more advanced scenarios, and two more.
Prerequisites
Please make sure the following two things to start using the Postman.
- Postman Installed
Make sure you have Postman installed on your machine. If not, download and install it from Postman’s official website. - Active Internet Connection
Ensure you have a stable internet connection to access both built-in and external sources for random data.
Below we have explained the four methods. You can easily follow these for using Postman APIs to generate random test data.
Method 1: Randomuser(dot)me as Random API for Postman
This method involves using the Randomuser(dot)me
website to generate random data for testing. It allows fetching details like names, emails, and more, making it easy to simulate diverse scenarios in Postman for API testing.
Step 1: Create a New Request in Postman
// Postman Pre-request Script
const response = pm.sendRequest({
url: "https://randomuser.me/api/",
method: "GET",
});
const randomUserData = JSON.parse(response.body);
// Set variables for reuse
pm.environment.set("randomFirstName", randomUserData.results[0].name.first);
pm.environment.set("randomLastName", randomUserData.results[0].name.last);
// Add more variables as needed
Step 2: Use Dynamic Inputs in REST API Requests
In your REST API testing requests, replace static data with variables:
{
"firstName": "{{randomFirstName}}",
"lastName": "{{randomLastName}}",
// Add more fields as needed
}
Step 3: Automate Testing with Collections
Create a Postman collection to run the Randomuser(dot)me
request and subsequent REST API testing requests in a sequence.
Method 2: Use External Libraries As Random API for Postman
In Method 2, we use tools like Faker to create specific and realistic random data in Postman. This adds more detail to our testing scenarios, making them closer to real-world situations.
Step 1: Install the ‘Faker’ Library
// Postman Pre-request Script
const faker = require('Faker');
// Set vars for reuse
pm.environment.set('randEmail', faker.internet.email());
pm.environment.set('randPhone', faker.phone.phoneNumber());
// Add more vars as needed
Step 2: Use Dynamic Inputs in REST API Requests
Replace static data in your REST API requests with variables:
{
"email": "{{randEmail}}",
"phone": "{{randPhone}}",
// Add more fields as needed
}
Step 3: Automate Advanced Testing with Collections
Create a Postman collection to run the Faker library script and subsequent REST API testing requests in a sequence.
Method 3: Use Postman Random Functions for In-Place Data Generation
In Method 3, we used the standard JS Math library. It provides simple functions to efficiently generate random data within Postman, easing up the testing process.
Step 1: Utilize Built-in Random Functions in Pre-request Script
// Postman Pre-request Script for Method 3
const getRandStr = (length) => {
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
let rc = "";
for (let i = 0; i < length; i++) {
const randIdx = Math.floor(Math.random() * charset.length);
rc += charset.charAt(randIdx);
}
return rc;
};
// Set variables for reuse
pm.variables.set("randFirstName", getRandStr(8));
pm.variables.set("randLastName", getRandStr(10));
Step 2: Use Dynamic Inputs in REST API Requests
In your REST API testing requests, directly use the generated random data:
{
"firstName": "{{randFirstName}}",
"lastName": "{{randLastName}}",
// Add more fields as needed
}
Step 3: Automate Testing with Collections
Create a Postman collection to run requests utilizing in-place generated random data for REST API testing.
Method 4: Postman Dynamic Variables with Timestamps
In Method 4, in place of a random API for Postman, we used dynamic variables (e.g. timestamp) for testing. Using timestamps is a reliable way to generate random inputs.
Step 1: Leverage Timestamps for Uniqueness
In this method, we’ll leverage timestamps to create unique and random-like data.
// Postman Pre-request Script for Method 4
const timestamp = new Date().getTime(); // Get current timestamp in msec
// Set variables for reuse
pm.variables.set("customTimestamp", timestamp);
Step 2: Use Dynamic Variables in REST API Requests
In your REST API testing requests, directly use the generated timestamp:
{
"customTimestamp": "{{customTimestamp}}",
// Add more fields as needed
}
Step 3: Automate Advanced Testing with Collections
Create a Postman collection to run requests utilizing dynamic variables with timestamps for REST API testing.
In this method, we are using the timestamp as a unique identifier for advanced data generation. The timestamp provides a high level of uniqueness. Hence, it is suitable for scenarios where unique or random-like values are required.
Must Read:
1. What is Regression Testing? How to Do It? Provide Examples.
2. What is Penetration Testing? How to Do It? Provide Examples.
3. What is the Software Development Life Cycle (SDLC)?
4. What is the Software Testing Life Cycle (SDLC)?
5. How to Generate Report in Selenium Webdriver?
6. How to Generate Extent Report in Selenium?
7. How to Zoom In/Out in Selenium Webdriver Using Java?
Before You Leave
Congratulations! You’ve seen four different Random API approaches for Postman and generate random inputs for REST API testing. With these details, you can further explore by changing parameters, data points, and libraries to suit your specific testing needs.
Feel free to try, even change this approach, and see how it fits into your testing process. We hope this tutorial gave you a unique perspective on using Random APIs for Postamn. If you have any further questions or concerns, please let us know.
Lastly, we need your support to continue. If you like our tutorials, share this post on social media like Facebook/Twitter.
Happy testing!