Test Driven Development
Test Driven Development
Test Driven Development
DEVELOPMENT
1 Presented By:
Nitin Garg
07030244008
MBA(SDM)
TEST-DRIVEN
DEVELOPMENT
2
ORIGIN
Test-Driven Development is a core part of the
agile process formalized by Kent Beck called
eXtreme Programming (XP).
4
COST OF DEVELOPMENT
C
o
s
t
Time Traditional
TDD
5
COST OF FIXING FAULTS
6
WHAT IS TDD?
TDD is a technique whereby you write your test cases
before you write any implementation code
Forces developers to think in terms of implementer and
user
An indication of intent
Tests provide a specification of what a piece of code
actually does it goes some way to defining an interface
Some might argue that tests are part of the
documentation 7
Could your customers/clients write tests?
WHAT IS TDD?
Before you write code, think about what it will do.
Write a test that will use the methods you havent even
written yet.
8
WHAT CAN BE TESTED?
Valid Input
In-valid Input
Exceptions
Boundary Conditions
10
METHODOLOGY
Test first Code last
You may not write production code unless youve first
written a failing unit test
Test more Code more
You may not write more of a unit test than is
sufficient to fail
Test again Code again
You may not write more production code than is
sufficient to make the failing unit test pass
11
TDD STAGES
Write a test
Refactor code
Compile
(and test)
Run test,
Fix compile errors
watch it pass
Run test,
Write code
watch it fail
12
TDD STAGES
The Extreme Programming Explored , Bill Wake describes
the test cycle:
Write Test
Compile
Refactor As
Needed
15
WHY DOES TDD WORK?
Encourages divide-and-conquer
Programmers are never scared to make a change
that might break the system
The testing time that is often squeezed out of the
16
ADVANTAGES OF TDD
TDD shortens the programming feedback loop
TDD promotes the development of high-quality
code
User requirements more easily understood
Better Code
18
EXAMPLE
We want to develop a method that, given two
Integers, returns an Integer that is the sum of
parameters.
19
EXAMPLE (CONT.)
Test Method
Integeri=
newInteger(5);
Integerj=
newInterger(2);
Objecto=sum(i,j);
20
EXAMPLE (CONT.)
Test Method
Integeri= publicstaticObject
newInteger(5); sum(Integeri,
Integerj= Integerj){
newInterger(2); returnnew
Object();
Objecto=sum(i,j);
}
21
EXAMPLE (CONT.)
Test Method
Integeri= publicstaticObject
newInteger(5); sum(Integeri,
Integerj= Integerj){
newInterger(2); returnnew
Objecto=sum(i,j); Object();
if(oinstanceof }
Integer)
returntrue;
else
22
returnfalse;
EXAMPLE (CONT.)
Test Method
Integeri= publicstaticInteger
newInteger(5); sum(Integeri,
Integerj= Integerj){
newInterger(2); returnnew
Objecto=sum(i,j); Integer();
if(oinstanceof }
Integer)
returntrue;
else
23
returnfalse;
EXAMPLE (CONT.)
Test Method
Integeri= publicstaticInteger
newInteger(5); sum(Integeri,
Integerj= Integerj){
newInterger(2); returnnew
Integer();
Objecto=sum(i,j);
}
if((oinstanceof
Integer)&&
((newInteger(7))
.equals(o))
returntrue;
else 24
returnfalse;
EXAMPLE (CONT.)
Test Method
Integeri= publicstaticInteger
newInteger(5); sum(Integeri,
Integerj= Integerj){
newInterger(2); returnnew
Objecto=sum(i,j); Integer(
if((oinstanceof i.intValue()+
Integer)&& j.intValue());
((newInteger(7)) }
.equals(o))
returntrue;
else
25
returnfalse;
OTHER TECHNIQUES OF TDD
26
TECHNIQUE 1
Identify a smallest possible change to be made
Implement test and (the one line of) code for that
change (see previous slide)
Run all tests
27
TECHNIQUE 2
Test and implement a low-level function
(using previous Techniques)
Test and implement a higher-level
function that invokes the lower-level
function
Test all the logic in the higher-level
function as expected; use as many tests as
necessary
Include one test that convinces you that
the higher-level function called the lower-
level one 28
TECHNIQUE 3
Build higher- and higher-level tests
Build tests that represent user actions such as
entering a piece of data and hitting OK
Build tests that string together a series of user
actions that represent Acceptance Test cases
Demonstrate the Acceptance Tests to the user(s)
regularly
29
CONCLUSION
More code has to be written using TDD but that isnt
the bottleneck in Software Development
Techniques have to be learned by developers and
enforced by managers
User Interface testing is the hardest
30