0
0
Postmantesting~5 mins

Random data generation in Postman

Choose your learning style9 modes available
Introduction

Random data helps test software with different inputs. It finds hidden bugs by using new values every time.

When you want to test a form with different names or emails.
When you need unique IDs for each test run.
When testing how the system handles unexpected or varied inputs.
When you want to avoid using real user data for privacy reasons.
When you want to simulate real user behavior with changing data.
Syntax
Postman
// Generate a random integer
pm.variables.set('randomInt', Math.floor(Math.random() * 100));

// Generate a random string
pm.variables.set('randomString', Math.random().toString(36).substring(2, 10));

// Generate a random email
pm.variables.set('randomEmail', `user${Math.floor(Math.random() * 10000)}@example.com`);

Use pm.variables.set to save random data for later use in requests.

Random data is generated fresh each time the script runs.

Examples
This sets a variable randNum with a random number from 0 to 50.
Postman
// Random number between 0 and 50
pm.variables.set('randNum', Math.floor(Math.random() * 51));
This creates a random string with letters and numbers, 6 characters long.
Postman
// Random lowercase string of length 6
pm.variables.set('randStr', Math.random().toString(36).substring(2, 8));
This generates a random email like test123@mail.com for testing signup forms.
Postman
// Random email for testing
pm.variables.set('randEmail', `test${Math.floor(Math.random() * 1000)}@mail.com`);
Sample Program

This script runs before a request in Postman. It creates random user ID, name, and email. Then it saves them as variables to use in the request.

Postman
// Postman Pre-request Script
// Generate random user data
const randomId = Math.floor(Math.random() * 10000);
const randomName = Math.random().toString(36).substring(2, 8);
const randomEmail = `user${randomId}@example.com`;

pm.variables.set('userId', randomId);
pm.variables.set('userName', randomName);
pm.variables.set('userEmail', randomEmail);

console.log(`Generated userId: ${randomId}`);
console.log(`Generated userName: ${randomName}`);
console.log(`Generated userEmail: ${randomEmail}`);
OutputSuccess
Important Notes

Random data helps catch bugs that fixed data might miss.

Always check that random values fit the expected format your system needs.

Use console logs in Postman to see generated values during testing.

Summary

Random data generation creates new test inputs each time.

It helps test software with varied and unexpected data.

Postman scripts can generate and save random data for requests.