Test Overview
This test sends a request to an API endpoint and verifies the response status code and body content. It checks for a default response and a conditional response based on a query parameter.
Jump into concepts and practice - no test required
This test sends a request to an API endpoint and verifies the response status code and body content. It checks for a default response and a conditional response based on a query parameter.
pm.test("Status code is 200", function () { pm.response.to.have.status(200); }); const jsonData = pm.response.json(); const url = pm.request.url.toString(); pm.test("Response contains default message", function () { if (!url.includes("param=hello")) { pm.expect(jsonData.message).to.eql("Default response"); } else { pm.expect(true).to.be.true; // Skip if condition not met } }); pm.test("Conditional response when param=hello", function () { if (url.includes("param=hello")) { pm.expect(jsonData.message).to.eql("Hello response"); } else { pm.expect(true).to.be.true; // Skip if condition not met } });
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Send GET request to API endpoint without query parameter | API server is running and ready to respond | Check response status code is 200 | PASS |
| 2 | Parse JSON response body | Response body received: {"message": "Default response"} | Verify response message equals 'Default response' | PASS |
| 3 | Send GET request to API endpoint with query parameter param=hello | API server is running and ready to respond | Check response status code is 200 | PASS |
| 4 | Parse JSON response body | Response body received: {"message": "Hello response"} | Verify response message equals 'Hello response' when param=hello | PASS |
pm.test('Check response', () => {
if (pm.response.code === 200) {
pm.expect(pm.response.json().success).to.be.true;
} else {
pm.expect(pm.response.code).to.equal(404);
}
});pm.test('Status check', () => {
if (pm.response.code = 201) {
pm.expect(pm.response.code).to.equal(201);
} else if (pm.response.code = 400) {
pm.expect(pm.response.json().error).to.exist;
}
});