Test-Driven
Development
        Matteo Vaccari
 m.vaccari@sourcesense.com
http://matteo.vaccari.name/blog

   BIt Systems,...
Three kinds of test

• Unit test
• Integration test
• Customer test
Unit tests
• Focus on a class, method or function
• Run entirely in memory
• Extremely fast!
Unit tests run fast.
A test is not a unit test if:
   1. It talks to a database
   2. It communicates across the network
 ...
Integration tests
• Unit tests prove our logic is correct
• Integration test prove our program
  communicates correctly wi...
End-to-end tests
                   Billing




          System
    GUI    under
            test



                    DB
End-to-end tests
                                        Billing




                            System
               GUI...
Focused integration tests
                                        Billing




                            System
         ...
Customer tests
• Also known as “Acceptance tests”
• Motto: We implement tricky domain concepts
  correctly




           ...
Customer tests
• Customer test are
  customer-provided
  examples
• Often captured in
  table form
• Expressed in the
  la...
Test-Driven
Development
Clean code that works.
Clean code that works
• is out of reach of even the best
  programmers, some of the time,
• and out of reach of most progr...
Simple design

The code is simple enough when it:
  0. Runs all the tests
  1. Expresses every idea that we need to expres...
Clean code that works


• First we'll solve the “that works” part
• Then we'll solve the “clean code” part
Write a test
public class AdderTest {
  @Test
  public void testTwoPlusThree() {
    Adder a = new Adder();
    assertEqua...
Now it compiles
public class AdderTest {
  @Test
  public void testTwoPlusThree() {
    Adder a = new Adder();
    assertE...
Red bar!
public class AdderTest {
  @Test
  public void testTwoPlusThree() {
    Adder a = new Adder();
    assertEquals(5...
Just pretend
public class AdderTest {
  @Test
  public void testTwoPlusThree() {
    Adder a = new Adder();
    assertEqua...
Remove the duplicated “5”
   public class AdderTest {
     @Test
     public void testTwoPlusThree() {
       Adder a = ne...
The procedure

1. Write a test

2. Make it compile
                  Expected 5, was 0

3. Make it pass

4. Remove duplica...
Red

Refactor             Green




   Repeat every 2-10 min.

                            m.vaccari@sourcesense.com
Clean code, why?

• Design is the great accelerator:
• If you drop quality for speed, you will get neither
• If you aim fo...
Test first, why?

• You think code from the point of view of
  the caller
• This perspective makes for better design
• Test...
Refactor, why?
• Because I can: the tests support refactoring
• Refactoring is when I do design
• I don’t claim I can gues...
The Bowling Score
           By Robert Martin “Uncle Bob”




http://butunclebob.com/ArticleS.UncleBob.TheBowlingGameKata
...
Il punteggio del bowling
  Ci sono 10 frame. In ogni frame il giocatore ha due possibilità di abbattere 10
     birilli (p...
The requirements

• Write class “Game” with two methods:
 • void roll(int pins); call when the player rolls
      a ball. ...
Let’s think about design?
•   A quick object-oriented analysis leads us to
    think we need
    •     class Game
    •   ...
Let’s think about design?
•   A quick object-oriented analysis leads us to
    think we need
    •   class Game
    •   cl...
Demo time


• ...eclipse!



                        m.vaccari@sourcesense.com
Ancora, perché test first?
 •   per concentrarsi su quello che serve veramente (no gold
     plating)

 •   good enough! qu...
Ancora, perché refactoring?

  • simplicity is key
  • il design nel tempo si imbastardisce
  • fare il design prima signi...
Do the simplest thing
that can possibly work


                m.vaccari@sourcesense.com
Do the simplest thing

1. Build the quickest code that will pass the
   tests
2. Refactor the code to have the simplest
  ...
What is simple design?
•   The code passes all tests

•   There is no duplication

•   The code expresses the programmer’s...
TDD is a key practice

• Defects kill predictability
    no predictability, no planning!
• Test-driven is predictable
• Ha...
No silver bullet

• Needs lots of practice
• Requires discipline
• Must think and be alert at all times!


               ...
Debugging
  Sucks

            Testing
             Rocks
Supermarket checkout

• Compute the
  total price
• Scan items one
  at a time
• In any order

     Source: Dave Thomas, h...
Back to the supermarket
Test-Driven Development
Test-Driven Development
Upcoming SlideShare
Loading in...5
×

Test-Driven Development

858

Published on

Published in: Business, Technology
0 Comments
5 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
858
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
37
Comments
0
Likes
5
Embeds 0
No embeds

No notes for slide

Test-Driven Development

  1. 1. Test-Driven Development Matteo Vaccari m.vaccari@sourcesense.com http://matteo.vaccari.name/blog BIt Systems, 4 June 2008
  2. 2. Three kinds of test • Unit test • Integration test • Customer test
  3. 3. Unit tests • Focus on a class, method or function • Run entirely in memory • Extremely fast!
  4. 4. Unit tests run fast. A test is not a unit test if: 1. It talks to a database 2. It communicates across the network 3. It touches the file system 4. You have to do things to your environment to run it (eg, change config files) Tests that do this are integration tests Michael Feathers
  5. 5. Integration tests • Unit tests prove our logic is correct • Integration test prove our program communicates correctly with the outside world
  6. 6. End-to-end tests Billing System GUI under test DB
  7. 7. End-to-end tests Billing System GUI under test DB • ...are the least convenient kind of integration tests
  8. 8. Focused integration tests Billing System GUI under test DB • Each test deals with one external interaction
  9. 9. Customer tests • Also known as “Acceptance tests” • Motto: We implement tricky domain concepts correctly Example from Mugridge & Cunningham Fit For Developing Software
  10. 10. Customer tests • Customer test are customer-provided examples • Often captured in table form • Expressed in the language of the business • Automated Example from Mugridge & Cunningham Fit For Developing Software
  11. 11. Test-Driven Development
  12. 12. Clean code that works.
  13. 13. Clean code that works • is out of reach of even the best programmers, some of the time, • and out of reach of most programmers (like me) most of the time -- Kent Beck
  14. 14. Simple design The code is simple enough when it: 0. Runs all the tests 1. Expresses every idea that we need to express 2. Contains no duplication 3. Has the minimum number of classes and functions (In this order) Adapted from Extreme Programming Installed by Ron Jeffries et al.
  15. 15. Clean code that works • First we'll solve the “that works” part • Then we'll solve the “clean code” part
  16. 16. Write a test public class AdderTest { @Test public void testTwoPlusThree() { Adder a = new Adder(); assertEquals(5, a.add(2, 3)); } } m.vaccari@sourcesense.com
  17. 17. Now it compiles public class AdderTest { @Test public void testTwoPlusThree() { Adder a = new Adder(); assertEquals(5, a.add(2, 3)); } } public class Adder { public int add(int a, int b) { return 0; } } m.vaccari@sourcesense.com
  18. 18. Red bar! public class AdderTest { @Test public void testTwoPlusThree() { Adder a = new Adder(); assertEquals(5, a.add(2, 3)); } } public class Adder { public int add(int a, int b) { return 0; } } Expected 5, was 0 m.vaccari@sourcesense.com
  19. 19. Just pretend public class AdderTest { @Test public void testTwoPlusThree() { Adder a = new Adder(); assertEquals(5, a.add(2, 3)); } } public class Adder { public int add(int a, int b) { return 5; } } m.vaccari@sourcesense.com
  20. 20. Remove the duplicated “5” public class AdderTest { @Test public void testTwoPlusThree() { Adder a = new Adder(); assertEquals(5, a.add(2, 3)); } } public class Adder { public int add(int a, int b) { return a+b; } } m.vaccari@sourcesense.com
  21. 21. The procedure 1. Write a test 2. Make it compile Expected 5, was 0 3. Make it pass 4. Remove duplication m.vaccari@sourcesense.com
  22. 22. Red Refactor Green Repeat every 2-10 min. m.vaccari@sourcesense.com
  23. 23. Clean code, why? • Design is the great accelerator: • If you drop quality for speed, you will get neither • If you aim for quality... • ... and you know how to get it... • ... you will also be fast! m.vaccari@sourcesense.com
  24. 24. Test first, why? • You think code from the point of view of the caller • This perspective makes for better design • Test coverage is a useful byproduct m.vaccari@sourcesense.com
  25. 25. Refactor, why? • Because I can: the tests support refactoring • Refactoring is when I do design • I don’t claim I can guess the right design at first • Design emerges, with thought, care and small steps m.vaccari@sourcesense.com
  26. 26. The Bowling Score By Robert Martin “Uncle Bob” http://butunclebob.com/ArticleS.UncleBob.TheBowlingGameKata m.vaccari@sourcesense.com
  27. 27. Il punteggio del bowling Ci sono 10 frame. In ogni frame il giocatore ha due possibilità di abbattere 10 birilli (pins). Il punteggio per il frame è il numero di birilli abbattuti, più i bonus per stike o spare. Uno spare è quando il giocatore abbatte 10 birilli in due tiri. Il bonus per quel frame è il numero di birilli abbattuti al tiro successivo. Nel frame 3 dell'esempio, il punteggio è 10 (i birilli abbattuti) più il bonus di 5 (abbattuti nel tiro successivo.) Uno strike è quando il giocatore abbatte tutti i birilli in un solo tiro. Il bonus per quel frame è il numero di birilli abbattuti nei due tiri successivi Nel decimo frame, se il giocatore fa uno strike o spare può fare i tiri necessari per completare il frame. In ogni caso, al decimo frame non vengono fatti più di tre tiri. m.vaccari@sourcesense.com
  28. 28. The requirements • Write class “Game” with two methods: • void roll(int pins); call when the player rolls a ball. The argument is the number of pins knocked down. • int score(); called when the game is ended. Returns the final score. m.vaccari@sourcesense.com
  29. 29. Let’s think about design? • A quick object-oriented analysis leads us to think we need • class Game • class Frame • class Roll • class TenthFrame extending Frame • ... m.vaccari@sourcesense.com
  30. 30. Let’s think about design? • A quick object-oriented analysis leads us to think we need • class Game • class Frame • class Roll • class TenthFrame extending Frame • ... forget about all that! m.vaccari@sourcesense.com
  31. 31. Demo time • ...eclipse! m.vaccari@sourcesense.com
  32. 32. Ancora, perché test first? • per concentrarsi su quello che serve veramente (no gold plating) • good enough! quando il test passa so che posso fermarmi • perché penso al codice come un cliente di questo codice • perché ottengo codice testabile, e il codice testabile • ha uno scopo preciso • è disaccoppiato dal resto del sistema • è più generale • il design emerge mano a mano che capisco meglio il problema m.vaccari@sourcesense.com
  33. 33. Ancora, perché refactoring? • simplicity is key • il design nel tempo si imbastardisce • fare il design prima significa farlo nel momento peggiore: quando ne so di meno • molto meglio fare design mentre sviluppo m.vaccari@sourcesense.com
  34. 34. Do the simplest thing that can possibly work m.vaccari@sourcesense.com
  35. 35. Do the simplest thing 1. Build the quickest code that will pass the tests 2. Refactor the code to have the simplest design possible m.vaccari@sourcesense.com
  36. 36. What is simple design? • The code passes all tests • There is no duplication • The code expresses the programmer’s intention • Using the smallest number of classes and methods In this order m.vaccari@sourcesense.com
  37. 37. TDD is a key practice • Defects kill predictability no predictability, no planning! • Test-driven is predictable • Hardly ever use the debugger m.vaccari@sourcesense.com
  38. 38. No silver bullet • Needs lots of practice • Requires discipline • Must think and be alert at all times! m.vaccari@sourcesense.com
  39. 39. Debugging Sucks Testing Rocks
  40. 40. Supermarket checkout • Compute the total price • Scan items one at a time • In any order Source: Dave Thomas, http://codekata.pragprog.com/2007/01/kata_nine_back_.html
  41. 41. Back to the supermarket
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×