- Test-driven development
Test-Driven Development (TDD ) is a
software developmenttechnique consisting of short iterations where new test cases covering the desired improvement or new functionality are written first, then the production code necessary to pass the tests is implemented, and finally the software is refactored to accommodate changes. The availability of tests before actual development ensures rapid feedback after any change.Practitioners emphasize that test-driven development is a method of designing software, not merely a method of testing.
Test-Driven Development is related to the test-first programming concepts of
Extreme Programming, begun in the late 20th century, but more recently is creating more general interest in its own right.Newkirk, JW and Vorontsov, AA. "Test-Driven Development in Microsoft .NET", Microsoft Press, 2004.]
Along with other techniques, the concept can also be applied to the improvement and removal of software defects from legacy code that was not developed in this way Feathers, M. Working Effectively with Legacy Code, Prentice Hall, 2004] .
Test-driven development requires that an automated unit test, defining requirements of the code, is written before each aspect of the code itself. These tests contain assertions that are either true or false. Running the tests gives rapid confirmation of correct behaviour as the code evolves and is refactored. Testing frameworks based on the
xUnitconcept (see the list of unit testing frameworksfor an exhaustive list) provide a mechanism for creating and running sets of automated test cases.
Test-Driven Development Cycle
The following sequence is based on the book "
Test-Driven Development by Example" Beck, K. Test-Driven Development by Example, Addison Wesley, 2003] , which many consider to be the canonical source text on the concept in its modern form.
1. Add a test
In test-driven development, each new feature begins with writing a test. This test must inevitably fail because it is written before the feature has been implemented. In order to write a test, the developer must understand the specification and the requirements of the feature clearly. This may be accomplished through
use cases and user stories to cover the requirements and exception conditions. This could also imply a variant, or modification of an existing test. This is a differentiating feature of test-driven development versus writing unit tests "after" the code is written: it makes the developer focus on the requirements "before" writing the code, a subtle but important difference.
2. Run all tests and see if the new one fails
This validates that the test harness is working correctly and that the new test does not mistakenly pass without requiring any new code.
The new test should also fail for the expected reason. This step tests the test itself, in the negative: it rules out the possibility that the new test will always pass, and therefore be worthless.
3. Write some code
The next step is to write some code that will cause the test to pass. The new code written at this stage will not be perfect and may, for example, pass the test in an inelegant way. That is acceptable because later steps will improve and hone it.
It is important that the code written is "only" designed to pass the test; no further (and therefore untested) functionality should be predicted and 'allowed for' at any stage.
4. Run the automated tests and see them succeed
If all test cases now pass, the programmer can be confident that the code meets all the tested requirements. This is a good point from which to begin the final step of the cycle.
5. Refactor code
Now the code can be cleaned up as necessary. By re-running the test cases, the developer can be confident that refactoring is not damaging any existing functionality. The concept of removing duplication is an important aspect of any software design. In this case, however, it also applies to removing any duplication between the test code and the production code — for example magic numbers or strings that were repeated in both, in order to make the test pass in step 3.
Starting with another new test, the cycle is then repeated to push forward the functionality. The size of the steps can be as small as the developer likes, or get larger if s/he feels more confident. If the code written to satisfy a test does not fairly quickly do so, then the step-size may have been too big, and maybe the smaller testable steps should be used instead. When using external libraries it is important not to make increments that are so small as to be effectively merely testing the library itself , unless there is some reason to believe that the library is buggy or is not sufficiently feature complete to serve all the needs of the main program being written.
There are various aspects to using test-driven development, for example the principles of "Keep It Simple, Stupid" (KISS) and "You Ain't Gonna Need It" (YAGNI). By focusing on writing only the code necessary to pass tests, designs can be cleaner and clearer than is often achieved by other methods. In "Test-Driven Development by Example" Kent Beck also suggests the principle "Fake it, till you make it".
To achieve some advanced design concept (such as a
Design Pattern), tests are written that will generate that design. The code may remain simpler than the target pattern, but still pass all required tests. This can be unsettling at first but it allows the developer to focus only on what is important.
Test-driven development requires the programmer to first fail the test cases. The idea is to ensure that the test really works and can catch an error. Once this is shown, the normal cycle will commence. This has been coined the "Test-Driven Development Mantra", known as red/green/refactor where red means "fail" and green is "pass".
Test-driven development constantly repeats the steps of adding test cases that fail, passing them, and refactoring. Receiving the expected test results at each stage reinforces the programmer's mental model of the code, boosts confidence and increases productivity.
Advanced practices of test-driven development can lead to [http://testdrivendeveloper.com/default.aspx#a9eda991a-3ada-4e70-84d8-1194fa5edda7 Acceptance Test-driven development] [ATDD] where the criteria specified by the customer are automated into acceptance tests, which then drive the traditional unit test-driven development [UTDD] process. This process ensures the customer has an automated mechanism to decide whether the software meets their requirements. With ATDD, the development team now has a specific target to satisfy, the acceptance tests - which keeps them continuously focused on what the customer really wants from that user story.
A 2005 study found that using TDD meant writing more tests and, in turn, programmers that wrote more tests tended to be more productive. [cite web
url = http://iit-iti.nrc-cnrc.gc.ca/publications/nrc-47445_e.html
title = On the Effectiveness of Test-first Approach to Programming
accessdate = 2008-01-14
last = Erdogmus
first = Hakan
coauthors = Morisio, Torchiano
publisher = Proceedings of the IEEE Transactions on Software Engineering, 31(1). January 2005. (NRC 47445)
quote = We found that test-first students on average wrote more tests and, in turn, students who wrote more tests tended to be more productive.] Hypotheses relating to code quality and a more direct correlation between TDD and productivity were inconclusive. [cite web
url = http://theruntime.com/blogs/jacob/archive/2008/01/22/tdd-proven-effective-or-is-it.aspx
title = TDD Proven Effective! Or is it?
accessdate = 2008-02-21
last = Proffitt
first = Jacob
quote = So TDD's relationship to quality is problematic at best. Its relationship to productivity is more interesting. I hope there's a follow-up study because the productivity numbers simply don't add up very well to me. There is an undeniable correlation between productivity and the number of tests, but that correlation is actually stronger in the non-TDD group (which had a single outlier compared to roughly half of the TDD group being outside the 95% band).]
Programmers using pure TDD on new ("greenfield") projects report they only rarely feel the need to invoke a
debugger. Used in conjunction with a Version control system, when tests fail unexpectedly, reverting the code to the last version that passed all tests may often be more productive than debugging. [cite web
url = http://clarkware.com/courses/TDDWithJUnit.html
title = Test-Driven Development with JUnit Workshop
accessdate = 2007-11-01
last = Clark
first = Mike
publisher = Clarkware Consulting, Inc.
quote = In fact, test-driven development actually helps you meet your deadlines by eliminating debugging time, minimizing design speculation and re-work, and reducing the cost and fear of changing working code. ] [cite web
url = http://www.gamesfromwithin.com/articles/0502/000073.html
title = Stepping Through the Looking Glass: Test-Driven Game Development (Part 1)
accessdate = 2007-11-01
last = Llopis
first = Noel
date = 20 February 2005
publisher = Games from Within
quote = Comparing [TDD] to the non-test-driven development approach, you're replacing all the mental checking and debugger stepping with code that verifies that your program does exactly what you intended it to do.]
Test-driven development can help to build software better and faster.Fact|date=February 2008 It offers more than just simple validation of correctness, but can also drive the design of a program. By focusing on the test cases first, one must imagine how the functionality will be used by clients (in this case, the test cases). Therefore, the programmer is only concerned with the interface and not the implementation. This benefit is complementary to
Design by Contractas it approaches code through test cases rather than through mathematical assertions or preconceptions.
The power test-driven development offers is the ability to take small steps when required. It allows a programmer to focus on the task at hand as the first goal is to make the test pass. Exceptional cases and error handling are not considered initially. Tests to create these extraneous circumstances are implemented separately. Another advantage is that test-driven development, when used properly, ensures that all written code is covered by a test. This can give the programmer, and subsequent users, a greater level of trust in the code.
While it is true that more code is required with TDD than without TDD because of the unit test code, total code implementation time is typically shorter. [cite web
url = http://www.ipd.uka.de/mitarbeiter/muellerm/publications/edser03.pdf
title = About the Return on Investment of Test-Driven Development
accessdate = 2007-11-01
last = Müller
first = Matthias M.
coauthors = Padberg, Frank
format = PDF
publisher = Universität Karlsruhe, Germany
pages = 6 ] Large numbers of tests help to limit the number of defects in the code. The early and frequent nature of the tests helps to catch defects early in the development cycle, preventing them from becoming endemic and expensive problems. Eliminating defects early in the process usually avoids lengthy and tedious debugging later in the project.
TDD can lead to more modularized, flexible, and extensible code. This effect often comes about because the methodology requires that the developers think of the software in terms of small units that can be written and tested independently and integrated together later. This leads to smaller, more focused classes, looser coupling, and cleaner interfaces. The use of the
Mock Objectdesign pattern also contributes to the overall modularization of the code because this pattern requires that the code be written so that modules can be switched easily between mock versions for unit testing or "real" version for deployment.
Because no more code is written than necessary to pass a failing test case, automated tests tend to cover every code path. For example, in order for a TDD developer to add an else branch off an existing if branch, the developer would first have to have written a failing test case that motivates the branch. As a result, the automated tests developed through strict application of TDD tend to be very robust, and can detect any significant functional mutation to the code base.
# Test-Driven Development is difficult to use in situations where full functional tests are required to determine success or failure. Examples of these are user interfaces, programs that work with databases, and some that depend on specific network configurations. TDD encourages developers to put the minimum amount of functional code into such modules and maximise the logic that is extracted into testable library code, using fakes and mocks to represent the outside world.
# Management support is essential. Without the entire organization believing that Test-Driven Development is going to improve the product, management will feel that time spent writing tests is wasted [http://people.apache.org/~stevel/slides/testing.pdf] .
# Testing has historically been viewed as a lower status position than developer or architect. This can be seen in products such as Visual Studio 2005, whose "Architect Edition" [http://msdn.microsoft.com/en-us/vsts2008/aa718756.aspx] lacked the testing facilities that the "Testing Edition" offered [http://msdn.microsoft.com/en-us/vsts2008/aa718941.aspx] .
# The tests themselves become part of the maintenance overhead of a project. Badly written tests, for example ones that check hard-coded error strings or which are themselves prone to failure, are expensive to maintain. There is a risk that tests that regularly generate false failures will be ignored, so that when a real failure occurs it may not be detected. It is possible to write tests for low and easy maintenance, for example by the reuse of error strings, and this should be a goal during the 'Refactor' phase described above.
There are two kinds of testing code: black box and white box, sometimes called glass box testing. Black box unit tests functionality at the interface boundaries. Nearly all unit tests are structured as black-box tests, because it guarantees software modularity, and forces an emphasis on the interface of the module. White box testing occurs when your tests can both observe and mutate state belonging to the software under test. These kinds of tests are strongly discouraged, because subtle bugs can appear if the test itself is buggy. Glass box testing occurs when your tests can only observe, but "not" mutate, the state belonging to the production code. Applications of glass box testing include hardware-level verification of a function's output. For example, verifying a skip-list's links are properly set is vital to the successful and bug-free operation of a skip-list's implementation.
Test-suite code clearly has to be able to access the code it is testing. In almost every case imaginable, this access occurs through the published interface of function, procedure, or method calls. The use of "mock objects" ensures
information hidingremains intact, guaranteeing a total separation of concerns.
Unit test code for TDD is almost "never" written within the same project or module as the code being tested. By placing tests in a separate module or library, the production code remains pristine. Placing the TDD code inside the same module would fundamentally alter the production code. Use of conditional compilation directives can introduce subtle bugs.
Some may object that using strict black box testing does not provide access to
privatedata and methods. This is intentional; as the software evolves, you may find the implementation of a class changes fundamentally. Remember a critical step of test-driven development is to refactor. Refactoring may introduce changes which adds or removes private members, or alters an existing member's type. These changes ought not break existing tests. Unit tests that exploit glass box testing are highly coupled to the production software; changing the implementation of a class or module may mean you must also update or discard existing tests, things which should never have to occur. For this reason, glass box testing must be kept to the minimum possible. White box testing should "never" be used in test-driven development.
In all cases, thought must be given to the question of deployment. The best approach is to develop your software so that you have three major components. The first major component is the unit test runner application framework itself. The second is the main entry module for the production logic. "Both" of these modules would link (preferably dynamically) to one or more "libraries," each implementing some or all of the business logic under development. This guarantees total modularity and is thoroughly deployable.
Fakes, mocks and integration tests
Unit tests are so-named because they each test "one unit" of code. Whether a module of code has hundreds of unit tests or only five is irrelevant. A test suite should never cross process boundaries in a program, let alone network connections. Doing either introduces delays, which make tests run slowly, which in turn discourages developers from running the whole suite. Introducing dependencies on external modules and/or data also turns "unit tests" into "integration tests". If one module misbehaves in a chain of inter-related modules, it may not be clear where to look for the cause of the failure.
When code under development relies on a database or a web service or any other external process or service, enforcing a unit-testable separation is an opportunity and a driving force to design more modular, more testable and more re-usable code [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 ] . Two steps are necessary:
# Whenever external access is going to be needed in the final design, an interface should be defined that describes the access that will be available.
# The interface should be implemented in two ways, one of which really accesses the external process, and the other is a fake or
mock object. Fake objects need do little more than add a message such as "Person object saved" to a trace-log or to the console. Mock objects differ in that they themselves contain test assertions that can make the test fail, for example, if the person's name and other data are inconsistent. 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 pre-defined fault-modes so that error handling routines can be developed and reliably tested.
A corollary of this approach is that the actual database or other external-access code is never tested by the TDD process itself. To avoid this, other tests are needed that instantiate the test-driven code with the 'real' implementations of the interfaces discussed above. Many developers find it useful to keep these tests quite separate from the TDD unit tests, and refer to them as "integration tests". There will be fewer of them, and they need be run less often than the unit tests. They can nonetheless be implemented using the same testing framework, for example
Integration tests that alter any persistent store or database should always be careful to leave them in a state ready for re-use, even if any test fails. This can be achieved using some combination of the following techniques where relevant and available to the developer:
TearDownmethod integrated into many test frameworks
exception handlingstructures where available
database transactionswhere a transaction 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 Ant or
Frameworks such as jMock and Rhino Mocks exist to make the process of creating and using complex mock objects easier.
* Aegis a software change management system which supports a test-driven development workflow
List of software development philosophies
List of unit testing frameworks
Software Development Rhythms
Behavior Driven Development
Design by Contract
* [http://www.vimeo.com/1653402 A Webcast Illustrating Test-Driven Development]
* [http://pillartechnology.com/index.php/verde Jump starting Test Driven Development]
* [http://126.96.36.199/search?q=cache:91_i_rkhcEQJ:butunclebob.com/ArticleS.UncleBob.TheThreeRulesOfTdd+http://butunclebob.com/ArticleS.UncleBob.TheThreeRulesOfTdd&hl=en&ct=clnk&cd=1&gl=uk&client=firefox-a Three Rules of TDD] (cached)
* [http://www.testdriven.com testdriven.com] on-line test-driven development community
* [http://testdrivendeveloper.com/2008/04/02/AcceptanceTestDrivenDevelopmentExplained.aspx Acceptance Test Driven Development - Explained]
* [http://c2.com/cgi/wiki?TestDrivenDevelopment c2.com] Test-driven development from WikiWikiWeb
* [http://www.agileadvice.com/archives/2005/05/the_qualities_o.html Brief explanation of the Qualities of an Ideal Test]
* [http://blog.james-carr.org/?p=44 TDD Anti-Patterns] Common mistakes and mishaps when starting out with TDD... a catalog of how to NOT do TDD.
* [http://www.agiledata.org/essays/tdd.html Introduction to Test-Driven Design (TDD)]
* [http://www.objectmentor.com/resources/articles/xpepisode.htm XP Episode] By Bob Martin and Bob Koss
* [http://www.methodsandtools.com/archive/archive.php?id=59 Mocking the Embedded World] is an article about adapting the test driven development approach to the embedded software development world with tools suggestions and a case study.
* [http://homepage.mac.com/hey.you/lessons.html Test Driven Development lessons learned]
* [http://www.methodsandtools.com/archive/archive.php?id=72 Acceptance TDD Explained]
Testing-Driven Development X Design by Contract
* [http://www.eiffel.com/general/monthly_column/2004/september.html Test or spec? Test and spec? Test from spec!] , by
Bertrand Meyer(September 2004)
* [http://www.junit.org/ JUnit]
* [http://www.concordion.org Concordion] acceptance testing framework for Java
* [http://www.jmock.org/index.html jMock]
* [http://fitnesse.org FitNesse] The fully integrated standalone wiki, and acceptance testing framework.
* [http://www.nunit.org NUnit: a unit-testing framework initially ported from JUnit]
* [http://www.mbunit.com MBUnit: the Generative Unit Test Framework for the .NET Framework]
* [http://msdn.microsoft.com/en-us/library/ms379625(VS.80).aspx Microsoft Visual Studio Team Test from a TDD approach]
* [http://msdn.microsoft.com/en-us/magazine/cc163665.aspx Write Maintainable Unit Tests That Will Save You Time And Tears]
* [http://www.methodsandtools.com/archive/archive.php?id=20 Improving Application Quality Using Test-Driven Development] by Craig Murphy. This article provides an introduction to Test-Driven Development with concrete examples using
* [http://www.parlezuml.com/tutorials/agiledotnet/tdd_nunit.pdf Test-driven Development using NUnit] tutorial (also [http://www.parlezuml.com/tutorials/agilejava/tdd_junit.pdf Java version] available)
* [http://www.software-architects.com/TechnicalArticles/TestDrivenDevelopment/tabid/83/language/en-US/Default.aspx Test Driven Development in .NET] example for TDD in Visual Studio and .NET including WatiN test framework for web applications
Testing in other languages
* [http://www.extremeperl.org/bk/unit-testing Extreme perl - Unit testing]
Wikimedia Foundation. 2010.