TEST DRIVEN DEVELOPMENT = TEST FIRST DEVELOPMENT + REFACTORING
Test driven development
(TDD) is a software development process, in which developer write test first
then start development in small incremental manner to pass this test and
refactoring the code whenever required.
Now days customer expect
from their software developer to write code with almost zero bug without
compromising deadlines. To achieve this TDD is one of the software development processes
which help in bug reduction, since this process start with testing and advocate
testing after any refactoring happens which make sure that software work at
least in tested scenarios if used correctly. TDD can change overall design of
code for better if it is used effectively.
Usually developers develop
the code first then write test cases. But in TDD process developer write a unit
test then run that test and that test fail since developer didn’t written
anything yet, now developer write code to pass that test and developer do
required refactoring whenever required.
There are two type of TDD:
- Developer TDD: Process is same as TDD.
- Acceptance TDD / Behavior Driven
Development (BDD): In BDD acceptance test are written then coding to pass that
acceptance test.
How to implement TDD?
Think
how new code will be implemented or refactoring of existing code happen.
Example: - Fibonacci number series 0,1,1,2,3,5,8…
Write
just enough stub code, so developer can compile.
Example:-
public int
fibonacci(int x) {
return 0;
}
Add a
test to for stub code.
Example:-
public void
testFibonacci_whenX_isOne() {
assertEquals(0,
Fibonacci(1));
}
Write a
code to pass the test.
Example:-
public int fibonacci(int x) {
if(x == 0) return 0;
if(x == 1) return 1;
return 0;
}
Again
run the test.
Add more tests for
incremental development and refactoring code whenever possible.
Note: - In pair
programming TDD is very effective. Since pair developer helps each other to
keep in track.
TDD and Legacy code
The challenge is working
with legacy code which didn’t have any test case. In order to introduce test
into legacy code developer have to isolate the unit and refactoring the code,
so that it can be tested.
Advantage of TDD
- TDD acts as a constant feedback that
each unit is still working.
- TDD helps to isolate the problems in
code.
- After the test pass and developer refactoring
the code to remove duplicate code then again this test ensue development
complete.
- Since TDD required very detail level
of requirement. It forces developer to understand the requirement well before
actual coding.
- The test suite acts as a regression
safety net on bugs: If a bug is found, the developer should create a test to
reveal the bug and then modify the production code so that the bug goes away
and all other tests still pass. On each successive test run, all previous bug
fixes are verified.
Disadvantage of TDD
- TDD didn’t test with other resources like
database, JMS etc.
- If TDD is not used carefully, it can add
cost and complexity to the project.
- TDD is highly reliant on refactoring and
programming skill.
- It is difficult to detect deeper faults in
code.
Even after enormous test
case can create problem just like when you add this test into build for
continuous integration it might take too long to complete. If developer use TDD
properly, then code produced from this process is testable, well designed &
developers find that they will be spending less time in debugging. But still
developer make mistakes but it can be catch too early.