Latest / Software Testing with Fexingo: QA, Automation, and Reliable Software Engineering / Why Your Test Suite Needs Contract Testing
Transcript
- Lucas: So, Luna, I was reading about that outage at a payments startup back in June. The one where a simple JSON field rename took down their whole checkout flow for almost a day. Luna: I remember that. The team said they had 'thorough integration tests' in their postmortem. But the tests were all against a mock that still had the old field name. Lucas: Exactly. That's the classic trap. Your mocks are only as good as the person who wrote them. And when the provider changes its API, your mock doesn't know. That's where contract testing comes in. Luna: Right, contract testing. I've heard the term thrown around, but honestly, I'm not sure I could explain it to someone. What's the core idea? Lucas: The core idea is simple: instead of each service testing against its own mock of the other, you define a contract — a machine-readable agreement of what requests and responses look like. Both sides test against that same contract. So if the provider changes something, the contract test fails before it ever reaches production. Luna: So it's like a shared source of truth for the API shape. But how do you actually create that contract? Is it just a JSON file you write by hand? Lucas: Usually it starts as a JSON file, but the smart approach is consumer-driven contracts. The consumer writes a test that says 'I expect this endpoint to return a payload with these fields.' That test generates a contract file. The provider then runs that contract file against its own code to verify it still satisfies the consumer's expectations. Luna: Consumer-driven. So the consumer has the power to define the contract? That seems backwards. Lucas: It's actually brilliant because the consumer knows exactly what it needs. The provider could have all these extra fields, but the consumer only cares about a subset. The contract captures what the consumer actually relies on. That way, the provider can change things that don't affect the consumer without breaking anything. Luna: Makes sense. And I guess the tooling supports that? I'm thinking of Pact, right? Lucas: Pact is the big one. It's been around for years and supports most languages. The consumer test uses Pact to generate a contract, which is then shared — often via a broker — and the provider runs Pact's verification against its own code. It's a nice workflow. Luna: So let's go back to that payments startup. How would contract testing have caught that rename before it hit production? Lucas: Let's say the checkout service was the consumer, and the payments service was the provider. The checkout service had a Pact test that expected a field called 'amount' in the response. The payments team renamed it to 'total'. When they ran the provider verification, Pact would compare the contract and flag that the provider no longer satisfies the consumer's expectation. That would fail the build, and they'd catch it before deploying. Luna: And because the contract is shared, the consumer's test would also fail if they updated the contract to expect 'total' but then forgot to update their own code. It's like a two-way safety net. Lucas: Exactly. And the beauty is you don't need to spin up the entire system to run these tests. They run in milliseconds in your CI pipeline. You get confidence without the flakiness of full end to end tests. Luna: But isn't there a risk that contracts become outdated or that teams just ignore them? I've seen testing processes that look great on paper but people just skip them. Lucas: That's a real risk. Contract tests only work if they're enforced. The key is to make them part of the definition of done. If a consumer changes its expectations, it has to update the contract and re-publish it. If a provider changes its API, it has to run the verification. If you let teams skip that, you're back to square one. Luna: So it requires a cultural shift. But what about teams that are really small? Is contract testing overkill for, say, a two-person startup? Lucas: Honestly, for a monolith with one team, it's overkill. You can just use integration tests. But as soon as you have two or more teams owning different services, contract testing starts to pay off. It's even more valuable when you have third-party consumers you don't control. Luna: Oh, that's a good point. If you're a public API provider, you can't force your consumers to update their tests every time. Contract testing lets you know when you're about to break someone without having to coordinate with them. Lucas: Right. And there's a nice practice called provider-driven contracts for that case. The provider declares what it promises to support, and consumers can check their own code against that contract. It's less common but useful for public APIs. Luna: So you've got consumer-driven for internal services and provider-driven for public APIs. What about versioning? APIs change, and contracts need to evolve too. Lucas: Versioning is a whole topic. A simple approach is to keep contracts backward compatible. If you rename a field, you can add the new one and keep the old one for a while. Pact has the concept of pending contracts, where you can mark a contract as pending to give the provider time to implement it. That way, you don't break the build while you're in transition. Luna: And I assume you need a broker to manage all these contracts? The Pact broker, I think it's called. Lucas: Yeah, the Pact broker is a key piece. It stores all the contracts, and it can even show you which consumers are using which version of the API. That visibility is gold. You can see at a glance which teams will be affected by a change. Luna: That's really powerful. So how does a team start with contract testing? Say they have an existing microservices setup and they don't want to boil the ocean. Lucas: Start with one service pair. Pick a service that's frequently changing and has a few consumers. Write a consumer test in Pact, generate the contract, and set up the provider verification. Once that's working, you can expand. Luna: And what about convincing skeptical teammates? Some developers I know are pretty wary of adding another testing layer. Lucas: The pitch is simple: contract tests run fast, they're deterministic, and they catch real bugs that integration tests miss. Plus, they actually reduce the need for full end to end tests, which are slow and flaky. So you're not adding more work; you're replacing painful work with something better. Luna: That's a good way to frame it. And for teams that use GraphQL, does contract testing still apply? I feel like GraphQL changes the game a bit. Lucas: GraphQL is interesting. The schema itself acts like a contract, but it's not enough. You still need to know which queries consumers actually use. There are tools that can generate contracts from your GraphQL schema and your consumer queries, so you can still use Pact-style verification. It's a bit more complex, but the principles hold. Luna: So it's not a one-size-fits-all, but the core idea of having a shared, testable agreement is universal. I like that. Lucas: Yeah, and honestly, if I had to pick one testing practice to adopt in 2026, it might be this. With more companies moving to microservices and APIs, the risk of breaking changes just keeps growing. Luna: Before we go, I want to mention something that came up last week. We had a listener write in about how much they appreciated the episode on test data diversity. It made me realize how much we rely on the support of people like them to keep making this show. Lucas: Absolutely. This podcast is ad-free, and that's only possible because of listener support. If today's conversation gave you something useful, you can help us keep doing this by buying us a coffee at buy me a coffee dot com slash fexingo. It's a small gesture, but it means the world to us. Luna: And we honestly appreciate every single one of you who's ever contributed, whether it's a coffee or just sharing the show with a colleague. It keeps us going. Lucas: So, back to contract testing — one thing I want to highlight is how it integrates with your existing CI. You don't need a separate pipeline; you just add a step that runs the contract verification. And you can even make it part of your merge checks, so no PR gets merged if it breaks a contract. Luna: That's a strong enforcement mechanism. And it's something every team can do, regardless of the size of their stack. Lucas: Right. So if you're tired of flaky integration tests and surprise breakages, contract testing might be the missing piece. Start small, get one contract working, and see the difference it makes. Luna: I think we've given people a lot to think about. Next time a field gets renamed, they'll be ready. Lucas: And that's the goal. Thanks for listening, everyone. We'll catch you next time on Fexingo.