Differential Testing

Simple Definition

Differential testing checks one implementation against another. You give both the same inputs, compare what comes out, and investigate every difference. One side is usually a trusted reference, such as the old version of a function, a well-known library, or a slower but simpler implementation.

Plain-English Explanation

Sometimes it is hard to say what the correct output is, but easy to say “it should match what we had before.” A refactor is the classic case: the goal is cleaner code with exactly the same behavior. Rather than writing new tests for every behavior, you run the old and new versions side by side on many inputs and let any mismatch tell you where behavior changed.

The same idea is used to test compilers (do two compilers produce programs that behave the same?), parsers, database engines, and ports from one language to another.

Example

An AI coding agent refactors a TypeScript utility that formats prices for invoices. The new code is shorter and passes the existing tests. Before merging, you keep a copy of the old function and run both on 10,000 inputs: the existing test cases, real values from last month’s invoices, and generated edge cases like negative amounts, zero, very large numbers, and many decimal places. Thirty inputs differ. Twenty-eight are the old function’s rounding bug, which you decide to keep fixed. Two are a new bug: the refactor drops the minus sign on negative refunds. Neither showed up in the original test suite.

The Parts That Need Care

  • The reference. Decide which side you trust. If the old code was wrong, matching it faithfully reproduces the bug.
  • Output comparison. Exact comparison works for simple values. Floating-point numbers, timestamps, generated IDs, and the order of items in a set often differ harmlessly, so you normalize outputs first: round numbers, sort lists, strip timestamps.
  • Expected differences. Some changes are intentional, like a fixed bug. Record them explicitly so they do not hide new, unintended differences.
  • Inputs. Real recorded inputs show what users actually send; generated inputs, often from property-based testing tools, find edge cases. Use both.
  • Regression detection. Kept as a standing check, a differential test catches behavior changes in later edits too.

Why It Matters

AI agents make large refactors cheap, and an agent’s own tests tend to confirm the code it just wrote. Comparing against the previous behavior is an independent check that does not depend on the agent understanding the code correctly. Running a new system alongside the old one in production, without acting on its output, is the operational version of the same idea; see shadow mode.

Limits

Matching outputs do not prove correctness. If both implementations share a mistake, or the inputs never reach the broken path, the test passes. Differential testing also says nothing about speed, memory use, or security unless you measure those separately. Treat it as one strong signal alongside reading the diff and other tests.

Continue learning

Explore related guides, tools, workflows, and prompts that help you go deeper into this topic.

See AI terms in action

Browse practical AI workflows that use the concepts in this glossary.

Last updated: