Better Practices

For individual engineers to the largest teams, Better Practices is intended to distill knowledge from the Postman community. This is a place to learn about modern software practices together! Read more: https://medium.com/better-practices/introducing-better-practices-e9cf14cf0c88

Consumer-driven Contract Testing using Postman

Kaustav Das Modak
Better Practices
Published in
10 min readOct 24, 2018

--

Contract tests ensure sanity and uniformity of interactions between services.
An example scenario documenting steps that are usually taken in building consumer-driven contracts for microservices

Make API contracts explicit and executable

Test contracts against multiple consumers

Independent service testing

Using Postman for consumer-driven contracts

A sample use case: Simple log retrieval service

{
"count": Number,
"entries": Array[Object]
}
{
"serviceName": String,
"timestamp": Number,
"description": String
}

Blueprint

Sample blueprint collection
{
"count": 5,
"entries": [
{
"serviceName": "foo",
"timestamp": 1540206226229,
"description": "Received foo request from user 100"
},
{
"serviceName": "bar",
"timestamp": 1540206226121,
"description": "Sent email to user 99"
},
{
"serviceName": "foo",
"timestamp": 154020622502,
"description": "Received foo request from user 10"
},
{
"serviceName": "baz",
"timestamp": 1540206223230,
"description": "Activated user 101"
},
{
"serviceName": "bar",
"timestamp": 1540206222126,
"description": "Error sending email to user 10"
}
]
}
Adding example to sample blueprint collection’s request
Published documentation generated by Postman from the sample blueprint collection
Response returned from mock server created from sample collection

Writing contract collections

Consumer contract collection using tests to assert on response data
// Define the schema expected in response
var responseSchema = {
"type": "object",
"properties": {
"count": {
"type": "number"
},
"entries": {
"type": "array",
"items": {
"type": "object",
"properties": {
"serverName": {
"type": "string"
},
"timestamp": {
"type": "number"
},
"description": {
"type": "string"
}
}
}
}
}
}
// Get response data as JSON
var jsonData = pm.response.json();
// Test for response data structure
pm.test('Ensure expected response structure', function () {
var validation = tv4.validate(jsonData, responseSchema);
pm.expect(validation).to.be.true;
});

Continuous testing

Organizing contract tests

Setup and Teardown folders in an actual contract collection used by the Postman engineering team.

References

--

--

Better Practices
Better Practices

Published in Better Practices

For individual engineers to the largest teams, Better Practices is intended to distill knowledge from the Postman community. This is a place to learn about modern software practices together! Read more: https://medium.com/better-practices/introducing-better-practices-e9cf14cf0c88

Kaustav Das Modak
Kaustav Das Modak

Responses (5)