Contract Testing
11.08.2021
Implementing a scalable testing strategy for Microservices
Said Mengi
Baran Gayretli
Consumer-Driven Contract Tests
ping@kloia.com
The testing pyramid is a concept that
groups software tests into three
different categories. This helps
developers and QA professionals
ensure higher quality, reduce the
time it takes to find the root cause of
bugs, and build a more reliable test
suite.
Consumer-Driven Contract Tests
ping@kloia.com
Unit tests in both sides where
consumers detail clearly what are
the interactions and behaviours
expected from the provider API.
Consumer-Driven Contract Tests
ping@kloia.com
Great for developing and testing intra-organisation microservices.
● You (or your team/organisation/partner organisation) control the development of both the
consumer and the provider.
● The requirements of the consumer(s) are going to be used to drive the features of the
provider.
● There is a small enough number of consumers for a given provider that the provider team
can manage an individual relationship with each consumer team.
Integration Tests vs Contract Tests
ping@kloia.com
Speed
Maintenance
Reliability
Realizing bugs towards the end of
deployment
Scalability
Pact
ping@kloia.com
● Some of the tools that support Consumer-Driven Contract Testing are Pact and Spring Cloud
Contract.
● We chose Pact because:
○ Generation of contracts from the code
○ Provider verification (pact-jvm-provider-junit)
○ Supports multiple languages JVM, Node, JS web apps, …
○ Pact Specification
○ Pact Broker
Pact Broker
ping@kloia.com
● Helps managing, versioning and distributing
contracts.
● Great tool to visualize contracts and system
relationships (dependency graph).
● Contracts as a form of “Live Documentation”
for APIs and interactions between
components.
CDC on the Consumer Side
ping@kloia.com
1) Define expected interactions
CDC on the Consumer Side
ping@kloia.com
2) Test the API client code
3) Pact Mock Service generates contract files for successful tests
CDC on the Consumer Side
ping@kloia.com
Contract Testing on the Provider Side
ping@kloia.com
code screenshot successfully
tests
Contract Testing on the Provider Side
ping@kloia.com
Auto-generated Pact provider contract tests
Pact uses the state description on the contract to
determine which set-up logic needs to be
executed before running a generated test.
Contract Testing on the Provider Side
ping@kloia.com
Test the provider
Contract Testing on the Provider Side
ping@kloia.com
PoC
ping@kloia.com
● Implementing Consumer-Driven Contract tests in Angular, Paulo Clavijo
https://paucls.wordpress.com/2017/08/04/pact-consumer-driven-contract-imple
mentation-in-angular
● Configure Pact JS Consumer Contract tests in Angular, Paulo Clavijo
https://paucls.wordpress.com/2017/08/04/configure-pact-js-consumer-contract-
tests-in-angular
● Sample project on Provider Side https://github.com/paucls/task_list_api-kotlin-pact
Further Reading
ping@kloia.com
References
● ContractTest, Martin Fowler. https://martinfowler.com/bliki/ContractTest.html
● Pact documentation https://docs.pact.io/documentation/implementation_guides.html
● Testing Strategies in a Microservice Architecture, Toby Clemson
https://martinfowler.com/articles/microservice-testing/
● Integrated Tests Are A Scam, J.B. Rainsberger http://blog.thecodewhisperer.com/permalink/integrated-tests-are-a-scam
● Consumer-Driven Contracts: A Service Evolution Pattern, Ian Robinson
https://www.martinfowler.com/articles/consumerDrivenContracts.html
● Microservices, Flexible Software Architecture. Eberhard Wolff.
● Contract testing with GraphQL, Andreas Marek
https://www.andimarek.com/post/contract-testing-graphql
● https://github.com/DiUS/pact-jvm/tree/master/pact-jvm-provider-junit
Thank you for listening
Questions?

Contract Testing

  • 1.
    Contract Testing 11.08.2021 Implementing ascalable testing strategy for Microservices Said Mengi Baran Gayretli
  • 2.
    Consumer-Driven Contract Tests ping@kloia.com Thetesting pyramid is a concept that groups software tests into three different categories. This helps developers and QA professionals ensure higher quality, reduce the time it takes to find the root cause of bugs, and build a more reliable test suite.
  • 3.
    Consumer-Driven Contract Tests ping@kloia.com Unittests in both sides where consumers detail clearly what are the interactions and behaviours expected from the provider API.
  • 4.
    Consumer-Driven Contract Tests ping@kloia.com Greatfor developing and testing intra-organisation microservices. ● You (or your team/organisation/partner organisation) control the development of both the consumer and the provider. ● The requirements of the consumer(s) are going to be used to drive the features of the provider. ● There is a small enough number of consumers for a given provider that the provider team can manage an individual relationship with each consumer team.
  • 5.
    Integration Tests vsContract Tests ping@kloia.com Speed Maintenance Reliability Realizing bugs towards the end of deployment Scalability
  • 6.
    Pact ping@kloia.com ● Some ofthe tools that support Consumer-Driven Contract Testing are Pact and Spring Cloud Contract. ● We chose Pact because: ○ Generation of contracts from the code ○ Provider verification (pact-jvm-provider-junit) ○ Supports multiple languages JVM, Node, JS web apps, … ○ Pact Specification ○ Pact Broker
  • 7.
    Pact Broker ping@kloia.com ● Helpsmanaging, versioning and distributing contracts. ● Great tool to visualize contracts and system relationships (dependency graph). ● Contracts as a form of “Live Documentation” for APIs and interactions between components.
  • 8.
    CDC on theConsumer Side ping@kloia.com
  • 9.
    1) Define expectedinteractions CDC on the Consumer Side ping@kloia.com 2) Test the API client code
  • 10.
    3) Pact MockService generates contract files for successful tests CDC on the Consumer Side ping@kloia.com
  • 11.
    Contract Testing onthe Provider Side ping@kloia.com
  • 12.
    code screenshot successfully tests ContractTesting on the Provider Side ping@kloia.com Auto-generated Pact provider contract tests
  • 13.
    Pact uses thestate description on the contract to determine which set-up logic needs to be executed before running a generated test. Contract Testing on the Provider Side ping@kloia.com
  • 14.
    Test the provider ContractTesting on the Provider Side ping@kloia.com
  • 15.
  • 16.
    ● Implementing Consumer-DrivenContract tests in Angular, Paulo Clavijo https://paucls.wordpress.com/2017/08/04/pact-consumer-driven-contract-imple mentation-in-angular ● Configure Pact JS Consumer Contract tests in Angular, Paulo Clavijo https://paucls.wordpress.com/2017/08/04/configure-pact-js-consumer-contract- tests-in-angular ● Sample project on Provider Side https://github.com/paucls/task_list_api-kotlin-pact Further Reading ping@kloia.com
  • 17.
    References ● ContractTest, MartinFowler. https://martinfowler.com/bliki/ContractTest.html ● Pact documentation https://docs.pact.io/documentation/implementation_guides.html ● Testing Strategies in a Microservice Architecture, Toby Clemson https://martinfowler.com/articles/microservice-testing/ ● Integrated Tests Are A Scam, J.B. Rainsberger http://blog.thecodewhisperer.com/permalink/integrated-tests-are-a-scam ● Consumer-Driven Contracts: A Service Evolution Pattern, Ian Robinson https://www.martinfowler.com/articles/consumerDrivenContracts.html ● Microservices, Flexible Software Architecture. Eberhard Wolff. ● Contract testing with GraphQL, Andreas Marek https://www.andimarek.com/post/contract-testing-graphql ● https://github.com/DiUS/pact-jvm/tree/master/pact-jvm-provider-junit
  • 18.
    Thank you forlistening Questions?