0
0
Postmantesting~15 mins

Why advanced tests handle complex scenarios in Postman - Automation Benefits in Action

Choose your learning style9 modes available
Verify API handles complex user creation scenarios
Preconditions (3)
Step 1: Open Postman and create a new POST request to the user creation API endpoint
Step 2: In the request body, enter a JSON payload with valid username, email, password, multiple roles, and nested metadata
Step 3: Send the request
Step 4: Verify the response status code is 201 Created
Step 5: Verify the response body contains the created user's id and matches the sent data
Step 6: Repeat the request with missing required fields and verify the API returns 400 Bad Request with appropriate error messages
Step 7: Repeat the request with invalid email format and verify the API returns 422 Unprocessable Entity
Step 8: Repeat the request with duplicate username and verify the API returns 409 Conflict
✅ Expected Result: The API correctly handles complex user creation scenarios by returning appropriate status codes and response bodies for valid and invalid inputs.
Automation Requirements - Postman test scripts
Assertions Needed:
Response status code is as expected (201, 400, 422, 409)
Response body contains expected fields and error messages
Response data matches the request data for successful creation
Best Practices:
Use Postman test scripts with pm.expect assertions
Validate both positive and negative scenarios
Use descriptive test names and comments
Avoid hardcoding values; use variables where appropriate
Automated Solution
Postman
pm.test('Status code is 201 for valid user creation', () => {
    pm.response.to.have.status(201);
});

const responseJson = pm.response.json();
pm.test('Response contains user id', () => {
    pm.expect(responseJson).to.have.property('id');
});

pm.test('Response username matches request', () => {
    const requestJson = pm.request.body.raw ? JSON.parse(pm.request.body.raw) : {};
    pm.expect(responseJson.username).to.eql(requestJson.username);
});

// Negative test for missing fields
if(pm.response.code === 400) {
    pm.test('Response has error message for missing fields', () => {
        pm.expect(responseJson).to.have.property('error');
        pm.expect(responseJson.error).to.include('missing');
    });
}

// Negative test for invalid email
if(pm.response.code === 422) {
    pm.test('Response has error message for invalid email', () => {
        pm.expect(responseJson).to.have.property('error');
        pm.expect(responseJson.error).to.include('email');
    });
}

// Negative test for duplicate username
if(pm.response.code === 409) {
    pm.test('Response has error message for duplicate username', () => {
        pm.expect(responseJson).to.have.property('error');
        pm.expect(responseJson.error).to.include('duplicate');
    });
}

This Postman test script checks the API response after sending a user creation request.

First, it verifies the status code is 201 for a successful creation.

Then it checks the response JSON contains a user id and that the username matches the request data.

For negative scenarios, it uses conditional checks on the status code to verify appropriate error messages for missing fields (400), invalid email (422), and duplicate username (409).

This approach ensures the API handles complex scenarios correctly by validating both success and error responses.

Common Mistakes - 3 Pitfalls
Not checking the response status code before validating the body
Hardcoding values directly in test scripts without using variables
Testing only positive scenarios and ignoring error cases
Bonus Challenge

Now add data-driven testing with 3 different user payloads including valid, missing fields, and invalid email

Show Hint