0
0
Postmantesting~10 mins

Random data generation in Postman - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test sends a POST request with randomly generated user data to an API endpoint. It verifies that the response status is 201 Created and that the response body contains the same random name and email sent.

Test Code - Postman
Postman
pm.test("Create user with random data", function () {
    const randomName = pm.variables.replaceIn("{{$randomFullName}}")
    const randomEmail = pm.variables.replaceIn("{{$randomEmail}}")

    const requestBody = {
        name: randomName,
        email: randomEmail
    };

    pm.sendRequest({
        url: pm.environment.get("apiUrl") + "/users",
        method: 'POST',
        header: {
            'Content-Type': 'application/json'
        },
        body: {
            mode: 'raw',
            raw: JSON.stringify(requestBody)
        }
    }, function (err, res) {
        pm.expect(err).to.be.null;
        pm.expect(res).to.have.property('code', 201);
        const jsonData = res.json();
        pm.expect(jsonData).to.have.property('name', randomName);
        pm.expect(jsonData).to.have.property('email', randomEmail);
    });
});
Execution Trace - 6 Steps
StepActionSystem StateAssertionResult
1Test startsPostman test runner is ready-PASS
2Generate random full name and email using Postman dynamic variablesRandom values assigned to variables randomName and randomEmail-PASS
3Send POST request to API endpoint /users with JSON body containing randomName and randomEmailRequest sent to server with random user data-PASS
4Receive response from serverResponse received with HTTP status code and JSON bodyCheck response status code is 201PASS
5Parse response JSON and verify 'name' and 'email' fields match sent random dataResponse JSON parsed and fields comparedAssert response.name == randomName and response.email == randomEmailPASS
6Test endsTest completed successfully-PASS
Failure Scenario
Failing Condition: API returns status code other than 201 or response body does not contain matching name and email
Execution Trace Quiz - 3 Questions
Test your understanding
What does the test verify after sending the POST request?
AResponse status is 200 and response body is empty
BRequest body contains fixed user data
CResponse status is 201 and response body contains the same random name and email
DResponse status is 404 Not Found
Key Result
Always verify that the response from an API matches the random data sent in the request to ensure the API processes input correctly.