0
0
Postmantesting~10 mins

Generating dynamic data in Postman - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test sends a POST request with dynamic user data generated at runtime. It verifies that the API responds with a success status and the correct user name in the response.

Test Code - Postman Test Script
Postman
pm.test("Create user with dynamic data", function () {
    // Generate dynamic user data
    const randomId = Math.floor(Math.random() * 10000);
    const userName = `user_${randomId}`;
    
    // Set request body with dynamic data
    pm.request.body.update({
        mode: 'raw',
        raw: JSON.stringify({
            name: userName,
            email: `${userName}@example.com`
        })
    });

    // Send the request
    pm.sendRequest(pm.request, function (err, res) {
        pm.expect(err).to.be.null;
        pm.expect(res).to.have.property('status', 201);
        const jsonData = res.json();
        pm.expect(jsonData.name).to.eql(userName);
    });
});
Execution Trace - 5 Steps
StepActionSystem StateAssertionResult
1Test starts and generates a random user IDRandom number generated, userName variable set to 'user_<randomId>'-PASS
2Updates the request body with dynamic userName and emailRequest body now contains JSON with dynamic name and email-PASS
3Sends POST request to create user with dynamic dataRequest sent to API endpoint-PASS
4Receives response and checks for no error and status code 201Response received with status code 201Assert err is null and response status is 201PASS
5Parses response JSON and verifies returned user name matches dynamic userNameResponse JSON contains user dataAssert response JSON name equals generated userNamePASS
Failure Scenario
Failing Condition: API returns error or status code other than 201, or returned user name does not match dynamic data
Execution Trace Quiz - 3 Questions
Test your understanding
What does the test verify after sending the POST request?
AThe request body is empty
BThe API returns status code 404
CThe API returns status code 201 and the user name matches the dynamic data
DThe user name is always 'user_1234'
Key Result
Generating dynamic data in tests helps avoid conflicts and makes tests more reliable by simulating real-world unique inputs.