Skip to main content

How to write automated tests for APIs using Postman

References:

http://blog.getpostman.com/2014/03/07/writing-automated-tests-for-apis-using-postman/


Ref. site: GET http://httpbin.org/get

Example code:

tests["response code is 200"] = responseCode.code === 200;

/*
pm.test("response status is 200", function () {
    pm.response.to.have.status(200);
});
*/

pm.test("Request was successful", function () {
    pm.response.to.be.success;
});

tests["body contains origin"] = responseBody.has("origin", "202.84.47.68");

// Response Assertion API in test scripts
// pm.response.to.have

pm.test("Response Assertions WORK fine", function () {
   
    pm.response.to.have.status("OK"); //pm.response.to.have.status(reason:String)
   
    pm.response.to.have.header("Content-Type", "application/json"); //pm.response.to.have.header(key:String, optionalValue:String)
   
    pm.response.to.have.body();
   
    pm.response.to.have.jsonBody();
});

// example using response assertions
pm.test("response should be okay to process", function () {
    pm.response.to.not.be.error;
    // pm.resonse.to.have.jsonBody("");
    pm.response.to.not.have.jsonBody("error");
});

// let jsonData = JSON.parse(responseBody);
let jsonData = pm.response.json();

tests["host is httpbin.org"] = jsonData.headers.Host === "httpbin.org";

tests["origin is accurate"] = jsonData.origin === "202.84.47.68";


// let jsonData = JSON.parse(responseBody);

// tests["totalHits is 500"] = jsonData.totalHits === 500;

Comments