Software contracts
- agility in the world of micro-services
Seb Rose
Mastodon: @sebrose@mastodon.scot
Twitter: @sebrose
Blog: https://cucumber.io/blog/
E-mail: seb.rose@smartbear.com
Please help us by completing this 30 second
microservices and contract testing questionnaire.
@sebrose h
tt
p:/
/smartbear.com
The he
ff
alump trap
@sebrose h
tt
p:/
/smartbear.com
Micro-service architecture …
… a collec
ti
on of services that are:
•Independently deployable
•Loosely coupled
•Organised around business capabili
ti
es
•Owned by a small team
•Highly maintainable and testable
h
tt
ps:/
/microservices.io/
@sebrose h
tt
p:/
/smartbear.com
Bene
fi
ts
The micro-service architecture enables the
rapid, frequent and reliable delivery of
large, complex applica
ti
ons. It also enables
an organiza
ti
on to evolve its technology
stack.
h
tt
ps:/
/microservices.io/
@sebrose h
tt
p:/
/smartbear.com
Pi
tf
alls
• The micro-service I depend on isn’t ready
• The micro-service I depend on doesn’t
provide the func
ti
onality I need
• I’m not con
fi
dent that the new version of
the micro-service will behave the same
• We want to update our micro-service, but
don’t know who is using it. Or how.
@sebrose h
tt
p:/
/smartbear.com
You have a distributed monolith
Beth Skurrie
If you can’t deploy services independently,
you don’t have micro-services.
Spot the he
ff
alump
@sebrose h
tt
p:/
/smartbear.com
TL;DR
• All interac
ti
ons between so
ft
ware
components are governed by contracts
• Contract tes
ti
ng ensures that both
components have the same expecta
ti
ons
• Contract tests should be wri
tt
en by the
developers
@sebrose h
tt
p:/
/smartbear.com
Agenda
So
ft
ware contracts
Where do contracts come from?
Decoupling with test doubles
Contract tests
Managing dependencies
Pact & Pact Broker
Q & A
@sebrose h
tt
p:/
/smartbear.com
Dependencies
X
Contracts
@sebrose h
tt
p:/
/smartbear.com
Consumer & provider
C P
Contract
@sebrose h
tt
p:/
/smartbear.com
Design by contract
Design by contract
Contract
• an agreement between client
and supplier
Characteris
ti
cs
• expect some bene
fi
ts
• incur some obliga
ti
ons
@sebrose h
tt
p:/
/smartbear.com
Explicit contract
h
tt
ps:/
/ijcnlp2008.org/images/bolt-clipart-clip-art-12.png
@sebrose h
tt
p:/
/smartbear.com
Implicit contract
h
tt
ps:/
/www.slidebackgrounds.com/bolt/jkM2jm8-bolt-nut-screw-backgrounds/840
@sebrose h
tt
p:/
/smartbear.com
Implicit contract
h
tt
ps:/
/www.slidebackgrounds.com/bolt/jkM2jm8-bolt-nut-screw-backgrounds/840
@sebrose h
tt
p:/
/smartbear.com
Agenda
So
ft
ware contracts
Where do contracts come from?
Decoupling with test doubles
Contract tests
Managing dependencies
Pact & Pact Broker
Q & A
@sebrose h
tt
p:/
/smartbear.com
▪Closed and complete Provider contracts express a service's business
function capabilities in terms of the complete set of exportable elements
available to consumers, and as such are closed and complete with respect
to the functionality available to the system.
▪Singular and authoritative Provider contracts are singular and authoritative
in their expression of the business functionality available to the system.
▪Bounded stability and immutability A provider contract is stable and
immutable for a bounded period and/or locale. Provider contracts typically
use some form of versioning to differentiate differently bounded instances
of the contract.
https://martinfowler.com/articles/consumerDrivenContracts.html
Provider contracts
@sebrose h
tt
p:/
/smartbear.com
▪Open and incomplete Consumer contracts are open and incomplete with
respect to the business functionality available to the system. They express
a subset of the system's business function capabilities in terms of the
consumer's expectations of the provider contract.
▪Multiple and non-authoritative Consumer contracts are multiple in
proportion to the number of consumers of a service, and each is non-
authoritative with regard to the total set of contractual obligations placed on
the provider. Consumers may evolve at different rates.
▪Bounded stability and immutability Like provider contracts, consumer
contracts are valid for a particular period of time and/or location.
https://martinfowler.com/articles/consumerDrivenContracts.html
Consumer driven contracts (CDC)
@sebrose h
tt
p:/
/smartbear.com
How do you agree a contract?
@sebrose h
tt
p:/
/smartbear.com
Provider
team
Consumer
team
How do you agree a contract?
@sebrose h
tt
p:/
/smartbear.com
Provider
team
Consumer
team
How do you agree a contract?
@sebrose h
tt
p:/
/smartbear.com
Provider
team
Consumer
team
How do you agree a contract?
@sebrose h
tt
p:/
/smartbear.com
Provider
team
Consumer
teams
How do you agree a contract?
@sebrose h
tt
p:/
/smartbear.com
You’re given it and have to live with it
OR
You nego
ti
ate and document a contract
h
tt
ps:/
/microservices.io/
How do you agree a contract?
@sebrose h
tt
p:/
/smartbear.com
• Each consumer captures their expecta
ti
ons
of the provider in a separate contract.
• All of these contracts are shared with the
provider so they gain insight into the
obliga
ti
ons they must ful
fi
l for each
individual client.
• The provider can create a test suite to
validate these obliga
ti
ons.
h
tt
ps:/
/thoughtworks.github.io/pacto/pa
tt
erns/cdc/
Consumer driven contracts
@sebrose h
tt
p:/
/smartbear.com
Agenda
So
ft
ware contracts
Where do contracts come from?
Decoupling with test doubles
Contract tests
Managing dependencies
Pact & Pact Broker
Q & A
@sebrose h
tt
p:/
/smartbear.com
C
C
Collaboration tests
Monolithic approach
Contract
Collaboration
Test
Production P
P
@sebrose h
tt
p:/
/smartbear.com
C
C
Collaboration tests
Test
double
Decoupled approach
Contract
Collaboration
Test
Production P
@sebrose h
tt
p:/
/smartbear.com
C
C
Collaboration tests
Test
double
Success!
Collaboration
Test
Production P
@sebrose h
tt
p:/
/smartbear.com
C
C
Collaboration tests
Test
double
Fail
Collaboration
Test
Production
Incorrect behaviour
P
@sebrose h
tt
p:/
/smartbear.com
Agenda
So
ft
ware contracts
Where do contracts come from?
Decoupling with test doubles
Contract tests
Managing dependencies
Pact & Pact Broker
Q & A
@sebrose h
tt
p:/
/smartbear.com
JB Rainsberger, via GOOS mailing list, “Unit-test mock/stub assumptions rots”
15 March 2012
Systema
ti
c contract tes
ti
ng
• Collabora
ti
on tests make
assump
ti
ons about the contract
• Contract tests try to jus
ti
fy those
assump
ti
ons
JB Rainsberger, via GOOS mailing list, “Unit-test mock/stub assumptions rots”
15 March 2012
@sebrose h
tt
p:/
/smartbear.com
Test
double
Contract test
Contract Test
P
@sebrose h
tt
p:/
/smartbear.com
Test
double
I
n
t
e
r
f
a
c
e
Hand-rolled
contract tests
Contract Test
P
@sebrose h
tt
p:/
/smartbear.com
Agenda
So
ft
ware contracts
Where do contracts come from?
Decoupling with test doubles
Contract tests
Managing dependencies
Pact & Pact Broker
Q & A
@sebrose h
tt
p:/
/smartbear.com
CI Staging Prod
C3 C2 C1
P3 P2 P1
CI - consumer & provider
Test Test Test
Test
Test Test Test
@sebrose h
tt
p:/
/smartbear.com
Agenda
So
ft
ware contracts
Where do contracts come from?
Decoupling with test doubles
Contract tests
Managing dependencies
Pact & Pact Broker
Q & A
@sebrose h
tt
p:/
/smartbear.com
Pact provides a mechanism for crea
ti
ng a
contract between a service consumer and a
service provider, and then providing the tools
to validate that the consumer and provider
adhere to the contact independently of each
other.
https://dius.com.au/2014/05/19/simplifying-micro-service-testing-with-pacts/
Simplifying consumer driven contracts
@sebrose h
tt
p:/
/smartbear.com
•Consumer creates contracts using Pact DSL
•When consumer tests are run:
•Pact creates a mock HTTP server
•a Pact
fi
le is created
•Provider uses Pact
fi
le to verify compa
ti
bility
•Provider may o
ff
er “backdoor” interface
Pact - key points
@sebrose h
tt
p:/
/smartbear.com
https://www.youtube.com/watch?v=79GKBYSqMIo
@sebrose h
tt
p:/
/smartbear.com
https://www.youtube.com/watch?v=79GKBYSqMIo
@sebrose h
tt
p:/
/smartbear.com
https://www.youtube.com/watch?v=79GKBYSqMIo
“Matrix”
@sebrose h
tt
p:/
/smartbear.com
CI Staging Prod
V32 V29 V27
Complica
ti
ons
W3 W2
X9 X8 X7
Y15 Y14 Y9
Z84 Z73 Z72
@sebrose h
tt
p:/
/smartbear.com
• Pacts are published by Consumer
• Pacts are fetched by Provider
• Results are stored in the “Matrix”
• “Matrix” supports independent deployment
Pact broker - key points
@sebrose h
tt
p:/
/smartbear.com
Pact
fl
ow is the complete contract testing solution
allowing teams to orchestrate and scale their contract
testing initiative.
Visibility to focus on what ma
tt
ers
With Pact
fl
ow, developers can
fi
nd and
fi
x integration
errors earlier in the SDLC and teams can improve
communication & collaboration, reduce reliance on
E2E tests resulting in faster and safer deployments.
@sebrose h
tt
p:/
/smartbear.com
•Increases agility and con
fi
dence
•Reduces need for integra
ti
on tests
•Speeds up development
•No subs
ti
tute for communica
ti
on
•Is a development ac
ti
vity
•Does not replace other forms of tes
ti
ng
Take aways
Contract tes
ti
ng:
https://bddbooks.com
Please help us by completing this
30 second microservices and
contract testing questionnaire.
Seb Rose
Mastodon: @sebrose@mastodon.scot
Twitter: @sebrose
Blog: https://cucumber.io/blog/
E-mail: seb.rose@smartbear.com
Download these
slides

Software contracts - Global Enterprise Agile 2023.pdf