Test Driven Development - (TDD)

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 21

Test-Driven Development

(TDD)
Author: Priyank Gairola

TDD A Snippet
Test-driven development is an evolutionary
approach to development which combines
test-first development where you write a
test before you write just enough
production code to fulfill that test and
refactoring.
Some say its a programming technique.
Precisely it is a technique whereby you
write your test cases before you write any
implementation code.
Tests drive or dictate the code that is
developed.
Test Driven Development
2
TDD is risk averse programming,

TDD Goals?
It gives you a chance to learn the lessons the code
has to teach you. If you only hack together the first
thing you can think of, you won't have the chance to
think of a second, better thing.
It improves the lives of the users of your software.
Keep in mind that if you don't test your software, your
users will..
It is a predictable way to develop. You know when you
are finished without worries about a long bug trail.
It lets your teammates count on you, and you on
them.
It just feels good to see the green bar (i.e. all tests
passing).
Test Driven Development

Stages in TDD?
Write a single test
Compile it. It shouldnt compile because
youve not written the implementation
code
Implement just enough code to get the
test to compile
Run the test and see it fail
Implement just enough code to get the
test to pass
Run the test and see it pass
Re-factor for clarity and once and only
once
Test Driven Development

Stages in TDD?
Write a test
Refactor code
(and test)

Compile

Run test,
watch it pass

Fix compile errors

Write code

Run test,
watch it fail

Test Driven Development

TDD Life Cycle


Write
Test
Refacto
r As
Needed

Compil
e

Run &
See the
Fail
Test Driven Development

Example
We want to develop a method that, given
two Integers, returns an Integer that is the
sum of parameters.

Test Driven Development

Example (cont.)
Test
Integer i = new
Integer(5);
Integer j = new
Integer(2);
Object o = sum(i, j);

Method

Test Driven Development

Example (cont.)
Test
Integer i = new
Integer(5);
Integer j = new
Integer(2);
Object o = sum(i,
j);

Method
public Object sum(Integer
i, Integer j) {
return new Object();
}

Test Driven Development

Example (cont.)
Test
Integer i = new
Method
Integer(5);
public Object
Integer j = new
sum(Integer i, Integer
Integer(2);
j) {
Object o = sum(i,
return new Object();
j);
}
if (o instanceof
Integer)
return true;
else
Test Driven Development
10
return false;

Example (cont.)
Test
Integer i = new
Method
Integer(5);
public Integer
Integer j = new
sum(Integer i, Integer j)
Integer(2);
Object o = sum(i, j); {
return new Integer();
if (o instanceof
}
Integer)
return true;
else
return false;
Test Driven Development

11

Example (cont.)
Test
Integer i = new
Integer(5);
Method
Integer j = new
public Integer
Integer(2);
sum(Integer i, Integer j)
Object o = sum(i,
{
j);
return new Integer();
if ((o instanceof
}
Integer)
&& ((new
Integer(7))
.equals(o))
return true;
Test Driven Development
12

Example (cont.)
Test
Integer i = new
Method
Integer(5);
public Integer
Integer j = new
sum(Integer i, Integer j)
Integer(2);
{
Object o = sum(i, j); return new Integer (
if ((o instanceof
i.intValue() +
Integer)
j.intValue());
&& ((new
}
Integer(7))
.equals(o))
return true;
Test Driven Development
13
else

Traditional Vs. Test Driven


Approach?

Test Driven Development

14

Traditional Vs. Test Driven


Approach?
S.
No

Traditional Approach

Test Driven Approach

Pick a feature or a user


requirement

Pick a feature or a user


requirement

Write the production code


that implements the feature
or user requirement.

Write a test that fulfills a


small task or piece of the
feature or user
requirement (e.g. one
method) and have the test
fail.

Write the tests to validate


the feature or user
requirement.

Write the production code


that implements the task
and will pass the test.

Run all the tests.

Run all of the tests.

Refactor if necessary

Refactor the production


and test code to make
them as simple as
Test Driven Development
15
possible, ensuring all tests

Benefits of TDD?

Test Driven Development contributes to software


development practice from many aspects such as
requirements definition, writing clean and well
designed code, and change and configuration
management. Few other benefits can be
summarized as:
Simpler Development Process -: Developers
who use TDD are more focused. The only thing
that a TDD developer has to worry about is
getting the next test to pass. The goal is focusing
the attention on a small piece of the software,
getting it to work, and moving on rather than
trying to create the software by doing a lot of upfront design.
Improved Communication
-: Communicating
Test Driven Development
16

Benefits
of
TDD?
Improved
Understanding
of

Required
Software
Behaviour
-:
The
level
of
requirements on a project varies greatly.
Sometimes requirements are very detailed and
other times they are vague. Writing unit tests
before writing the code helps developers focus on
understanding the required behaviour of the
software. Each of these pass/fail criteria adds to
the knowledge of how the software must behave.
As more unit tests are added because of new
features or new bugs, the set of unit tests come
to represent a set of required behaviours of
higher and higher fidelity.
Reduced Design Complexity -: Developers try
to be forward looking and build flexibility into
Driven Development
17
software so that itTestcan
adapt to the ever-changing

Advantages of TDD?
TDD shortens the programming feedback
loop
TDD promotes the development of highquality code
User requirements more easily understood
Reduced interface misunderstandings
TDD provides concrete evidence that your
software works

Reduced software defect rates


Better Code
Less Debug Time

Test Driven Development

18

Disadvantages of TDD?

Programmers like to code, not to test


Test writing is time consuming
Test completeness is difficult to judge
TDD may not always work

Test Driven Development

19

Conclusion

TDD is a design technique that needs a


little bit of extra time for planning ahead. It
doesnt necessarily improve the clients
experience. If they are more interested in
development time rather than the quality
of code, then they might not appreciate
TDD designed code itself. TDD relies on the
inherent human nature to fix things.
Writing tests are important but not
essential. We might forget them. TDD will
actually show a broken status if any test
fails, this is a great incentive to fix tests.
People tend to
make a lot of design
Test Driven Development
20
decisions while writing test cases. This is a

Test Driven Development

21

You might also like