Test Driven Development
Clean Code that works!!!
Agenda
2
Intro to TDD
TDD by example
Conclusions
Intro
3
What is TDD
Developed (or rediscovered) by Kent Beck to encourage
simple design and inspire confidence.
Related to the test-first programming concepts of eXtrem
Programming
A software development process that relies on the
repetition of a very short development cicle
4
The golden rules
Never write model code
unless you have a test that
requires it
Write new code only if an
automated test has failed
Eliminate duplication
5
What's my motivation?
TDD<piè di
pagina><piè di
pagina><piè di 6
COURAGE
Clean Code that works!!!
Managing fear
“I have a problem and I
can't see the end from
the beginning”
Fear says you to be
carefull
7
TDD mantra
8
Benefits
9
Programmers who wrote more tests tended to be more productive
Programmers using TDD rarely felt the need to invoke a debugger
TDD offers validation of correctness and drives the design of a program
TDD offers the ability to take small steps
Even if more code is required, the total code implementation time could be
shorter
TDD can lead to more modularized, flexible and extensible code
Because no more code is written than necessary to pass a failing test case, tests
tend to cover every code path.
TDD by example
10
Multi Currency Money
11
Bond portfolio management system.
Company Shares Price Total
Pippo SpA 1000 25 25000
Pluto srl 400 100 40000
Total 65000
Multy currency report
Company Shar
es
Price Total
Pippo SpA 1000 25 EUR 25000 EUR
Gastone SaS 400 100 USD 40000 USD
Total 65000 EUR
Rates
From To Rate
USD EUR 2
12
Conclusions
13
TDD Best Practices I
Work only on one test at a time
14
TDD Best Practices II
Write test assertions
self-explanatory
15
TDD Best Practices III
Same structure for tests and
source code
16
TDD Best Practices IV
Keep tests small
17
TDD Best Practices V.I
integrate your code every few unit test and never
leave broken tests
18
If you are working in a team...
TDD Best Practices V.II
end every day with a broken test
19
If you are working alone...
Summary
TDD Mantra
TDD rhythm
Value Object pattern
Information Hiding principle
DRY principle
20
Bibliography and references
Kent Beck – Test-Driven Development by Example, The Addison-Wesley Signature Series
Jason Gorma – Agile Java Development: Test-Driven Development using JUnit and Eclipse
WardsWiKi - http://c2.com/cgi/wiki?TestDrivenDevelopment
WardsWiKi - http://c2.com/cgi/wiki?DontRepeatYourself
www.testdriven.com
www.junit.com
www.bigthink.it/php/test-driven-development-prima-i-test-poi-il-codice
www.agiledata.org/essays/tdd.html
21
Thank you for your attention!!!
22

Test Driven Development

  • 1.
  • 2.
    Agenda 2 Intro to TDD TDDby example Conclusions
  • 3.
  • 4.
    What is TDD Developed(or rediscovered) by Kent Beck to encourage simple design and inspire confidence. Related to the test-first programming concepts of eXtrem Programming A software development process that relies on the repetition of a very short development cicle 4
  • 5.
    The golden rules Neverwrite model code unless you have a test that requires it Write new code only if an automated test has failed Eliminate duplication 5
  • 6.
    What's my motivation? TDD<pièdi pagina><piè di pagina><piè di 6 COURAGE Clean Code that works!!!
  • 7.
    Managing fear “I havea problem and I can't see the end from the beginning” Fear says you to be carefull 7
  • 8.
  • 9.
    Benefits 9 Programmers who wrotemore tests tended to be more productive Programmers using TDD rarely felt the need to invoke a debugger TDD offers validation of correctness and drives the design of a program TDD offers the ability to take small steps Even if more code is required, the total code implementation time could be shorter TDD can lead to more modularized, flexible and extensible code Because no more code is written than necessary to pass a failing test case, tests tend to cover every code path.
  • 10.
  • 11.
    Multi Currency Money 11 Bondportfolio management system. Company Shares Price Total Pippo SpA 1000 25 25000 Pluto srl 400 100 40000 Total 65000 Multy currency report Company Shar es Price Total Pippo SpA 1000 25 EUR 25000 EUR Gastone SaS 400 100 USD 40000 USD Total 65000 EUR Rates From To Rate USD EUR 2
  • 12.
  • 13.
  • 14.
    TDD Best PracticesI Work only on one test at a time 14
  • 15.
    TDD Best PracticesII Write test assertions self-explanatory 15
  • 16.
    TDD Best PracticesIII Same structure for tests and source code 16
  • 17.
    TDD Best PracticesIV Keep tests small 17
  • 18.
    TDD Best PracticesV.I integrate your code every few unit test and never leave broken tests 18 If you are working in a team...
  • 19.
    TDD Best PracticesV.II end every day with a broken test 19 If you are working alone...
  • 20.
    Summary TDD Mantra TDD rhythm ValueObject pattern Information Hiding principle DRY principle 20
  • 21.
    Bibliography and references KentBeck – Test-Driven Development by Example, The Addison-Wesley Signature Series Jason Gorma – Agile Java Development: Test-Driven Development using JUnit and Eclipse WardsWiKi - http://c2.com/cgi/wiki?TestDrivenDevelopment WardsWiKi - http://c2.com/cgi/wiki?DontRepeatYourself www.testdriven.com www.junit.com www.bigthink.it/php/test-driven-development-prima-i-test-poi-il-codice www.agiledata.org/essays/tdd.html 21
  • 22.
    Thank you foryour attention!!! 22