0
0
Postmantesting~15 mins

Output block in Postman - Build an Automation Script

Choose your learning style9 modes available
Verify the output block in Postman test script
Preconditions (2)
Step 1: Send the API request
Step 2: Go to the Tests tab in Postman
Step 3: Write a test script that captures the response body
Step 4: Use the output block to log the response status and body
Step 5: Run the request and observe the output block in the Postman console
✅ Expected Result: The Postman console shows the response status code and the response body logged correctly in the output block
Automation Requirements - Postman test scripts (JavaScript)
Assertions Needed:
Verify response status code is 200
Verify response body contains expected keys or values
Best Practices:
Use pm.response to access response data
Use console.log for output block logging
Write clear and simple assertions
Keep test scripts readable and maintainable
Automated Solution
Postman
pm.test('Status code is 200', () => {
    pm.response.to.have.status(200);
});

const responseBody = pm.response.json();
console.log('Response Status:', pm.response.code);
console.log('Response Body:', responseBody);

pm.test('Response body has userId', () => {
    pm.expect(responseBody).to.have.property('userId');
});

This script first checks if the response status code is 200 using pm.test and pm.response.to.have.status. Then it parses the response body as JSON and logs the status code and body to the Postman console using console.log, which acts as the output block. Finally, it asserts that the response body contains a key named userId. This approach helps verify the API response and outputs useful information for debugging.

Common Mistakes - 3 Pitfalls
Using console.log outside of test scripts
Not parsing response body before accessing properties
Hardcoding status code without assertion
Bonus Challenge

Now add data-driven testing with 3 different API endpoints and verify their status codes and output blocks

Show Hint