Software Development

Contract Testing vs Integration Testing: Pact, Testcontainers, and Where Each One Pays Off

Picture a familiar Friday. Every test in both services is green, so the team deploys. Minutes later, the checkout page throws errors because the provider renamed one JSON field. Each team tested its own code well, yet nobody tested the agreement between them.

Two tools often come up here: Pact and Testcontainers. They sound like rivals, but they answer different questions. In this article, we compare them, measure what each style costs, and decide where each one pays off.

Two Different Questions

An integration test asks, “does my service work with the real thing?” That real thing might be a database, a message broker, or another service. A contract test asks something narrower: “do these two services agree on the messages they exchange?”

Consequently, the two approaches catch different bugs. One finds mistakes in how your code talks to real infrastructure. The other finds mismatches between teams before anything is deployed.

Contract Testing With Pact

How it works

With Pact, the consumer team writes a test against a mock provider. When the test passes, Pact saves the expected requests and responses into a pact file. Then the provider team replays that file against its real code. If both sides pass, they agree.

The Pact FAQ describes the goal well. It replaces one slow integration test with two sets of fast “unit” tests, and the pact file keeps them in sync. The same FAQ notes that Pact is “contract by example”, not a full schema.

In practice, the pact files go to a Pact Broker. Before each release, a tool called can-i-deploy checks whether the version you want to ship was verified by its partners, as shown in the PactFlow consumer guide.

What it deliberately does not do

Importantly, a contract test does not check side effects. According to the Pact documentation, a contract test confirms that both sides understand the request and response for creating an order. However, checking that the order was actually saved is a functional test, and that belongs in the provider’s own test suite.

Integration Testing With Testcontainers

How it works

Testcontainers starts real dependencies from your test code. As the Testcontainers for Java site explains, it provides lightweight, throwaway instances of databases, browsers, or anything else that runs in a Docker container. When the test ends, the container is destroyed.

As a result, you stop testing against a fake. A mocked repository will never reveal a bad SQL dialect, a failing migration, or a serializer that behaves differently on a real broker. A real PostgreSQL container will.

What it costs

Nevertheless, there is a price. Testcontainers needs Docker on every laptop and CI runner, and each container takes time to start. Moreover, it still tests only your own service against its own dependencies. It cannot tell you whether another team’s deployment will break you.

What the Numbers Say

To make the cost difference concrete, I built a small local simulation in Python. One check validates a response shape in memory, which is how a contract check behaves. The other calls a real HTTP server backed by a real SQLite file. I ran each style several times and took the median of three runs.

Check styleMedian95th percentileTime for 1,000 checks
Contract-style (in memory)0.001 ms0.001 msabout 1 ms
HTTP + database, already running0.71 ms0.95 ms0.71 s
HTTP + database, fresh per test5.0 ms10.6 ms5.0 s
Figure 1. Latency per check, log scale. Data note: my own local run (Python 3.12, loopback, SQLite), median of three runs; it shows relative cost only.
Figure 2. Time to run 1,000 checks, log scale, derived from the medians above. Data note: a real Docker container start adds much more than this toy setup. I could not measure it, because my sandbox has no Docker.

Clearly, the gap is large even in this tiny example. Starting a fresh dependency per test is about seven times slower than reusing a running one. With real containers, that gap would be wider still, so many teams share one container across a test class.

Where Each One Pays Off

SituationBetter fitWhy
Many services, separate teams, independent releasesPactCatches breaking changes before deploy, without running every service together
Complex SQL, migrations, or queriesTestcontainersOnly a real database exposes dialect and schema problems
Message formats between producers and consumersPactBoth sides prove they read and write the same shape
One team, one deployable, a few dependenciesTestcontainersFew boundaries, so contracts add little
Provider business rulesProvider’s own testsContracts do not check side effects

In short, Pact pays off at team boundaries, while Testcontainers pays off at infrastructure boundaries. A small team with one deployable should usually start with Testcontainers. Once a second team consumes your API, contracts earn their keep.

Quick checklist

  • Do other teams consume your API? Add contract tests.
  • Does your code run real SQL or talk to a broker? Add Testcontainers.
  • Keep contract tests small and isolated, not functional.
  • Share containers per test class to save time.
  • Run can-i-deploy before every release.

What We Have Learned

Contract tests and integration tests answer different questions, so they complement each other instead of competing. Pact proves that a consumer and a provider agree on messages, and it does so with fast unit-style tests plus a broker check before release. Testcontainers proves that your service works against real infrastructure, at the cost of Docker and slower startup. In our small measurement, an in-memory check was thousands of times cheaper than a real HTTP and database call. Therefore, use contracts at the boundaries between teams, use containers at the boundaries with infrastructure, and keep business-rule testing in each service’s own suite.

Eleftheria Drosopoulou

Eleftheria is an Experienced Business Analyst with a robust background in the computer software industry. Proficient in Computer Software Training, Digital Marketing, HTML Scripting, and Microsoft Office, they bring a wealth of technical skills to the table. Additionally, she has a love for writing articles on various tech subjects, showcasing a talent for translating complex concepts into accessible content.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Back to top button