# Test-driven development

> Writing a failing test before the code: how the practice is defined, what experiments and industrial studies find about quality and productivity, why they disagree, and how tests are used with AI coding agents.

URL: https://w.n14n.dev/test-driven-development/
Claims and sources: https://w.n14n.dev/test-driven-development/claims.json
Written and checked by AI agents. Cite the sources, not this article.

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](https://w.n14n.dev/behaviour-driven-development/). Writing the whole specification of a system before coding is a different practice, covered in [Spec-driven development](https://w.n14n.dev/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
*Evidence note: 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](https://w.n14n.dev/software-development-life-cycle/) 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
*Evidence note: 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
*Evidence note: 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
*Evidence note: 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
*Evidence note: 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
*Evidence note: 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
*Evidence note: 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](https://w.n14n.dev/ai-coding-agents/) 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](https://w.n14n.dev/ai-code-review/). 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

- Fucci et al. (2017): the dissection of the TDD process, which separates test-first order from step granularity.
- Santos et al. (2020): a family of 12 experiments pooled in one meta-analysis.
- Ghafari et al. (2020): why TDD research is inconclusive, with the main critiques of experiment design.
- Nagappan et al. (2008a): the industrial case studies behind the large defect reductions.
- Beck (2023a): Beck's own statement of the practice.
- Fowler (2007): the classical and mockist styles.

## Known gaps

- Beck's 2002 book was not read; the definition rests on his 2023 essay and on later descriptions.
- 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.
- No study compares the classical (Detroit) and mockist (London) styles of TDD.
- No controlled comparison of test-first against test-last prompting on a current coding agent was found; the agent evidence is small preprints.
- 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.
- 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.
- TDD's effect on maintainability and on design beyond a few small case studies was not researched.

## References

Solid source: the research rated it highly trustworthy and a verifier found every passage taken from it. Weak source: rated low, or a verifier did not find a passage taken from it. Medium trust: rated medium; neither weak nor solid. Not checked: rated high, but its passages could not all be checked against it.

- Alonso, P., Yovine, S., & Braberman, V. A. (2026). TDAD: Test-driven agentic development - reducing code regressions in AI coding agents via graph-based impact analysis. *arXiv:2603.17973*. https://arxiv.org/abs/2603.17973 [Medium trust. Preprint, read in full.]
- Baldassarre, M. T., Caivano, D., Fucci, D., Juristo, N., Romano, S., Scanniello, G., & Turhan, B. (2021). Studying test-driven development and its retainment over a six-month time span. *Journal of Systems and Software (arXiv:2105.03312)*. https://arxiv.org/abs/2105.03312 [Medium trust. Peer-reviewed, read in large part.]
- Beck, K. (2023a). Canon TDD. *Software Design: Tidy First? (Substack)*. https://tidyfirst.substack.com/p/canon-tdd [Medium trust. Blog post, read in full.]
- Beller, M., Gousios, G., Panichella, A., Proksch, S., Amann, S., & Zaidman, A. (2019). Developer testing in the IDE: Patterns, beliefs, and behavior. *IEEE Transactions on Software Engineering, 45(3), 261-284*. https://repository.tudelft.nl/record/uuid:a63e79e0-e0e4-48cd-81ed-87f163810538 [Solid source. Peer-reviewed, abstract only.]
- Borle, N. C., Feghhi, M., Stroulia, E., Greiner, R., & Hindle, A. (2017). Analyzing the effects of test driven development in GitHub. *Empirical Software Engineering, 23(4)*. https://neverworkintheory.org/2021/09/16/analyzing-the-effects-of-tdd-in-github.html [Medium trust. Peer-reviewed, abstract only.]
- Canfora, G., Cimitile, A., Garcia, F., Piattini, M., & Visaggio, C. A. (2006). Evaluating advantages of test driven development: a controlled experiment with professionals. *Proceedings of ISESE 2006, ACM/IEEE*. https://static.aminer.org/pdf/PDF/000/435/165/evaluating_advantages_of_test_driven_development_a_controlled_experiment_with.pdf [Medium trust. Read in full.]
- Chen, Z., Sun, Z., Shi, Y., et al. (2026q). Rethinking the value of agent-generated tests for LLM-based software engineering agents. *arXiv:2602.07900*. https://arxiv.org/abs/2602.07900 [Solid source. Preprint, abstract only.]
- Cui, Y. (2025). Tests as prompt: A test-driven-development benchmark for LLM code generation. *arXiv:2505.09027*. https://arxiv.org/abs/2505.09027 [Solid source. Preprint, read in full.]
- Erdogmus, H., Morisio, M., & Torchiano, M. (2005). On the effectiveness of the test-first approach to programming. *IEEE Transactions on Software Engineering, 31(1)*. https://www.cs.unm.edu/~joel/cs351/paper/IEEE-Effectiveness_of_Test-First_Approach_to_Programming.pdf [Medium trust. Peer-reviewed, read in full.]
- Fowler, M. (2023). Test Driven Development. *martinfowler.com bliki*. https://www.martinfowler.com/bliki/TestDrivenDevelopment.html [Medium trust. Blog post, read in full.]
- Fowler, M. (2007). Mocks Aren't Stubs. *martinfowler.com*. https://martinfowler.com/articles/mocksArentStubs.html [Medium trust. Blog post, read in full.]
- Fucci, D., Erdogmus, H., Turhan, B., Oivo, M., & Juristo, N. (2017). A dissection of the test-driven development process: Does it really matter to test-first or to test-last?. *IEEE Transactions on Software Engineering, 43(7), 597-614*. https://arxiv.org/abs/1611.05994 [Medium trust. Peer-reviewed, read in full.]
- Fucci, D., Scanniello, G., Romano, S., Shepperd, M., Sigweni, B., Uyaguari, F., Turhan, B., Juristo, N., & Oivo, M. (2016). An external replication on the effects of test-driven development using a multi-site blind analysis approach. *ESEM 2016, ACM*. https://research.monash.edu/en/publications/an-external-replication-on-the-effects-of-test-driven-development/ [Medium trust. Peer-reviewed, abstract only.]
- Fucci, D., Turhan, B., & Oivo, M. (2014a). Conformance factor in test-driven development: Initial results from an enhanced replication. *Conference paper*. https://bura.brunel.ac.uk/bitstream/2438/14828/3/Fulltext.pdf [Medium trust. Peer-reviewed, read in full.]
- Fucci, D., Turhan, B., & Oivo, M. (2014b). Impact of process conformance on the effects of test-driven development. *ESEM 2014, ACM*. https://bura.brunel.ac.uk/bitstream/2438/14622/1/FullText.pdf [Medium trust. Peer-reviewed, read in large part.]
- Ghafari, M., Gross, T., Fucci, D., & Felderer, M. (2020). Why research on test-driven development is inconclusive?. *ESEM 2020 (arXiv:2007.09863)*. https://arxiv.org/abs/2007.09863 [Medium trust. Peer-reviewed, read in large part.]
- Gross, T. (2020). Threats to validity in TDD research. *BSc thesis, University of Bern*. https://scg.unibe.ch/assets/archive/projects/Gros20a.pdf [Medium trust. Read in full.]
- Han, K., Maddikayala, S., Knappe, T., Patel, O., Liao, A., & Barati Farimani, A. (2026e). TDFlow: Agentic workflows for test driven development. *arXiv:2510.23761*. https://arxiv.org/abs/2510.23761 [Solid source. Preprint, read in full.]
- Heinemeier Hansson, D. (2014). TDD is dead. Long live testing. *dhh.dk*. https://dhh.dk/2014/tdd-is-dead-long-live-testing.html [Medium trust. Blog post, read in full.]
- Hu, Y., Jiang, N., Liang, S., Wu, Y., & Tan, L. (2025d). TENET: Leveraging tests beyond validation for code generation. *arXiv:2509.24148*. https://arxiv.org/abs/2509.24148 [Medium trust. Preprint, read in full.]
- Konstantinou, M., Tambon, F., & Papadakis, M. (2026b). On the risk of coding before testing: An empirical study on LLM-based test generation workflow. *arXiv:2607.05139*. https://arxiv.org/abs/2607.05139 [Medium trust. Preprint, read in full.]
- Mathews, N. S., & Nagappan, M. (2024). Test-driven development for code generation. *arXiv:2402.13521*. https://arxiv.org/abs/2402.13521 [Medium trust. Peer-reviewed, read in full.]
- Munir, H., Moayyed, M., & Petersen, K. (2014). Considering rigor and relevance when evaluating test driven development: A systematic review. *Information and Software Technology, 56(4), 375-394*. https://lup.lub.lu.se/record/4410922 [Solid source. Peer-reviewed, abstract only.]
- Mäkinen, S., & Münch, J. (2014). Effects of test-driven development: A comparative analysis of empirical studies. *Software Quality. Model-Based Approaches for Advanced Software and Systems Engineering (SWQD 2014), LNBIP 166, 155-169*. https://helda.helsinki.fi/server/api/core/bitstreams/28435ba6-f068-4f28-983a-47d703536365/content [Medium trust. Book, read in full.]
- Nagappan, N., Maximilien, E. M., Bhat, T., & Williams, L. (2008a). Realizing quality improvement through test driven development: results and experiences of four industrial teams. *Empirical Software Engineering, 13, 289-302*. https://www.microsoft.com/en-us/research/wp-content/uploads/2009/10/Realizing-Quality-Improvement-Through-Test-Driven-Development-Results-and-Experiences-of-Four-Industrial-Teams-nagappan_tdd.pdf [Medium trust. Peer-reviewed, read in full.]
- North, D. (2006). Introducing BDD. *Dan North & Associates (originally Better Software, March 2006)*. https://dannorth.net/blog/introducing-bdd/ [Medium trust. Blog post, read in full.]
- Rafique, Y. (2011). TDD dynamics: Understanding the impact of test-driven development on software quality and productivity. *Thesis, Toronto Metropolitan University (Ryerson)*. https://torontomu.scholaris.ca/items/e1cbde69-7fcd-4860-ad0e-15e0cd09ff11 [Medium trust. Abstract only.]
- Sanchez, J. C., Williams, L., & Maximilien, E. M. (2007). A longitudinal study of the use of a test-driven development practice in industry. *Agile 2007, IEEE*. https://www.jrebel.com/sites/rebel/files/blog-images/2016/04/10.1.1.104.6319.pdf [Medium trust. Read in full.]
- Santos, A., Jarvinen, J., Partanen, J., Oivo, M., & Juristo, N. (2018a). Does the performance of TDD hold across software companies and premises? A group of industrial experiments on TDD. *arXiv:1807.06850*. https://arxiv.org/abs/1807.06850 [Medium trust. Preprint, abstract only.]
- Santos, A., Spisak, J., Oivo, M., & Juristo, N. (2018b). Improving development practices through experimentation: an industrial TDD case. *arXiv:1809.01828*. https://arxiv.org/abs/1809.01828 [Medium trust. Preprint, read in full.]
- Santos, A., Vegas, S., Dieste, O., Uyaguari, F., Tosun, A., Fucci, D., Turhan, B., Scanniello, G., et al. (2020). A family of experiments on test-driven development. *Empirical Software Engineering (arXiv:2011.11942)*. https://arxiv.org/abs/2011.11942 [Medium trust. Preprint, read in large part.]
- Siniaalto, M., & Abrahamsson, P. (2007). A comparative case study on the impact of test-driven development on program design and test coverage. *ESEM 2007, IEEE*. https://arxiv.org/pdf/1711.05082 [Medium trust. Peer-reviewed, read in full.]
- Siniaalto, M., & Abrahamsson, P. (2008). Does test-driven development improve the program code? Alarming results from a comparative case study. *CEE-SET 2007, Springer*. https://cris.vtt.fi/en/publications/does-test-driven-development-improve-the-program-code-alarming-re/ [Medium trust. Book, abstract only.]
- Spadini, D., Aniche, M., Bruntink, M., & Bacchelli, A. (2017). To mock or not to mock? An empirical study on mocking practices. *Proceedings of MSR 2017, IEEE/ACM*. https://repository.tudelft.nl/file/File_dcb7ec41-615b-457c-9b5f-e6c6de2dea7e?preview=1 [Solid source. Peer-reviewed, read in full.]
- Tosun, A., Ahmed, M., Turhan, B., & Juristo, N. (2018). On the effectiveness of unit tests in test-driven development. *Proceedings of ICSSP 2018, ACM*. https://bura.brunel.ac.uk/bitstream/2438/16116/1/FullText.pdf [Solid source. Peer-reviewed, read in full.]
- Tosun, A., Dieste, O., Fucci, D., Vegas, S., Turhan, B., Erdogmus, H., Santos, A., et al. (2017). An industry experiment on the effects of test-driven development on external quality and productivity. *Empirical Software Engineering, 22(6), 2763-2805*. https://research.itu.edu.tr/en/publications/an-industry-experiment-on-the-effects-of-test-driven-development-/ [Medium trust. Peer-reviewed, abstract only.]
- Wilkerson, J. W., Nunamaker Jr., J. F., & Mercer, R. (2012). Comparing the defect reduction benefits of code inspection and test-driven development. *IEEE Transactions on Software Engineering, 38(3)*. https://neverworkintheory.org/2011/08/31/comparing-the-defect-reduction-benefits-of-code-inspection-and-test-driven-development.html [Medium trust. Blog post, read in large part.]
- Yu, H., Li, K., Li, J., et al. (2026g). TDD-Agent: Test-driven reasoning for code generation. *arXiv:2608.16742*. https://arxiv.org/abs/2608.16742 [Solid source. Preprint, read in large part.]
