Testing your Golang APIs and Microservices with Pact

Testing your Golang APIs and Microservices with Pact

Objectives of  this guide

This guide will:

  1. Explain you the basics of Pact
  2. Highlight the value over traditional end-to-end integrated tests
  3. Provide a hands-on Go workshop which explains all of the key-concepts in 1 hour

What is Pact?

Pact is a tool that makes it fast, easy and reliable to test your APIs and microservices during development. It implements a style of testing referred to as consumer-driven contract testing. It's ideally suited to environments where there are a number of teams building out a large distributed system.  You can read more about that here, but as a brief introduction:

Contract testing is a technique for testing an integration point by checking each application in isolation to ensure the messages it sends or receives conform to a shared understanding that is documented in a "contract".  

For applications that communicate via HTTP, these "messages" would be the HTTP request and response, and for an application that used queues, this would be the message that goes on the queue

This means that you can:

  • Deploy services independently of one another, a key tenet of microservices delivery
  • Prevent breaking changes from being released
  • Avoid expensive and brittle end-to-end tests

It helps you to move both faster and safer. You can read more on the benefits of contract testing here.

How Pact Works

We've created a page dedicated to explaining this concept, so rather than duplicate it - we'll wait for you here :)

Once you have the concepts down pat, the process looks like this:

  1. Create a consumer Pact test
  2. Publish the pact test to a broker (using a service like Pactflow)
  3. Verify the consumer test with your API Provider (Provider Pact test)
  4. Add can-i-deploy to your build pipeline
  5. Ship it 🚀
Example workflow using Pact Go and Pactflow

It really is that easy. Let's get our hands dirty, shall we?


We've hand-crafted a workshop designed to explain all of the key concepts in Pact, in order, to take you from a n00b to a "I can POC this" in about one hour. Here is an overview of the 11 steps in the workshop:

Step Title Concept Covered Learning objectives
step 1 Create our consumer before the Provider API even exists Consumer-driven design
  • Understand use case
    step 2 Write a unit test for our consumer -
    • How to write a basic unit test for an HTTP Client
    • Understand how a unit test is unable to catch certain integration issues
      step 3 Write a Pact test for our consumer Consumer side pact test
      • Understand basic Consumer-side Pact concepts
      • Understand "Matchers" to avoid test data brittleness
      • Demonstrate that Pact tests are able to catch a class of integration problems
        step 4 Verify the consumer pact with the Provider API Provider side pact test
        • Understand basic Provider-side Pact concepts
        • Place provider side testing in a broader testing context (e.g. where it fits on the pyramid)
          step 5 Fix the consumer's bad assumptions about the Provider Humans talking to humans (collaboration)
          • Understand that a tool facilitates collaboration, it doesn't replace it
            step 6 Write a pact test for 404 (missing User) in consumer Testing API invariants
            • Understand how we can test "stateful" APIs without having to create complex, sequential API calls
              step 7 Update API to handle 404 case Provider States
              • How Pact deals with "stateful" interactions, via the concept of "Provider States"
                step 8 Write a pact test for the 401 case Testing authenticated APIs
                • Service evolution - adding a new feature
                  step 9 Update API to handle 401 case Service evolution
                  • Understand the challenge of dealing with complex/dynamic data, such as time-bound authentication tokens
                    step 10 Fix the provider to support the 401 case Request filters
                    • Understand Pact's approach to dealing with complex/dynamic data, such as time-bound authentication tokens
                      step 11 Implement a broker workflow for integration with CI/CD Automation
                      • Understand how to use Pact in a CI and CD workflow

                        Head over to https://github.com/pact-foundation/pact-workshop-go to get started.

                        Next steps

                        Now that you have a good understanding of Pact, why not sit down with your team and scratch out the shape of a POC?

                        About Pactflow

                        Created by the team that brought you Pact, our vision is to transform the way teams test and release distributed systems. As the leading contract-testing collaboration platform we are making it easier than ever to test and deploy microservices, serverless functions and APIs.

                        Like to get in touch, drop as a email - we'd love to say hi!

                        arrow-up icon