0
0
Postmantesting~5 mins

Generating dynamic data in Postman

Choose your learning style9 modes available
Introduction

Generating dynamic data helps tests use fresh, unique values each time. This avoids errors from repeated data and makes tests more real.

When creating new users in a test to avoid duplicate emails.
When testing APIs that require unique IDs or timestamps.
When filling forms with random but valid data to check input handling.
When simulating different user inputs in automated tests.
Syntax
Postman
pm.variables.set('variableName', pm.variables.replaceIn('{{ $randomDataType }}'));

Use pm.variables.set to save dynamic data for later use in the test.

{{ $randomDataType }} is a Postman dynamic variable like {{ $randomEmail }} or {{ $randomInteger }}.

Examples
This sets a variable userEmail with a random email address.
Postman
pm.variables.set('userEmail', pm.variables.replaceIn('{{ $randomEmail }}'));
This sets a variable userId with a unique random UUID.
Postman
pm.variables.set('userId', pm.variables.replaceIn('{{ $randomUUID }}'));
This sets a variable randomNumber with a random integer.
Postman
pm.variables.set('randomNumber', pm.variables.replaceIn('{{ $randomInteger }}'));
Sample Program

This test generates a random email and UUID, saves them as variables, and checks their format. It prints the values in the console.

Postman
pm.test('Generate dynamic user data', () => {
    const email = pm.variables.replaceIn('{{ $randomEmail }}');
    const id = pm.variables.replaceIn('{{ $randomUUID }}');
    pm.variables.set('userEmail', email);
    pm.variables.set('userId', id);
    pm.expect(email).to.include('@');
    pm.expect(id).to.match(/[0-9a-fA-F-]{36}/);
    console.log('Generated Email:', email);
    console.log('Generated UUID:', id);
});
OutputSuccess
Important Notes

Dynamic variables in Postman start with {{ $random }} and cover emails, UUIDs, integers, strings, and more.

Use pm.variables.set to keep the generated data for use in later requests or tests.

Check the generated data format with assertions to catch errors early.

Summary

Dynamic data makes tests flexible and realistic.

Postman provides many built-in random data types.

Always save and verify dynamic data for reliable tests.