0
0
Postmantesting~15 mins

Generating dynamic data in Postman - Build an Automation Script

Choose your learning style9 modes available
Generate dynamic user data for API request
Preconditions (2)
Step 1: Open Postman and create a new POST request to https://api.example.com/users
Step 2: In the Body tab, select raw and JSON format
Step 3: Add JSON body with fields: name, email, and phone
Step 4: Use Postman dynamic variables to generate random name, email, and phone
Step 5: Send the request
Step 6: Verify the response status is 201 Created
Step 7: Verify the response body contains the same name and email as sent
✅ Expected Result: The API accepts the dynamically generated user data and returns status 201 with the correct user details in the response body
Automation Requirements - Postman Tests (JavaScript)
Assertions Needed:
Response status code is 201
Response body contains the sent name
Response body contains the sent email
Best Practices:
Use Postman dynamic variables like {{$randomFirstName}}, {{$randomEmail}}
Write assertions in the Tests tab using pm.expect
Keep test scripts clear and maintainable
Automated Solution
Postman
pm.test('Status code is 201', function () {
    pm.response.to.have.status(201);
});

const responseJson = pm.response.json();

pm.test('Response contains sent name', function () {
    pm.expect(responseJson.name).to.eql(pm.variables.get('name'));
});

pm.test('Response contains sent email', function () {
    pm.expect(responseJson.email).to.eql(pm.variables.get('email'));
});

This test script runs after the request is sent.

First, it checks the response status is 201, meaning the user was created successfully.

Then it parses the JSON response body.

It asserts that the response's name and email fields match the values sent in the request, which are stored in Postman variables.

This ensures the API accepted the dynamic data correctly.

Common Mistakes - 3 Pitfalls
Hardcoding user data instead of using dynamic variables
Not storing dynamic data in variables for assertions
Using incorrect assertion syntax in Postman tests
Bonus Challenge

Now add data-driven testing by running the request with 3 different sets of dynamic user data automatically

Show Hint