Contract tests verify assumptions shared by producers and consumers. They help detect incompatible interface changes before deployment.
Before you start
You should understand inputs, expected outputs and the boundaries of the component under test. State the risk a test should detect before choosing a tool or mock. Distinguish controlled dependencies from real integrations so the result does not imply more coverage than the test actually provides.
The practical goal is to reason through this situation: A consumer expects a required identifier and documented error shape. Read the walkthrough first, then try the interview exercise before opening its answer. The important part is explaining the decision and its consequences, rather than remembering a definition alone.
Step-by-step walkthrough
Step 1: Record consumer assumptions
Required fields and error shapes must stay compatible.
Step 2: Run producer verification
Check actual responses satisfy supported consumer expectations.
Step 3: Cover important semantics
Schema shape alone does not establish meaning or authorization.
Worked scenario
A consumer expects a required identifier and documented error shape.
A response retains a numeric amount field but changes from minor units to major units. A schema-only check passes while the consumer’s meaning breaks. Contract examples should make significant conventions explicit, and broader tests should cover behavior that a message fixture cannot establish.
Common mistake
Schema compatibility alone may not verify ordering, meaning or authorization behavior.
Verify the behavior
Run old and new consumers with known values, not only type-compatible fields.
Interview exercise
Evolve a response safely.
Answer and reasoning
Check old and new consumer expectations and supplement contracts with tests for important semantic guarantees.
Continue learning
Compare the scenario with the Software Testing interview questions and test your understanding with the Software Testing MCQs. For terminology and implementation details, consult the reference material.