API Contract Testing with example using Postman

Contract testing for API is about making sure that the API behaves according to the contract that has been agreed upon, and it can be done using different tools and frameworks.

Here’s an example of contract testing using Postman:

# Import the collection from a file
pm.test("status code is 200", function () {
    pm.response.to.have.status(200);
});
pm.test("response should be a JSON", function () {
    pm.response.to.have.header("Content-Type", "application/json");
});
pm.test("response should be not be empty", function () {
    pm.expect(pm.response.json().data).to.not.be.empty;
});

This code is a test script written in JavaScript, it check if response status is 200, if the response type is JSON and if the JSON response data is not empty. The test will fail if the API does not adhere to the contract, and the developers can use the failure message to identify and fix the issue.

Click here to learn about API Contract Testing with example using Pact framework in Python