Test-driven development: Difference between revisions

Content deleted Content added
Citation bot (talk | contribs)
Add: date, authors 1-1. Removed parameters. Some additions/deletions were parameter name changes. | Use this bot. Report bugs. | Suggested by Abductive | Category:Extreme programming | #UCB_Category 22/41
 
(29 intermediate revisions by 20 users not shown)
Line 1:
{{shortShort description|Coding techniqueMethod of repetitively writing test code then production code}}
{{Software development process}}
 
'''Test-driven development''' ('''TDD''') is a way of writing [[source code|code]] that involves writing an [[test automation|automated]] [[unit testing|unit-level]] [[Test case (software)|test case]] that fails, then writing just enough code to make the test pass, then [[refactoring]] both the test code and the production code, then repeating with another new test case.
 
Alternative approaches to writing automated tests is to write all of the production code before starting on the test code or to write all of the test code before starting on the production code. With TDD, both are written together, therefore shortening debugging time necessities.<ref>{{Cite journal |last1=Parsa |first1=Saeed |last2=Zakeri-Nasrabadi |first2=Morteza |last3=Turhan |first3=Burak |date=2025-01-01 |title=Testability-driven development: An improvement to the TDD efficiency |url=https://www.sciencedirect.com/science/article/pii/S0920548924000461 |journal=Computer Standards & Interfaces |volume=91 |pages=103877 |doi=10.1016/j.csi.2024.103877 |issn=0920-5489|url-access=subscription }}</ref>
 
TDD is related to the test-first programming concepts of [[extreme programming]], begun in 1999,<ref name="Cworld92">{{cite web |url=http://www.computerworld.com/softwaretopics/software/appdev/story/0,10801,66192,00.html |title=Extreme Programming |author=Lee Copeland |date=December 2001 |publisher=Computerworld |access-date=January 11, 2011 |archive-url=https://web.archive.org/web/20110605060209/http://www.computerworld.com/s/article/66192/Extreme_Programming?taxonomyId=063 |archive-date=June 5, 2011 |url-status=dead }}</ref> but more recently has created more general interest in its own right.<ref name=Newkirk>Newkirk, JW and Vorontsov, AA. ''Test-Driven Development in Microsoft .NET'', Microsoft Press, 2004.</ref>
Line 16:
| text = The original description of TDD was in an ancient book about programming. It said you take the input tape, manually type in the output tape you expect, then program until the actual output tape matches the expected output. After I'd written the first xUnit framework in [[Smalltalk]] I remembered reading this and tried it out. That was the origin of TDD for me. When describing TDD to older programmers, I often hear, "Of course. How else could you program?" Therefore I refer to my role as "rediscovering" TDD.
| author = [[Kent Beck]]
| title =
| title = Why does Kent Beck refer to the "rediscovery" of test-driven development? What's the history of test-driven development before Kent Beck's rediscovery?
| source = "Why does Kent Beck refer to the 'rediscovery' of test-driven development? What's the history of test-driven development before Kent Beck's rediscovery?"<ref>{{cite web|url=http://www.quora.com/Why-does-Kent-Beck-refer-to-the-rediscovery-of-test-driven-development |title=Why does Kent Beck refer to the "rediscovery" of test-driven development? |author=Kent Beck | date=May 11, 2012 |access-date=December 1, 2014}}</ref>
}}
 
Line 23:
[[File:TDD Global Lifecycle.png|thumb|A graphical representation of the test-driven development lifecycle]]
 
The TDD steps vary somewhat by author in count and description, but are generally as follows. These are based on the book ''Test-Driven Development by Example'',<ref name=Beck>{{cite book |last=Beck| first=Kent |title=Test-Driven Development by Example |publisher=Addison Wesley |___location=Vaseem |date=2002-11-08 |isbn=978-0-321-14653-3}}</ref> and Kent Beck's [Canon TDD article.<ref>{{Cite web |last=Beck |first=Kent |date=2023-12-11 |title=Canon TDD |url=https://tidyfirst.substack.com/p/canon-tdd Canon|access-date=2024-10-22 TDD|website=Software article].Design: Tidy First?}}</ref>
 
;1. List scenarios for the new feature
Line 39:
:* moving code to where it most logically belongs
:* removing [[duplicate code]]
:* making [[nameIdentifier (computer languages)|names]]s [[Self-documenting code|self-documenting]]
:* splitting methods into smaller pieces
:* re-arranging [[Inheritance (object-oriented programming)|inheritance hierarchies]]
Line 69:
 
===Code visibility===
{{Main|Unit_testing#Code_Visibility}}
Test code needs access to the code it is testing, but testing should not compromise normal design goals such as [[information hiding]], encapsulation and the [[separation of concerns]]. Therefore, unit test code is usually located in the same project or [[Module (programming)|module]] as the code being tested.
In test-driven development, writing tests before implementation raises questions about testing [[access modifiers|private methods]] versus testing only through [[Interface (computing)|public interfaces]]. This choice affects the design of both test code and production code.
 
===Test isolation===
In [[object oriented design]] this still does not provide access to private data and methods. Therefore, extra work may be necessary for unit tests. In [[Java (programming language)|Java]] and other languages, a developer can use [[Reflection (computer science)|reflection]] to access private fields and methods.<ref>{{cite web
Test-driven development relies primarily on [[unit testing|unit tests]] for its rapid red-green-refactor cycle. These tests execute quickly by avoiding process boundaries, network connections, or external dependencies. While TDD practitioners also write [[integration testing|integration tests]] to verify component interactions, these slower tests are kept separate from the more frequent unit test runs. Testing multiple integrated modules together also makes it more difficult to identify the source of failures.
|title=Subverting Java Access Protection for Unit Testing
|url=http://www.onjava.com/pub/a/onjava/2003/11/12/reflection.html
|last=Burton
|first=Ross
|date=2003-11-12
|publisher=O'Reilly Media, Inc.
|access-date=2009-08-12
}}</ref> Alternatively, an [[inner class]] can be used to hold the unit tests so they have visibility of the enclosing class's members and attributes. In the [[.NET Framework]] and some other programming languages, [[partial class]]es may be used to expose private methods and data for the tests to access.
 
When code under development relies on a database, a web service, or any other external process or servicedependencies, enforcingTDD aencourages unit-testablethe separationuse isof also[[test an opportunity and a driving forcedouble]]s to design moremaintain modularfast, moreisolated testableunit and more reusable codetests.<ref>{{cite book |title=Refactoring - Improving the design of existing code |last=Fowler |first=Martin |year=1999 |publisher=Addison Wesley Longman, Inc. |___location=Boston |isbn=0-201-48567-2 |url=https://archive.org/details/isbn_9780201485677 }}</ref> TwoThe stepstypical areapproach necessary:involves using interfaces to separate external dependencies and implementing [[Test_double#Implementation_approaches|test double]]s for testing purposes.
It is important that such testing hacks do not remain in the production code. In [[C (programming language)|C]] and other languages, [[Directive (programming)|compiler directives]] such as <code>#if DEBUG ... #endif</code> can be placed around such additional classes and indeed all other test-related code to prevent them being compiled into the released code. This means the released code is not exactly the same as what was unit tested. The regular running of fewer but more comprehensive, end-to-end, integration tests on the final release build can ensure (among other things) that no production code exists that subtly relies on aspects of the test harness.
 
Since test doubles don't prove the connection to real external components, TDD practitioners supplement unit tests with [[integration testing]] at appropriate levels. To keep execution faster and more reliable, testing is maximized at the unit level while minimizing slower tests at higher levels.
There is some debate among practitioners of TDD, documented in their blogs and other writings, as to whether it is wise to test private methods and data anyway. Some argue that private members are a mere implementation detail that may change, and should be allowed to do so without breaking numbers of tests. Thus it should be sufficient to test any class through its public interface or through its subclass interface, which some languages call the "protected" interface.<ref>{{cite web | url=https://www.python.org/dev/peps/pep-0008/ |title=PEP 8 -- Style Guide for Python Code |last1=van Rossum |first1=Guido |last2=Warsaw |first2=Barry |date=5 July 2001 |publisher=Python Software Foundation |access-date=6 May 2012}}</ref> Others say that crucial aspects of functionality may be implemented in private methods and testing them directly offers advantage of smaller and more direct unit tests.<ref>{{cite web
|url=http://blogs.msdn.com/jamesnewkirk/archive/2004/06/07/150361.aspx
|title=Testing Private Methods/Member Variables - Should you or shouldn't you
|last=Newkirk
|first=James
|date=7 June 2004
|publisher=Microsoft Corporation
|access-date=2009-08-12}}</ref><ref>{{cite web
|url=http://www.codeproject.com/KB/cs/testnonpublicmembers.aspx
|title=How to Test Private and Protected methods in .NET
|last=Stall
|first=Tim
|date=1 Mar 2005
|publisher=CodeProject
|access-date=2009-08-12}}</ref>
 
===Fakes, mocks and integration tests===
Unit tests are so named because they each test ''one unit'' of code. A complex module may have a thousand unit tests and a simple module may have only ten. The unit tests used for TDD should never cross process boundaries in a program, let alone network connections. Doing so introduces delays that make tests run slowly and discourage developers from running the whole suite. Introducing dependencies on external modules or data also turns ''unit tests'' into ''integration tests''. If one module misbehaves in a chain of interrelated modules, it is not so immediately clear where to look for the cause of the failure.
 
When code under development relies on a database, a web service, or any other external process or service, enforcing a unit-testable separation is also an opportunity and a driving force to design more modular, more testable and more reusable code.<ref>{{cite book |title=Refactoring - Improving the design of existing code |last=Fowler |first=Martin |year=1999 |publisher=Addison Wesley Longman, Inc. |___location=Boston |isbn=0-201-48567-2 |url=https://archive.org/details/isbn_9780201485677 }}</ref> Two steps are necessary:
 
# Whenever external access is needed in the final design, an [[Interface (computer science)|interface]] should be defined that describes the access available. See the [[dependency inversion principle]] for a discussion of the benefits of doing this regardless of TDD.
# The interface should be implemented in two ways, one of which really accesses the external process, and the other of which is a [[mock object|fake or mock]]. Fake objects need do little more than add a message such as "Person object saved" to a [[Tracing (software)|trace log]], against which a test [[Assertion (computing)|assertion]] can be run to verify correct behaviour. Mock objects differ in that they themselves contain [[Assertion (computing)|test assertions]] that can make the test fail, for example, if the person's name and other data are not as expected.
 
Fake and mock object methods that return data, ostensibly from a data store or user, can help the test process by always returning the same, realistic data that tests can rely upon. They can also be set into predefined fault modes so that error-handling routines can be developed and reliably tested. In a fault mode, a method may return an invalid, incomplete or [[Null character|null]] response, or may throw an [[Exception handling|exception]]. Fake services other than data stores may also be useful in TDD: A fake encryption service may not, in fact, encrypt the data passed; a fake random number service may always return 1. Fake or mock implementations are examples of [[dependency injection]].
 
A test double is a test-specific capability that substitutes for a system capability, typically a class or function, that the UUT depends on. There are two times at which test doubles can be introduced into a system: link and execution. Link time substitution is when the test double is compiled into the load module, which is executed to validate testing. This approach is typically used when running in an environment other than the target environment that requires doubles for the hardware level code for compilation. The alternative to linker substitution is run-time substitution in which the real functionality is replaced during the execution of a test case. This substitution is typically done through the reassignment of known function pointers or object replacement.
 
Test doubles are of a number of different types and varying complexities:
* [[Dummy code|Dummy]] – A dummy is the simplest form of a test double. It facilitates linker time substitution by providing a default return value where required.
* [[Method stub|Stub]] – A stub adds simplistic logic to a dummy, providing different outputs.
* Spy – A spy captures and makes available parameter and state information, publishing accessors to test code for private information allowing for more advanced state validation.
* [[Mock object|Mock]] – A mock is specified by an individual test case to validate test-specific behavior, checking parameter values and call sequencing.
* Simulator – A simulator is a comprehensive component providing a higher-fidelity approximation of the target capability (the thing being doubled). A simulator typically requires significant additional development effort.<ref name="Pathfinder Solutions" />
 
A corollary of such dependency injection is that the actual database or other external-access code is never tested by the TDD process itself. To avoid errors that may arise from this, other tests are needed that instantiate the test-driven code with the "real" implementations of the interfaces discussed above. These are [[Integration testing|integration tests]] and are quite separate from the TDD unit tests. There are fewer of them, and they must be run less often than the unit tests. They can nonetheless be implemented using the same testing framework.
 
Integration tests that alter any [[persistent storage|persistent store]] or database should always be designed carefully with consideration of the initial and final state of the files or database, even if any test fails. This is often achieved using some combination of the following techniques:
* The <code>TearDown</code> method, which is integral to many test frameworks.
* <code>try...catch...finally</code> [[exception handling]] structures where available.
* [[Database transactions]] where a transaction [[atomicity (database systems)|atomically]] includes perhaps a write, a read and a matching delete operation.
* Taking a "snapshot" of the database before running any tests and rolling back to the snapshot after each test run. This may be automated using a framework such as [[Apache Ant|Ant]] or [[NAnt]] or a [[continuous integration]] system such as [[CruiseControl]].
* Initialising the database to a clean state ''before'' tests, rather than cleaning up ''after'' them. This may be relevant where cleaning up may make it difficult to diagnose test failures by deleting the final state of the database before detailed diagnosis can be performed.
 
===Keep the unit small===
Line 152 ⟶ 104:
* Dependencies between test cases. A test suite where test cases are dependent upon each other is brittle and complex. Execution order should not be presumed. Basic refactoring of the initial test cases or structure of the UUT causes a spiral of increasingly pervasive impacts in associated tests.
* Interdependent tests. Interdependent tests can cause cascading false negatives. A failure in an early test case breaks a later test case even if no actual fault exists in the UUT, increasing defect analysis and debug efforts.
* Testing precise execution, behavior, timing or performance.
* Building "all-knowing oracles". An oracle that inspects more than necessary is more expensive and brittle over time. This very common error is dangerous because it causes a subtle but pervasive time sink across the complex project.<ref name="pathfindersolns.com">{{YouTube| id=0BWSms3J40Y| title=Test-Driven Development (TDD) for Complex Systems Introduction}} by Pathfinder Solutions</ref>{{Clarify|reason=needs better explanation, what is an all-knowing oracle? needs better tone, more factual|date=February 2022}}
* Testing implementation details.
Line 198 ⟶ 150:
Creating and managing the [[Software architecture|architecture]] of test software within a complex system is just as important as the core product architecture. Test drivers interact with the UUT, [[test double]]s and the unit test framework.<ref name="Pathfinder Solutions" />
 
== Advantages and Disadvantages of Test Driven Development ==
 
=== Advantages ===
Line 206 ⟶ 158:
# '''Comprehensive Test Coverage''': TDD ensures that all new code is covered by at least one test, leading to more robust software.
# '''Enhanced Confidence in Code''': Developers gain greater confidence in the code's reliability and functionality.
# '''Enhanced Confidence in Tests''': As the tests are known to be failing without the proper implementation, we know that the tests actually tests the implementation correctly.
# '''Well-Documented Code''': The process naturally results in well-documented code, as each test clarifies the purpose of the code it tests.
# '''Requirement Clarity''': TDD encourages a clear understanding of requirements before coding begins.
Line 220 ⟶ 173:
 
# '''Increased Code Volume''': Implementing TDD can result in a larger codebase as tests add to the total amount of code written.
# '''False Security from Tests''': A large number of passing tests can sometimes give a misleading sense of security regarding the code's robustness.<ref>{{Cite journal |last1=Parsa |first1=Saeed |last2=Zakeri-Nasrabadi |first2=Morteza |last3=Turhan |first3=Burak |date=2025-01-01 |title=Testability-driven development: An improvement to the TDD efficiency |url=https://www.sciencedirect.com/science/article/pii/S0920548924000461 |journal=Computer Standards & Interfaces |volume=91 |pages=103877 |doi=10.1016/j.csi.2024.103877 |issn=0920-5489|url-access=subscription }}</ref>
# '''Maintenance Overheads''': Maintaining a large suite of tests can add overhead to the development process.
# '''Time-Consuming Test Processes''': Writing and maintaining tests can be time-consuming.
# '''Testing Environment Set-Up''': TDD requires setting up and maintaining a suitable testing environment.
# '''Learning Curve''': It takes time and effort to become proficient in TDD practices.
# '''Overcomplication''': AnDesigning overemphasiscode to cater for complex tests onvia TDD can lead to code that is more complexcomplicated than necessary.
# '''Neglect of Overall Design''': Focusing too narrowly on passing tests can sometimes lead to neglect of the bigger picture in software design.
# '''Increased Costs''': The additional time and resources required for TDD can result in higher development costs.
 
=== Benefits ===
Line 311 ⟶ 263:
| url=https://www.simple-talk.com/dotnet/.net-framework/are-unit-tests-overused/
| title=Are Unit Tests Overused?
| work=Simple Talk
| publisher=Simple-talk.com
| date=2012-10-19 |access-date=2014-03-25}}</ref>
Line 357 ⟶ 310:
* [[Self-testing code]]
* [[Software testing]]
* [[Test case]]
* [[Transformation Priority Premise]]
* [[Unit testing]]