Random data helps test software with different inputs. It finds hidden bugs by using new values every time.
Random data generation in Postman
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
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
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));
Postman
// Random lowercase string of length 6 pm.variables.set('randStr', Math.random().toString(36).substring(2, 8));
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}`);
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.
Practice
1. What is the main purpose of using random data generation in Postman tests?
easy
Solution
Step 1: Understand the role of random data
Random data generation creates new inputs for each test run to simulate varied user inputs.Step 2: Identify the benefit in testing
This helps find bugs that fixed data might miss by testing unexpected or edge cases.Final Answer:
To create different test inputs each time to check software behavior -> Option AQuick Check:
Random data = varied inputs [OK]
Hint: Random data means new inputs every test run [OK]
Common Mistakes:
- Thinking random data makes tests faster
- Believing random data avoids scripting
- Confusing data storage with data generation
2. Which Postman syntax correctly generates a random integer between 1 and 100?
easy
Solution
Step 1: Identify how to generate random integer in JavaScript
Math.floor(Math.random() * 100) + 1 generates a number from 1 to 100.Step 2: Check how to save it in Postman environment
pm.environment.set('randomInt', value) saves the value for later use in requests.Final Answer:
pm.environment.set('randomInt', Math.floor(Math.random() * 100) + 1); -> Option CQuick Check:
Use pm.environment.set to save random data [OK]
Hint: Use pm.environment.set to store random values [OK]
Common Mistakes:
- Using pm.variables.get to set a value
- Not saving the random value for reuse
- Using a non-existent pm.randomInt function
3. Given this Postman script snippet, what will be the output stored in the environment variable 'randomName'?
const names = ['Alice', 'Bob', 'Charlie'];
const randomIndex = Math.floor(Math.random() * names.length);
pm.environment.set('randomName', names[randomIndex]);medium
Solution
Step 1: Understand randomIndex calculation
Math.floor(Math.random() * names.length) gives 0, 1, or 2 randomly.Step 2: Check what is stored in 'randomName'
names[randomIndex] picks a random name from the array.Final Answer:
One of "Alice", "Bob", or "Charlie" randomly -> Option AQuick Check:
Random index picks random name [OK]
Hint: Random index picks from array length [OK]
Common Mistakes:
- Assuming output is always first element
- Thinking names.length is undefined
- Expecting a number instead of a name
4. Identify the error in this Postman pre-request script for generating a random email:
var randomNum = Math.random() * 1000;
pm.environment.set('email', 'user' + randomNum + '@test.com');medium
Solution
Step 1: Check randomNum value type
Math.random() * 1000 produces a decimal number like 123.456.Step 2: Understand email format requirements
Email should not contain decimals in username part; it should be an integer.Final Answer:
randomNum should be rounded to an integer -> Option BQuick Check:
Round random numbers for clean strings [OK]
Hint: Round random numbers before string use [OK]
Common Mistakes:
- Ignoring decimal values in emails
- Misusing pm.environment.set syntax
- Not declaring variables properly
5. You want to generate a random user profile in Postman with a name, age (18-60), and email. Which script correctly generates and saves all three as environment variables?
hard
Solution
Step 1: Check age generation range
Math.floor(Math.random() * 43) + 18 correctly generates age between 18 and 60.Step 2: Verify name selection and email format
names[Math.floor(Math.random() * names.length)] picks a random name; email uses template string with age.Step 3: Confirm environment variable setting
pm.environment.set is used correctly to save all variables.Final Answer:
Option D script correctly generates and saves all variables -> Option DQuick Check:
Random index + correct age range + pm.environment.set [OK]
Hint: Use Math.floor and correct index for arrays [OK]
Common Mistakes:
- Using age as array index
- Not rounding random numbers
- Incorrect string concatenation
