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 style | Median | 95th percentile | Time for 1,000 checks |
|---|---|---|---|
| Contract-style (in memory) | 0.001 ms | 0.001 ms | about 1 ms |
| HTTP + database, already running | 0.71 ms | 0.95 ms | 0.71 s |
| HTTP + database, fresh per test | 5.0 ms | 10.6 ms | 5.0 s |


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
| Situation | Better fit | Why |
|---|---|---|
| Many services, separate teams, independent releases | Pact | Catches breaking changes before deploy, without running every service together |
| Complex SQL, migrations, or queries | Testcontainers | Only a real database exposes dialect and schema problems |
| Message formats between producers and consumers | Pact | Both sides prove they read and write the same shape |
| One team, one deployable, a few dependencies | Testcontainers | Few boundaries, so contracts add little |
| Provider business rules | Provider’s own tests | Contracts 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.


