Pact: a consumer-driven contract testing framework

Overview

CDC Pact

  • Pact is a consumer-driven contract testing framework. Born out of a microservices boom, Pact was created to solve the problem of integration testing large, distributed systems. (Old definition)
  • How Pact works slide from pactflow.io
  • Rescue for integration test cost.
  • Gives fast feedback
  • Implement any programming language you want. For example
  • No dedicated test environments (it works on dev machine)

Project Architecture 🚀

ℹ️ Terminology

Interaction: A request and response pair. A pact consists of a collection of interactions.

Pact file: A file containing the JSON serialised interactions (requests and responses) that were defined in the consumer tests. This is the Contract.

Pact verification: To verify a Pact contract, the requests contained in a Pact file are replayed against the provider code, and the responses returned are checked to ensure they match those expected in the Pact file.

Pact Specification A provider state name is specified when writing the consumer specs, then, when the pact verification is set up in the provider the same name will be used to identify the set up code block that should be run before the request is executed.

ℹ️ Pact Broker

The Pact Broker is an open source tool that requires you to deploy, administer and host yourself. It enables you to share your pacts and verification results between projects

Pact Generation and Verification Flow 🚀

Matching on types

dsl.Like(content) tells Pact that value is not important, just focus type match.

Matching on arrays

dsl.EachLike(content, min) tells Pact that the value should be array type, consisting of elements like those passed in. min must be >= 1.

"fabrics": dsl.EachLike(dsl.MapMatcher{
    "title":    dsl.Like("Pamuk"),
    "count":    dsl.Integer()
}, 1),

Matching by regular expression

dsl.Term(example, matcher) tells Pact that the value should match using a given regular expression, using example in mock responses.

"type": Term("admin", "admin|user|guest") => "type": "admin"

Match common formats

method description
Identifier() Match an ID (e.g. 42)
Integer() Match all numbers that are integers (both ints and longs)
Decimal() Match all real numbers (floating point and decimal)
HexValue() Match all hexadecimal encoded strings
Date() Match string containing basic ISO8601 dates (e.g. 2016-01-01)
Timestamp() Match a string containing an RFC3339 formatted timestapm (e.g. Mon, 31 Oct 2016 15:21:41 -0400)
Time() Match string containing times in ISO date format (e.g. T22:44:30.652Z)
IPv4Address() Match string containing IP4 formatted address
IPv6Address() Match string containing IP6 formatted address
UUID() Match strings containing UUIDs

🤘 References 💪

Pact Docs
Pact Go
Turkish Microservice Architecture Book
Pact Broker Docker
Building Microservices
Contract testing and how Pact works
Test Double
Difference between gobuild and build directive

You might also like...
Sample full stack micro services application built using the go-Micro framework.
Sample full stack micro services application built using the go-Micro framework.

goTemp goTemp is a full stack Golang microservices sample application built using go-micro. The application is built as a series of services that prov

NewSQL distributed storage database based on micro service framework
NewSQL distributed storage database based on micro service framework

QLite 是基于微服务的 NewSQL 型数据库系统,与传统的一体化数据库不同,该系统将本该内置的多种数据结构(STL)拆分成多个服务模块,每个模块都是独立的一个节点,每个节点都与其主网关进行连接,从而形成分布式存储结构。

Kratos is a microservice-oriented governance framework implements by golang
Kratos is a microservice-oriented governance framework implements by golang

Kratos is a microservice-oriented governance framework implements by golang, which offers convenient capabilities to help you quickly build a bulletproof application from scratch.

RPC explained by writing simple RPC framework in 300 lines of pure Golang.

Simple GoRPC Learning RPC basic building blocks by building a simple RPC framework in Golang from scratch. RPC In Simple Term Service A wants to call

Modern microservice web framework of golang
Modern microservice web framework of golang

gogo gogo is an open source, high performance RESTful api framework for the Golang programming language. It also support RPC api, which is similar to

a microservice framework for rapid development of micro services in Go with rich eco-system
a microservice framework for rapid development of micro services in Go with rich eco-system

中文版README Go-Chassis is a microservice framework for rapid development of microservices in Go. it focus on helping developer to deliver cloud native a

The Go backend framework with superpowers

Encore - The Go backend framework with superpowers https://encore.dev Overview Encore is a Go backend framework for rapidly creating APIs and distribu

🦄🌈 YoyoGo is a simple, light and fast , dependency injection based micro-service framework written in Go.
🦄🌈 YoyoGo is a simple, light and fast , dependency injection based micro-service framework written in Go.

🦄🌈 YoyoGo is a simple, light and fast , dependency injection based micro-service framework written in Go. Support Nacos ,Consoul ,Etcd ,Eureka ,kubernetes.

Kratos is a microservice-oriented governance framework implements by golang,
Kratos is a microservice-oriented governance framework implements by golang,

Kratos is a microservice-oriented governance framework implements by golang, which offers convenient capabilities to help you quickly build a bulletproof application from scratch.

Owner
A.Samet İleri
Software Developer 🚀. When all you have is a hammer, everything looks like a nail.
A.Samet İleri
ftgogo - event-driven architecture demonstration application

ftgogo (food-to-gogo) is a Golang implementation of the FTGO application described in the book "Microservice Patterns" by Chris Richardson. A library edat was developed to provide for Golang many of the solutions that Eventuate, the framework used by FTGO, provides for Java.

Michael Stack 101 Jan 3, 2023
Go microservice tutorial project using Domain Driven Design and Hexagonal Architecture!

"ToDo API" Microservice Example Introduction Welcome! ?? This is an educational repository that includes a microservice written in Go. It is used as t

Mario Carrion 616 Jan 4, 2023
Fast, intuitive, and powerful configuration-driven engine for faster and easier REST development

aicra is a lightweight and idiomatic configuration-driven engine for building REST services. It's especially good at helping you write large APIs that remain maintainable as your project grows.

xdrm-brackets 8 Oct 19, 2022
This is an example to demonstrate implementation golang microservices using domain driven design principles and sugestions from go-kit

go-kit DDD Domain Driven Design is prevelent and rising standard for organizing your microservice code. This design architecture emphasis on Code orga

Sourabh Mandal 1 Feb 9, 2022
Rpcx-framework - An RPC microservices framework based on rpcx, simple and easy to use, ultra fast and efficient, powerful, service discovery, service governance, service layering, version control, routing label registration.

RPCX Framework An RPC microservices framework based on rpcx. Features: simple and easy to use, ultra fast and efficient, powerful, service discovery,

ZYallers 1 Jan 5, 2022
Demo project for unit testing presentation @ GoJKT meetup

go-demo-service Demo project for unit testing presentation @ GoJKT meetup This is a demo project to show examples of unit testing for GoJKT meetup Use

M Yusuf Irfan H 3 Jul 10, 2021
Canonicity testing library

What are canonical tests? That's when instead of comparing the expected and actual values in code: assert.Equal(t, expected, actual) You instead asser

Michael Sorokin 3 Dec 29, 2021
Testing ground for CRUD backend using Golang, gRPC, protobufs

blog-example-service Simple example CRUD backend using Golang, gRPC, and protobufs. Using with MongoDB as the database (default) You will need a Mongo

Jordan Weiner 0 Dec 16, 2021
Go Micro is a framework for distributed systems development

Go Micro Go Micro is a framework for distributed systems development. Overview Go Micro provides the core requirements for distributed systems develop

Asim Aslam 19.9k Jan 3, 2023
Micro-service framework in Go

Kite Micro-Service Framework Kite is a framework for developing micro-services in Go. Kite is both the name of the framework and the micro-service tha

Koding, Inc. 3.2k Jan 9, 2023