Test-driven development

Hand-drawn chain of seven terracotta rings, each broken into three arcs, over dense sand, umber, linen and moss hatched shards.

Test-driven development (TDD) is a way of programming in which the developer writes a small failing test before writing the code that makes it pass. It comes from Kent Beck and the Extreme Programming movement (Fowler, 2023). Advocates name two benefits of writing the test first: code that tests itself, and thinking about the interface before the implementation (Fowler, 2023).

Experiments and industrial studies disagree on whether it improves quality. The quality gain is small or absent against careful test-last work, and productivity results are mixed. Researchers also dispute whether the order of test and code matters at all.

The cycle and its definition

TDD repeats a short loop: write a test, make it pass, then refactor (restructure the code without changing what it does). Fowler dates the practice to Beck in the late 1990s and calls the loop red-green-refactor, after a failing and a passing test run (Fowler, 2023). He adds a first step of listing the test cases to write, and calls skipped refactoring the most common way to get TDD wrong (Fowler, 2023).

Beck’s own 2023 statement has the same shape. It begins with a list of test scenarios, then takes one test at a time, makes it pass, optionally refactors, and repeats (Beck, 2023a). He warns against turning every list item into a concrete test at once. Critiques of writing all tests first, he says, attack something that is not TDD (Beck, 2023a). Researchers describe the intended pace as cycles of about five minutes, at most ten (Fucci et al., 2017).

Behaviour-driven development (BDD), which writes examples of behaviour in a structured notation, began as an answer to confusion about TDD (North, 2006); see Behaviour-driven development. Writing the whole specification of a system before coding is a different practice, covered in Spec-driven development.

Variants

The main variant differs only in order. In incremental test-last (ITL), the developer writes production code and then, at once, its test, before moving to the next small piece of functionality (Tosun et al., 2018). Researchers see idealised TDD as the end of a progression from a traditional approach to an iterative one, with variants such as ITL along the way (Fucci et al., 2017).

A second split concerns how tests treat other objects. Classical TDD uses real objects where possible and a test double (a stand-in object) only when the real one is awkward. Mockist TDD always uses a mock for any object with interesting behaviour and works from the outside of the system inward (Fowler, 2007). Fowler, who calls himself a classicist, says he sees no compelling benefit in the mockist style and worries that it couples tests to implementation. Some mockists reject the distinction altogether (Fowler, 2007).

These accounts are practitioner opinion. A study of mocking found that developers’ use of mocks depends heavily on the role of the class. Developers also report that keeping a mock compatible with the real class is hard and that mocking increases coupling between test and production code (Spadini et al., 2017).

Effects on quality and productivity Contested: studies disagree on this

Studies disagree about whether TDD improves software quality, and the answer depends on the study’s setting and on what TDD is compared with. Of six literature reviews, all but one found improved external quality (defects users would see), and four of six found productivity inconclusive, while one found it degraded (Gross, 2020). A doctoral thesis states that empirical research on TDD has not produced conclusive results and that all possible results have been reported (Rafique, 2011).

Reviews attribute the inconsistency to study setting, rigor, relevance, study type and the comparison process, waterfall or iterative test-last (Gross, 2020). A meta-analysis (a statistical pooling of studies) found quality improved in bigger and longer industrial projects but not in all academic experiments (Mäkinen & Münch, 2014). How effective TDD looked depended on which method the reference group used (Mäkinen & Münch, 2014).

Whether rigorous studies show the gain Contested: studies disagree on this

Sources report opposite links between rigor and the quality gain. Munir and colleagues found clear external-quality improvement in studies of high rigor and relevance, apparently with a loss of productivity. The other categories held many studies showing no difference (Munir et al., 2014). Ghafari and colleagues cite a large systematic study in which the gains were not significant in some studies and more pronounced in low-rigor, low-relevance studies (Ghafari et al., 2020).

One thesis reads Munir and a second review as also finding the gains stronger in low-rigor studies (Gross, 2020). The sources do not reconcile these accounts.

Controlled experiments Strong evidence: many studies agree on this

Controlled experiments mostly find no clear quality or productivity gain over test-last work. A randomised trial failed to support the claims that TDD improves quality and productivity, and an external replication with 21 graduate students found no significant difference either (Fucci et al., 2016). A study of 30 novices over about six months found no effect on quality or productivity, although TDD users wrote significantly more tests with higher fault-detection capability (Baldassarre et al., 2021).

A pooled analysis of 12 experiments found novices reached slightly higher quality with ITL than with TDD. The mean gap was 7.06 percentage points, and its 95% confidence interval (the range likely to contain the true value) crosses zero, so it is not statistically significant (Santos et al., 2020). In an experiment with 24 professionals, quality did not differ between TDD and ITL. Productivity was higher with TDD on a simple task and dropped significantly on a complex task in an existing code base (Tosun et al., 2017).

Industrial case studies

Case studies in industry report large gains. Three Microsoft teams and one IBM team had pre-release defect density 40% to 90% lower than similar projects without TDD, at a subjectively estimated 15-35% longer initial development time (Nagappan et al., 2008a). The authors note that the TDD projects were new, while one comparison was an enhancement of a legacy system. No equal comparison is possible outside a controlled case study, they add (Nagappan et al., 2008a). A five-year IBM study reported 40% lower defect density than a prior non-TDD project, and only three of its eleven surveyed developers wrote tests before the code (Sanchez et al., 2007).

Experiments in companies give a less favourable picture. At one firm TDD beat the usual way of working on external quality, but only slightly, and the authors did not recommend immediate adoption (Santos et al., 2018b). In four experiments at two companies, ITL scored higher than TDD on external quality in three of four comparisons, and the lead grew with experience in unit testing. None of the differences was statistically significant (Santos et al., 2018a). A quasi-experiment with students found TDD no more effective than traditional programming at reducing defects (Wilkerson et al., 2012).

Does test-first order matter Contested: studies disagree on this

Studies disagree on whether writing the test first matters. In a regression analysis of 82 data points from 39 professionals, the order of tests and code had no important influence on quality or productivity. The granularity (size of each step) and uniformity of cycles did (Fucci et al., 2017). The authors conclude that TDD’s benefits may come from fine-grained, steady steps rather than from writing the test first (Fucci et al., 2017). The 12-experiment family says the same: what appears to improve quality is cycle duration, uniformity and refactoring effort, not whether testing comes first (Santos et al., 2020).

A few studies favour test-first on some measures. In the 24-professional experiment, TDD tests had a higher mutation score than ITL tests, with a median of 84% against 62%. A mutation score measures how many planted defects a test suite detects. The TDD tests covered fewer methods, though (Tosun et al., 2018).

Other studies find less. A professional experiment found TDD improved unit testing but slowed the overall process relative to testing after coding (Canfora et al., 2006). Students in a controlled experiment who wrote tests first did not achieve better quality on average (Erdogmus et al., 2005).

One replication found no quality or productivity gain over test-last, but found that quality rose with how closely participants followed the TDD process (Fucci et al., 2014a). In a smaller sample of 22, process conformance did not predict quality or productivity (Fucci et al., 2014b).

Why results disagree

Authors offer several explanations. Experience is one: in the 12-experiment family, professionals dropped more in quality-related performance than students, which the authors attribute to resistance to change and lower motivation (Santos et al., 2020). Duration is another. Case studies run for months or years and experiments for days or weeks, and longer studies report the most favourable results for TDD (Santos et al., 2020). Case-study improvements range from 22% to 267% (Santos et al., 2020).

The task and the control group also matter. Task selection can dominate TDD findings (Tosun et al., 2017). Observed gains may not belong to TDD alone. A control group may not have written tests, or the TDD group may have tried harder knowing it was an experiment (Gross, 2020).

Critiques of the experiments Weak evidence: few or small studies support this

Critics say the experiments are too small and too artificial to settle the question. Participants have little prior TDD experience, from a couple of days to a couple of months, and most studies use one to four synthetic tasks such as coding katas (Ghafari et al., 2020). Only two of the analysed studies examined work in real projects, and very few studies have many participants (Gross, 2020). Brownfield projects, which build on existing code, are under-represented, and generalising from new projects to them may not be valid (Ghafari et al., 2020). The same thesis notes that papers focus almost exclusively on quality, while developers weigh collaboration, confidence and knowledge transfer equally (Gross, 2020).

Design Weak evidence: few or small studies support this

The belief that TDD yields simpler, more cohesive, less coupled code is a claim more than a finding. A case study of three projects found no significantly better result on four of six design metrics, and less cohesive code in the TDD project than in two test-last projects. Coupling was slightly lower, but too little to conclude from (Siniaalto & Abrahamsson, 2007).

The authors note that studies of TDD’s effect on design were very scarce. The projects were small student projects, with the TDD team less experienced (Siniaalto & Abrahamsson, 2007). A five-project case study found that some parts of the code may deteriorate under TDD (Siniaalto & Abrahamsson, 2008).

Practitioner critics go further. Hansson argues that test-first at the unit level produces overly complex indirection and calls its design justification a dogma, from his own experience and without measurements (Heinemeier Hansson, 2014).

Adoption

Available field data suggest strict TDD is uncommon. A field study of 2,443 engineers monitored for 2.5 years in four development environments found that half did not test and that TDD was not widely practiced (Beller et al., 2019). A mining study of GitHub repositories found Java TDD projects relatively rare (Borle et al., 2017).

TDD with AI coding agents Weak evidence: few or small studies support this

Evidence on TDD with language models comes from a few recent preprints, and it supports giving a model tests more than telling it to follow TDD. Supplying test cases with a problem let GPT-4 Turbo solve an additional 12.0% of MBPP and 8.5% of HumanEval problems, two function-level coding benchmarks (Mathews & Nagappan, 2024). A test-first prompt scored highest on one benchmark for all three models tested (Yu et al., 2026g). A repository-level agent rose from 29.90% to 49.18% Pass@1 (the share of tasks solved on the first try) when given tests at every stage. Three to five tests worked better than more (Hu et al., 2025d).

Instructions to follow TDD, by contrast, did not help in the agent studies. Adding TDD instructions to a small model’s prompt without saying which tests to check raised the regression rate (previously working behaviour that breaks) from 6.08% to 9.94%. A tool that mapped code to the tests depending on it cut the rate to 1.82% (Alonso et al., 2026). Prompts that encouraged or discouraged writing test files changed resolved tasks by 0 to 13 of 500 per model, with no statistically significant difference (Chen et al., 2026q). Given human-written tests, an agent workflow solved 88.8% of SWE-Bench Lite and 94.3% of SWE-Bench Verified tasks, and its authors name writing valid reproduction tests as the main obstacle (Han et al., 2026e).

Tests written by the same model are a weaker check. Tests a model generated after seeing faulty code found faults less often than independently generated tests, 14% against 25%, because the code’s faults were replicated in the tests (Konstantinou et al., 2026b). In a benchmark of 1,000 web-app tasks where only tests serve as the prompt, weaker open models passed 7% to 33%. Rewriting the failing tests to match the model’s code lifted every model (Cui, 2025).

Further reading

Known gaps

What this article cannot tell you yet.

  1. Beck’s 2002 book was not read; the definition rests on his 2023 essay and on later descriptions.
  2. Effect sizes of the earlier meta-analyses (Rafique and Mišić, Munir et al., Bissi, Kollanus) could not be read; they are known through later summaries and an abstract.
  3. No study compares the classical (Detroit) and mockist (London) styles of TDD.
  4. No controlled comparison of test-first against test-last prompting on a current coding agent was found; the agent evidence is small preprints.
  5. No study has re-tested the finding that granularity and uniformity of cycles matter more than test-first order, and no direct replication of the 40-90% industrial defect reduction was found.
  6. Studies of experienced TDD practitioners over months, and the full text of several industrial studies (Bhat and Nagappan 2006, George and Williams 2004), were not read.
  7. TDD’s effect on maintainability and on design beyond a few small case studies was not researched.

References

Solid source
Rated highly trustworthy by the research, and every passage taken from it was found in it.
Weak source
Rated low, or a passage taken from it was not found in it.
Medium trust
Rated medium by the research.
Not checked
Rated high, but its passages could not all be checked against it.
Show all 38 references