Contract testing

In microservices architecture

Bartosz Frąckowiak

Index

Why?

How can we ensure that two separate systems (such as two microservices) are compatible and are able to communicate with one other?

testing pyramid

testing pyramid

Feedback loop

INTEGRATION  CONTRACT 
Validate subset of execution logic Validates only service endpoints structure 
Can include contract specification check into scope Based on the contract specification
broaden scope tight scope
causes side effects on service no mocking required
Validates actual response Validates only contract specification

Integration test    vs

Contract test

Can a service provider know about its consumers?

 

Can consumers know about service provider flows?

 

Who creates/owns the tests?

 

Where tests should be run?

 

What about other communication protocols than HTTP?

consumer-driven API design

"Domain-Driven" API design

CONTEXT IS CRUCIAL 

pact.io

pact.io

Pack.io demo

State of Pack .net

Contracts in Nuget

  • The specification in form of plain .NET code

    • reflection-based tests

  • Versioning, hosting server out of the box

  • Technology coupling

    • How to test the NuGet package on FE?

Hosting contract as a NuGet packages

OpenAPI

The OpenAPI Specification (OAS) defines a standard, language-agnostic interface to RESTful APIs which allows both humans and computers to discover and understand the capabilities of the service without access to source code, documentation, or through network traffic inspection. When properly defined, a consumer can understand and interact with the remote service with a minimal amount of implementation logic.

OpenAPI Specification v 3.0.3

The AsyncAPI Specification is a project used to describe and document message-driven APIs in a machine-readable format. It’s protocol-agnostic, so you can use it for APIs that work over any protocol (e.g., AMQP, MQTT, WebSockets, Kafka, STOMP, HTTP, Mercure, etc).

AsyncAPI Specification v 2.2.0

OpenAPI and AsyncAPI specs demo

1

Update service contract with Semantic versioning

2

After each merge to master build agent uploads OpenAPI spec to dedicated storage

3

Dedicated storage keeps tract of each contract specification of each service

5

Each Contract test on consumer side get specification of provider in particular version using simple HTTP protocol and keeps in memory

4

Consumer runs a PR-gate build, within contract tests inside

6

Asserting is done in a fasy and efective way. Each asseret do not casue any state change on eight consumer or provider side.

OpenAPI contract test demo

PHILOSOPHY

 

"We can solve any problem by introducing an extra level of indirection."

 

 

 is a term originated by Andrew Koenig to describe a remark by Butler Lampson[1] attributed to David J. Wheeler:[2]

Thank you

ContractTesting

By Bartosz Frąckowiak

ContractTesting

Contract testing in a microservices architecture

  • 4