Test Driven Development Method in Software Development Process
Test Driven Development Method in Software Development Process
Test-driven development requires developers to create The next step is to write some code that will cause the test
automated unit tests that define code requirements to pass. The new code written at this stage will not be perfect
(immediately) before writing the code itself. The tests contain and may, for example, pass the test in an inelegant way. That
assertions that are either true or false. Passing the tests is acceptable because later steps will improve and hone it.
confirms correct behavior as developers evolve and refactor
the code. Developers often use testing frameworks to create It is important that the code written is only designed to pass
and automatically run sets of test cases. the test; no further (and therefore untested) functionality
should be predicted and allowed for at any stage.
The following figure presents the TDD cycle:
2.4 Run the automated tests and see them succeed
Test
Repeat
If all test cases now pass, the programmer can be confident
(Re) Write
a test
succeeds
that the code meets all the tested requirements. This is a good
point from which to begin the final step of the cycle.
Check if the
test fails
Test(s)
2.5 Refactor code
Write fail
production
code
Now the code can be cleaned up as necessary. By re-
Run all
tests running the test cases, the developer can be confident that
refactoring is not damaging any existing functionality. The
Clean up
code 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
Figure 1 – A Graphical Representation of the TDD Cycle
production code.
2.6 Repeate whether the software meets their requirements. With ATDD,
the development team now has a specific target to satisfy, the
Starting with another new test, the cycle is then repeated to acceptance tests, which keeps them continuously focused on
push forward the functionality. The size of the steps should what the customer really wants from that user story.
always be small, with as few as 1 to 10 edits between each
test run. If new code does not rapidly satisfy a new test, or
other tests fail unexpectedly, the programmer should undo or 4. TDD A&D TRADITIO&AL TESTI&G
revert in preference to excessive debugging. Continuous
Integration helps by providing revertible checkpoints. When TDD is primarily a design technique with a side effect of
using external libraries it is important not to make increments ensuring that your source code is thoroughly unit tested.
that are so small as to be effectively merely testing the library However, there is more to testing than this. You'll still need
itself [11] unless there is some reason to believe that the to consider other agile testing techniques such as agile
library is buggy or is not sufficiently feature-complete to acceptance testing and investigative testing. Much of this
serve all the needs of the main program being written. testing can also be done early in your project. In fact, in XP
the acceptance tests for a user story are specified by the
project stakeholder(s) either before or in parallel to the code
3. TDD DEVELOPME&T STYLE being written, giving stakeholders the confidence that the
system does in fact meet their requirements.
There are various aspects to using Test Driven With traditional testing a successful test finds one or more
Development. By focusing on writing only the code necessary defects. It is the same with TDD; when a test fails you have
to pass tests, designs can be cleaner and clearer than is often made progress because you now know that you need to
achieved by other methods [10]. resolve the problem. More importantly, you have a clear
To achieve some advanced design concept (such as a measure of success when the test no longer fails. TDD
design pattern), tests are written that will generate that increases your confidence that your system actually meets the
design. The code may remain simpler than the target pattern, requirements defined for it, that your system actually works
but still pass all required tests. This can be unsettling at first and therefore you can proceed with confidence [13].
but it allows the developer to focus only on what is important. As with traditional testing, the greater the risk profile of the
Write the tests first. The tests should be written before the system the more thorough your tests need to be. With both
functionality that is being tested. This has been claimed to traditional testing and TDD you are not striving for
have two benefits. It helps ensure that the application is perfection, instead you are testing to the importance of the
written for testability, as the developers must consider how to system. In other words, you should test with a purpose and
test the application from the outset, rather than worrying know why you are testing something and to what level it
about it later. It also ensures that tests for every feature will needs to be tested. An interesting side effect of TDD is that
be written. When writing feature-first code, there is a you achieve 100% coverage test – every single line of code is
tendency by developers and the development organizations to tested – something that traditional testing doesn’t guarantee.
push the developer onto the next feature, neglecting testing In general, TDD is a specification technique, a valuable side
entirely. effect is that it results in significantly better code testing than
First fail the test cases. The idea is to ensure that the test do traditional techniques.
really works and can catch an error. Once this is shown, the
underlying functionality can be implemented. This has been
coined the test-driven development mantra, known as 5. ADVA&TAGES OF TDD
red/green/refactor where red means fail and green is pass.
Developers list many advantages to working in this fast,
Test-driven development constantly repeats the steps of incremental manner. Highest on the list of advantages to
adding test cases that fail, passing them, and refactoring. practicing TDD are:
Receiving the expected test results at each stage reinforces the
programmer's mental model of the code, boosts confidence • Quick results: Developers can see the effect of
and increases productivity. design decisions within minutes.
Advanced practices of test-driven development can lead to • Flexibility: Changes are easy because of the short
Acceptance Test-Driven Development (ATDD) where the distance between commits.
criteria specified by the customer are automated into • Automatic catalog of regression tests: If
acceptance tests, which then drive the traditional Unit Test- something developed six months ago suddenly
Driven Development (UTDD) process [12]. This process breaks under today's code, it is known immediately.
ensures the customer has an automated mechanism to decide
• Good, clean code that works early in the development cycle, preventing them from
There are also following advantages: becoming endemic and expensive problems. Eliminating
defects early in the process usually avoids lengthy and tedious
• The suite of unit tests provides constant feedback that debugging later in the project.
each component is still working.
TDD can lead to more modularized, flexible, and
• The unit tests act as documentation that cannot go out- extensible code. This effect often comes about because the
of-date, unlike separate documentation, which can and methodology requires that the developers think of the
frequently does. software in terms of small units that can be written and tested
• When the test passes and the production code is independently and integrated together later. This leads to
refactored to remove duplication, it is clear that the code is smaller, more focused classes, looser coupling, and cleaner
finished, and the developer can move on to a new test. interfaces.
• Test-driven development forces critical analysis and Because no more code is written than necessary to pass a
design because the developer cannot create the production failing test case, automated tests tend to cover every code
code without truly understanding what the desired result path. For example, in order for a TDD developer to add an
should be and how to test it. else branch off an existing if statement, the developer would
first have to write a failing test case that motivates the branch.
• The software tends to be better designed, that is, loosely As a result, the automated tests resulting from TDD tend to be
coupled and easily maintainable, because the developer is very thorough: they will detect any unexpected changes in the
free to make design decisions and refactor at any time with code's behavior. This detects problems that can arise where
confidence that the software is still working. This confidence fixing something later in the development cycle unexpectedly
is gained by running the tests. The need for a design pattern alters other functionality.
may emerge, and the code can be changed at that time.
• The test suite acts as a regression safety net on bugs: If a 6. TDD RESULTS A&ALYSIS
bug is found, the developer should create a test to reveal the
bug and then modify the production code so that the bug goes TDD programming method was applied on SW
away and all other tests still pass. On each successive test run, development project at Ericsson. The main reason for that
all previous bug fixes are verified. was the conclusion that insufficient component testing before
• Reduced debugging time. delivery to integration tests was caused by deadline pressure
that commonly occur shortly before the deliveries. During
Test-driven development offers more than just simple such time pressure, people tend to deliver the code with lower
validation of correctness, but can also drive the design of a quality assurance. To address this problem, a central part of
program. By focusing on the test cases first, one must imagine the process change was to introduce component-level test-
how the functionality will be used by clients (in the first case, driven development. The reason why TDD could make
the test cases). So, the programmer is only concerned with the developers test more efficient is that when writing the test
interface and not the implementation. This benefit is cases before the code it is more likely that the written tests are
complementary to Design by Contract as it approaches code executed before delivery. From this analysis, a proposal for a
through test cases rather than through mathematical assertions concept consisting of component-level test automation and
or preconceptions [1]. TDD was made.
The power test-driven development offers is the ability to The concept was implemented into a new product release
take small steps when required. It allows a programmer to and results were compared with its predecessors. During both
focus on the task at hand as the first goal is to make the test projects personnel turnover was very low and the
pass. Exceptional cases and error handling are not considered development processes were stable.
initially. Tests to create these extraneous circumstances are
implemented separately. Another advantage is, as already The chosen metric was Fault Slip Through i.e. measuring
mentioned, that test-driven development, when used properly, the number of faults per specific phase especially highlighting
ensures that all written code is covered by a test. This can the fault slippage from different development phases. FST
give the programming team, and subsequent users, a greater provides information regarding the number of faults not
level of confidence in the code. detected in a certain activity. These faults have instead been
detected in a later activity.
While it is true that more code is required with TDD than
without TDD because of the unit test code, total code In this particular case slippage from previous project phases
implementation time is typically shorter [14]. Large numbers to Function and System tests was measured. Test results are
of tests help to limit the number of defects in the code. The shown in the following table:
early and frequent nature of the tests helps to catch defects
Table 1: Fault Statistics – Project Comparison The main disadvantage with TDD is that in the worst case,
the test cases duplicate the amount of code to write and
Without With Improvement maintain. However, this is the same problem as for all kinds
TDD TDD of test automation. Nevertheless, to what extent the amount of
Product FST to 73% 67% 8.2% code increases depends on the granularity of the test cases
Function Test and what module level the test cases encapsulates, e.g. class
Product FST to 57% 46% 19.3% level or component level.
System Test Although gathering data for more accurate statistical
analysis will require longer period of time, initial analysis that
was performed during case study presented in this paper is
As can be seen from the table, slippage from Unit to pointing to some potential areas of improvements in product
Function test and from Function to System test was reduced. life cycle.
Lower result in first case can be explained by fact that one
feature in Unit test was not fully following the new approach
(due to the specific test environment it was tested off-site) REFERE&CES
causing the higher fault slippage than expected.
Our future goal is further enhancing the TDD practice in a [1] L. Williams, E. M. Maximilien, M. Vouk, “Test-Driven Development
as a Defect-Reduction Practice”, Proceedings of International
away to include explicit estimation of the probability that the Symposium on Software Reliability Engineering, pp 66-75, October
software system performs according to its requirements. 1996
Some productivity impact of such approach will also be [2] J. D. Musa, “Software Reliability Engineering”, New York: McGraw-
examined. Hill, 1998.
[3] B. W. Boehm, “Software Engineering Economics”, Englewood Cliffs,
NJ: Prentice-Hall, Inc., 1981.
[4] S. E. Elmaghraby, E. I. Baxter, M. A. Vouk, “An Approach to the
7. CO&CLUSIO& Modeling and Analysis of Software Production Processes”, Intl. Trans.
Operations Res, vol.2, pp. 117-135, 1995.
Test-driven development is a software development [5] M. C. Paulk, B. Curtis, M. B. Chrisis, “Capability Maturity Model for
practice that has been used sporadically for decades. With Software Version 1.1”, Software Engineering Institute CMU/SEI-93-
TR, February 1993.
this practice, test cases (preferably automated) are
[6] W. S. Humprey, “A Discipline for Software Engineering”,
incrementally written before production code is implemented. Massachusetts: Addison Wesley Longman, Inc. 1995.
The most obvious advantage of TDD is the same as for test [7] I. Sommerville, “Software Engineering”, Sixth ed. Harlow, England:
Addison-Wesley, 2001.
automation in general, i.e. the possibility to do continuous
[8] L.-O. Damm, L. Lundberg, “Results from introducing component-level
quality assurance of the code. This gives both instant test automation and Test-driven Development”, Journal of Systems and
feedback to the developers about the state of their code and Software, Volume 79, Issue 7, July 2006.
most likely, a significantly lower percentage of faults left to [9] L.-O. Damm, L. Lundberg, “Early and Cost-Effective Software Fault
be found in later testing and at customer sites. Further, with Detection”, International Conference on Software Engineering,
Proceedings of the 29th international conference on Software
early quality assurance, a common problem with test Engineering, pp. 560–570.
automation is avoided. That is, when an organization [10] K. Beck, “Test-Driven Development by Example”, Addison Wesley,
introduces automated testing late in the development cycle, it 2003
becomes a catch for all faults just before delivery to the [11] JW. Newkirk and AA. Vorontsov , “Test-Driven Development in
customer. The corrections of found faults lead to a spiral of Microsoft. NET, Microsoft Press, 2004.
testing and re-testing which delays the delivery of the [12] L. Koskela, “Test Driven: TDD and Acceptance TDD for Java
Developers”, Manning Publications, 2007
product.
[13] ***, “Techniques for successful Evolutionary/Agile Database
A side effect of TDD is that the resulting tests are working Development”, http://www.agiledata.org
examples for invoking the code, thereby providing a working [14] M. Muller, F. Padberg, “About the Return on Investment of Test-
specification for the code. Driven Development”, Universitat Kalsruhe, pp. 6. http://www.
ipd.uka.de