test-driven development
TRANSCRIPT
![Page 1: Test-Driven Development](https://reader034.vdocuments.mx/reader034/viewer/2022052618/5549fc76b4c905557a8b4be3/html5/thumbnails/1.jpg)
Matteo [email protected]
http://matteo.vaccari.name/blog
BIt Systems, 4 June 2008
Test-Driven Development
![Page 2: Test-Driven Development](https://reader034.vdocuments.mx/reader034/viewer/2022052618/5549fc76b4c905557a8b4be3/html5/thumbnails/2.jpg)
Three kinds of test
• Unit test
• Integration test
• Customer test
![Page 3: Test-Driven Development](https://reader034.vdocuments.mx/reader034/viewer/2022052618/5549fc76b4c905557a8b4be3/html5/thumbnails/3.jpg)
Unit tests• Focus on a class, method or function
• Run entirely in memory
• Extremely fast!
![Page 4: Test-Driven Development](https://reader034.vdocuments.mx/reader034/viewer/2022052618/5549fc76b4c905557a8b4be3/html5/thumbnails/4.jpg)
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
![Page 5: Test-Driven Development](https://reader034.vdocuments.mx/reader034/viewer/2022052618/5549fc76b4c905557a8b4be3/html5/thumbnails/5.jpg)
Integration tests
• Unit tests prove our logic is correct
• Integration test prove our program communicates correctly with the outside world
![Page 6: Test-Driven Development](https://reader034.vdocuments.mx/reader034/viewer/2022052618/5549fc76b4c905557a8b4be3/html5/thumbnails/6.jpg)
System under test
DB
Billing
GUI
End-to-end tests
![Page 7: Test-Driven Development](https://reader034.vdocuments.mx/reader034/viewer/2022052618/5549fc76b4c905557a8b4be3/html5/thumbnails/7.jpg)
System under test
DB
Billing
GUI
End-to-end tests
• ...are the least convenient kind of integration tests
![Page 8: Test-Driven Development](https://reader034.vdocuments.mx/reader034/viewer/2022052618/5549fc76b4c905557a8b4be3/html5/thumbnails/8.jpg)
System under test
DB
Billing
GUI
Focused integration tests
• Each test deals with one external interaction
![Page 9: Test-Driven Development](https://reader034.vdocuments.mx/reader034/viewer/2022052618/5549fc76b4c905557a8b4be3/html5/thumbnails/9.jpg)
Customer tests• Also known as “Acceptance tests”
• Motto: We implement tricky domain concepts correctly
Example from Mugridge & Cunningham Fit For Developing Software
![Page 10: Test-Driven Development](https://reader034.vdocuments.mx/reader034/viewer/2022052618/5549fc76b4c905557a8b4be3/html5/thumbnails/10.jpg)
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
![Page 11: Test-Driven Development](https://reader034.vdocuments.mx/reader034/viewer/2022052618/5549fc76b4c905557a8b4be3/html5/thumbnails/11.jpg)
![Page 12: Test-Driven Development](https://reader034.vdocuments.mx/reader034/viewer/2022052618/5549fc76b4c905557a8b4be3/html5/thumbnails/12.jpg)
Test-Driven Development
![Page 13: Test-Driven Development](https://reader034.vdocuments.mx/reader034/viewer/2022052618/5549fc76b4c905557a8b4be3/html5/thumbnails/13.jpg)
Clean code that works.
![Page 14: Test-Driven Development](https://reader034.vdocuments.mx/reader034/viewer/2022052618/5549fc76b4c905557a8b4be3/html5/thumbnails/14.jpg)
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
![Page 15: Test-Driven Development](https://reader034.vdocuments.mx/reader034/viewer/2022052618/5549fc76b4c905557a8b4be3/html5/thumbnails/15.jpg)
Simple design
The code is simple enough when it:0. Runs all the tests1. Expresses every idea that we need to express2. Contains no duplication3. Has the minimum number of classes and functions
(In this order)
Adapted from Extreme Programming Installed by Ron Jeffries et al.
![Page 16: Test-Driven Development](https://reader034.vdocuments.mx/reader034/viewer/2022052618/5549fc76b4c905557a8b4be3/html5/thumbnails/16.jpg)
Clean code that works
• First we'll solve the “that works” part
• Then we'll solve the “clean code” part
![Page 17: Test-Driven Development](https://reader034.vdocuments.mx/reader034/viewer/2022052618/5549fc76b4c905557a8b4be3/html5/thumbnails/17.jpg)
Write a test
public class AdderTest { @Test
public void testTwoPlusThree() {Adder a = new Adder();assertEquals(5, a.add(2, 3));
}}
![Page 18: Test-Driven Development](https://reader034.vdocuments.mx/reader034/viewer/2022052618/5549fc76b4c905557a8b4be3/html5/thumbnails/18.jpg)
Now it compilespublic 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; }}
![Page 19: Test-Driven Development](https://reader034.vdocuments.mx/reader034/viewer/2022052618/5549fc76b4c905557a8b4be3/html5/thumbnails/19.jpg)
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
![Page 20: Test-Driven Development](https://reader034.vdocuments.mx/reader034/viewer/2022052618/5549fc76b4c905557a8b4be3/html5/thumbnails/20.jpg)
Just pretendpublic 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; }}
![Page 21: Test-Driven Development](https://reader034.vdocuments.mx/reader034/viewer/2022052618/5549fc76b4c905557a8b4be3/html5/thumbnails/21.jpg)
Remove the duplicated “5”public class AdderTest {
@Testpublic 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; }}
![Page 22: Test-Driven Development](https://reader034.vdocuments.mx/reader034/viewer/2022052618/5549fc76b4c905557a8b4be3/html5/thumbnails/22.jpg)
The procedure
1. Write a test
2. Make it compile
3. Make it pass
4. Remove duplication
Expected 5, was 0
![Page 24: Test-Driven Development](https://reader034.vdocuments.mx/reader034/viewer/2022052618/5549fc76b4c905557a8b4be3/html5/thumbnails/24.jpg)
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!
![Page 25: Test-Driven Development](https://reader034.vdocuments.mx/reader034/viewer/2022052618/5549fc76b4c905557a8b4be3/html5/thumbnails/25.jpg)
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
![Page 26: Test-Driven Development](https://reader034.vdocuments.mx/reader034/viewer/2022052618/5549fc76b4c905557a8b4be3/html5/thumbnails/26.jpg)
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
![Page 27: Test-Driven Development](https://reader034.vdocuments.mx/reader034/viewer/2022052618/5549fc76b4c905557a8b4be3/html5/thumbnails/27.jpg)
The Bowling ScoreBy Robert Martin “Uncle Bob”
http://butunclebob.com/ArticleS.UncleBob.TheBowlingGameKata
![Page 28: Test-Driven Development](https://reader034.vdocuments.mx/reader034/viewer/2022052618/5549fc76b4c905557a8b4be3/html5/thumbnails/28.jpg)
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.
![Page 29: Test-Driven Development](https://reader034.vdocuments.mx/reader034/viewer/2022052618/5549fc76b4c905557a8b4be3/html5/thumbnails/29.jpg)
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.
![Page 30: Test-Driven Development](https://reader034.vdocuments.mx/reader034/viewer/2022052618/5549fc76b4c905557a8b4be3/html5/thumbnails/30.jpg)
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
• ...
![Page 31: Test-Driven Development](https://reader034.vdocuments.mx/reader034/viewer/2022052618/5549fc76b4c905557a8b4be3/html5/thumbnails/31.jpg)
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!
![Page 33: Test-Driven Development](https://reader034.vdocuments.mx/reader034/viewer/2022052618/5549fc76b4c905557a8b4be3/html5/thumbnails/33.jpg)
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
![Page 34: Test-Driven Development](https://reader034.vdocuments.mx/reader034/viewer/2022052618/5549fc76b4c905557a8b4be3/html5/thumbnails/34.jpg)
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
![Page 36: Test-Driven Development](https://reader034.vdocuments.mx/reader034/viewer/2022052618/5549fc76b4c905557a8b4be3/html5/thumbnails/36.jpg)
Do the simplest thing
1. Build the quickest code that will pass the tests
2. Refactor the code to have the simplest design possible
![Page 37: Test-Driven Development](https://reader034.vdocuments.mx/reader034/viewer/2022052618/5549fc76b4c905557a8b4be3/html5/thumbnails/37.jpg)
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
![Page 38: Test-Driven Development](https://reader034.vdocuments.mx/reader034/viewer/2022052618/5549fc76b4c905557a8b4be3/html5/thumbnails/38.jpg)
TDD is a key practice
• Defects kill predictability
no predictability, no planning!
• Test-driven is predictable
• Hardly ever use the debugger
![Page 39: Test-Driven Development](https://reader034.vdocuments.mx/reader034/viewer/2022052618/5549fc76b4c905557a8b4be3/html5/thumbnails/39.jpg)
No silver bullet
• Needs lots of practice
• Requires discipline
• Must think and be alert at all times!
![Page 40: Test-Driven Development](https://reader034.vdocuments.mx/reader034/viewer/2022052618/5549fc76b4c905557a8b4be3/html5/thumbnails/40.jpg)
Debugging Sucks
Testing Rocks
![Page 41: Test-Driven Development](https://reader034.vdocuments.mx/reader034/viewer/2022052618/5549fc76b4c905557a8b4be3/html5/thumbnails/41.jpg)
![Page 42: Test-Driven Development](https://reader034.vdocuments.mx/reader034/viewer/2022052618/5549fc76b4c905557a8b4be3/html5/thumbnails/42.jpg)
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
![Page 43: Test-Driven Development](https://reader034.vdocuments.mx/reader034/viewer/2022052618/5549fc76b4c905557a8b4be3/html5/thumbnails/43.jpg)
Back to the supermarket