CONTRACT

TESTING

Who are we?

Sai Krishna

Lead Consultant

ThoughtWorks

Srinivasan Sekar

Lead Consultant

ThoughtWorks

Monolithic vs Microservices

A sad story ....

Payment

Users

{
    "user":[]

}

{
    "users":[]

}

How do we avoid problems like this?

Test Pyramid

Mocked

Dependencies

Payment Service

Mocked User service

Request

Response

Mocked Test are

Targeted

Cheap

Fast

Not Trustworthy

Integration Test

Integration test are 

TrustWorthy

Slow

Brittle

Whose fault ?

Consumer-Driven Contracts

  • The idea of Consumer-Driven Contracts is to formalize the interactions between a consumer and a provider.

 

  • Consumer creates a contract, which is an agreement between the consumer and a provider on the interactions that will take place between them.

 

  • Provider has agreed on the contract, both consumer and provider can take a copy of the contract and use tests to verify that their corresponding implementations don’t violate the contract. The advantage of these new tests is that they are pretty much unit-tests: they can be run locally and independently, and they are fast and reliable

How pact works

A contract between a consumer and provider is called a pact. Each pact is a collection of interactions. Each interaction describes:

 

  • An expected request - describing what the consumer is expected to send to the provider.

 

  • A minimal expected response - describing the parts of the response the consumer wants the provider to return.

Who would typically implement Pact?

  • The consumer team is responsible for implementing the Pact tests in the consumer codebase that will generate the contract.

 

  • The provider team is responsible for setting up the Pact verification task in the provider codebase, and for writing the code that sets up the correct data for each provider state described in the contract.

 

  • Both teams are responsible for collaborating and communicating about the API and its usage!

PACT Client

CONSUMER

API

PRODUCER MOCK

API

Request

Response

CONTRACT TESTING

 

Contract

PRODUCER VERIFIER

PRODUCER

API

Request

Response

Contract

{
   "user":
}
{
   "users":
}

Consumer Side

Provider Side

DEMO

PACT BROKER

Conclusion

By formalising the interaction between consumer and provider, and verifying that both services stick to the contract they’ve agreed upon, we see that we have no need to perform integration tests between them. Instead we’re using new tests that are specific, fast and robust.

Blog Summary

Contract Testing

By Sai Krishna

Contract Testing

  • 1,236