Bird
Raised Fist0
Postmantesting~8 mins

Random data generation in Postman - Framework Patterns

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Framework Mode - Random data generation
Folder Structure
postman-project/
├── collections/
│   └── api-requests.postman_collection.json
├── environments/
│   ├── dev.postman_environment.json
│   ├── staging.postman_environment.json
│   └── prod.postman_environment.json
├── scripts/
│   ├── pre-request-scripts/
│   │   └── random-data.js
│   └── test-scripts/
│       └── response-validation.js
├── globals/
│   └── globals.json
├── README.md
└── postman.config.json
Test Framework Layers
  • Collections: Group of API requests to test endpoints.
  • Environments: Define variables like base URLs and credentials for different setups.
  • Pre-request Scripts: JavaScript code that runs before each request to generate random data dynamically.
  • Test Scripts: JavaScript code that runs after requests to validate responses.
  • Globals: Variables accessible across collections and environments.
  • Configuration: Settings for Postman CLI or Newman runs.
Configuration Patterns
  • Environment Variables: Store environment-specific data like URLs and credentials.
  • Global Variables: Store data shared across environments, e.g., random seeds or counters.
  • Pre-request Script for Random Data: Use JavaScript to generate random strings, numbers, or dates and set them as variables before requests.
  • Data Files: Use CSV or JSON files with random or varied data for data-driven testing with Newman.
  • Secure Storage: Keep sensitive data like API keys in environment variables, not hardcoded.
Test Reporting and CI/CD Integration
  • Newman CLI: Run Postman collections in command line with detailed reports.
  • Report Formats: Generate HTML, JSON, or JUnit reports for easy review.
  • CI/CD Pipelines: Integrate Newman runs in pipelines (GitHub Actions, Jenkins, GitLab CI) to automate tests on code changes.
  • Fail Fast: Configure tests to stop on failures to save time.
  • Logging: Use console logs in test scripts to debug random data usage.
Best Practices
  1. Isolate Random Data Generation: Keep random data code in pre-request scripts for easy reuse and maintenance.
  2. Use Meaningful Variable Names: Name random data variables clearly to understand their purpose.
  3. Control Randomness When Needed: Use seeds or fixed values in some tests to reproduce bugs.
  4. Validate Random Data Usage: Write assertions to check that random data is accepted and processed correctly by the API.
  5. Keep Sensitive Data Secure: Never generate or expose sensitive data like passwords in logs or reports.
Self Check

Where in this folder structure would you add a new pre-request script to generate a random email address for user registration tests?

Key Result
Use pre-request scripts in Postman to generate random data dynamically before API calls, organized within a clear folder structure and integrated with environments and CI/CD for robust testing.

Practice

(1/5)
1. What is the main purpose of using random data generation in Postman tests?
easy
A. To create different test inputs each time to check software behavior
B. To make tests run faster by using fixed data
C. To avoid writing any test scripts
D. To store test results permanently

Solution

  1. Step 1: Understand the role of random data

    Random data generation creates new inputs for each test run to simulate varied user inputs.
  2. Step 2: Identify the benefit in testing

    This helps find bugs that fixed data might miss by testing unexpected or edge cases.
  3. Final Answer:

    To create different test inputs each time to check software behavior -> Option A
  4. Quick 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
A. pm.variables.get('randomInt', Math.floor(Math.random() * 100) + 1);
B. var randomInt = Math.floor(Math.random() * 100) + 1;
C. pm.environment.set('randomInt', Math.floor(Math.random() * 100) + 1);
D. pm.randomInt(1, 100);

Solution

  1. Step 1: Identify how to generate random integer in JavaScript

    Math.floor(Math.random() * 100) + 1 generates a number from 1 to 100.
  2. Step 2: Check how to save it in Postman environment

    pm.environment.set('randomInt', value) saves the value for later use in requests.
  3. Final Answer:

    pm.environment.set('randomInt', Math.floor(Math.random() * 100) + 1); -> Option C
  4. Quick 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
A. One of "Alice", "Bob", or "Charlie" randomly
B. "Alice" always
C. An error because names.length is undefined
D. A number between 0 and 2

Solution

  1. Step 1: Understand randomIndex calculation

    Math.floor(Math.random() * names.length) gives 0, 1, or 2 randomly.
  2. Step 2: Check what is stored in 'randomName'

    names[randomIndex] picks a random name from the array.
  3. Final Answer:

    One of "Alice", "Bob", or "Charlie" randomly -> Option A
  4. Quick 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
A. Variable randomNum is not declared
B. randomNum should be rounded to an integer
C. pm.environment.set is used incorrectly
D. Email string concatenation syntax is wrong

Solution

  1. Step 1: Check randomNum value type

    Math.random() * 1000 produces a decimal number like 123.456.
  2. Step 2: Understand email format requirements

    Email should not contain decimals in username part; it should be an integer.
  3. Final Answer:

    randomNum should be rounded to an integer -> Option B
  4. Quick 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
A. const names = ['Anna', 'Ben', 'Cara']; const age = Math.floor(Math.random() * 43) + 18; const email = `user${age}@mail.com`; pm.environment.set('name', names[age]); pm.environment.set('age', age); pm.environment.set('email', email);
B. const names = ['Anna', 'Ben', 'Cara']; const age = Math.random() * 60; const email = 'user' + age + '@mail.com'; pm.environment.set('name', names[age]); pm.environment.set('age', age); pm.environment.set('email', email);
C. const names = ['Anna', 'Ben', 'Cara']; const age = Math.floor(Math.random() * 43) + 18; const email = 'user' + age + '@mail.com'; pm.environment.set('name', names[age]); pm.environment.set('age', age); pm.environment.set('email', email);
D. const names = ['Anna', 'Ben', 'Cara']; const age = Math.floor(Math.random() * 43) + 18; const email = `user${age}@mail.com`; pm.environment.set('name', names[Math.floor(Math.random() * names.length)]); pm.environment.set('age', age); pm.environment.set('email', email);

Solution

  1. Step 1: Check age generation range

    Math.floor(Math.random() * 43) + 18 correctly generates age between 18 and 60.
  2. 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.
  3. Step 3: Confirm environment variable setting

    pm.environment.set is used correctly to save all variables.
  4. Final Answer:

    Option D script correctly generates and saves all variables -> Option D
  5. Quick 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