tdd reloaded - jugtaa 24 ottobre 2012
DESCRIPTION
Seconda serata di introduzione al Test-Driven Development, tenuta in XPeppers a Trento il 24 Ottobre 2012. Nelle slide c'è anche la descrizione del coding dojo sullo string calculator che abbiamo svolto assieme.TRANSCRIPT
![Page 1: TDD reloaded - JUGTAA 24 Ottobre 2012](https://reader036.vdocuments.net/reader036/viewer/2022081512/558ccff0d8b42a812b8b4654/html5/thumbnails/1.jpg)
Test-Driven Development
JUG Trentino Alto-AdigeOttobre 2012
Pietro Di [email protected]
twitter: @pierodibellowww.xpeppers.com
![Page 2: TDD reloaded - JUGTAA 24 Ottobre 2012](https://reader036.vdocuments.net/reader036/viewer/2022081512/558ccff0d8b42a812b8b4654/html5/thumbnails/2.jpg)
Test-Driven Development
JUG Trentino Alto-AdigeOttobre 2012
Pietro Di [email protected]
twitter: @pierodibellowww.xpeppers.com
reloaded
![Page 3: TDD reloaded - JUGTAA 24 Ottobre 2012](https://reader036.vdocuments.net/reader036/viewer/2022081512/558ccff0d8b42a812b8b4654/html5/thumbnails/3.jpg)
Che cos’è il TDD?
![Page 4: TDD reloaded - JUGTAA 24 Ottobre 2012](https://reader036.vdocuments.net/reader036/viewer/2022081512/558ccff0d8b42a812b8b4654/html5/thumbnails/4.jpg)
Che cos’è il TDD?
TDD = Test-driven development
![Page 5: TDD reloaded - JUGTAA 24 Ottobre 2012](https://reader036.vdocuments.net/reader036/viewer/2022081512/558ccff0d8b42a812b8b4654/html5/thumbnails/5.jpg)
Che cos’è il TDD?
TDD = Test-driven development
è una tecnica di sviluppo
![Page 6: TDD reloaded - JUGTAA 24 Ottobre 2012](https://reader036.vdocuments.net/reader036/viewer/2022081512/558ccff0d8b42a812b8b4654/html5/thumbnails/6.jpg)
Che cos’è il TDD?
TDD = Test-driven development
è una tecnica di sviluppo
inventata da uno sviluppatore
![Page 7: TDD reloaded - JUGTAA 24 Ottobre 2012](https://reader036.vdocuments.net/reader036/viewer/2022081512/558ccff0d8b42a812b8b4654/html5/thumbnails/7.jpg)
Che cos’è il TDD?
TDD = Test-driven design
è una tecnica di design e di sviluppo
![Page 8: TDD reloaded - JUGTAA 24 Ottobre 2012](https://reader036.vdocuments.net/reader036/viewer/2022081512/558ccff0d8b42a812b8b4654/html5/thumbnails/8.jpg)
Qual’è l’obbiettivo del TDD?
L’obbiettivo del TDD è scrivere “codice pulito che funziona”
![Page 9: TDD reloaded - JUGTAA 24 Ottobre 2012](https://reader036.vdocuments.net/reader036/viewer/2022081512/558ccff0d8b42a812b8b4654/html5/thumbnails/9.jpg)
• 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
Clean code that works
Qual’è l’obbiettivo del TDD?
![Page 10: TDD reloaded - JUGTAA 24 Ottobre 2012](https://reader036.vdocuments.net/reader036/viewer/2022081512/558ccff0d8b42a812b8b4654/html5/thumbnails/10.jpg)
Che cos’è il TDD?
TDD = Test-driven development
guidata dalla scrittura di test automatici unitari
![Page 11: TDD reloaded - JUGTAA 24 Ottobre 2012](https://reader036.vdocuments.net/reader036/viewer/2022081512/558ccff0d8b42a812b8b4654/html5/thumbnails/11.jpg)
Che cos’è il TDD?
Test automatici e unitari
automatici = le verifiche le fa la macchina al posto
mio
![Page 12: TDD reloaded - JUGTAA 24 Ottobre 2012](https://reader036.vdocuments.net/reader036/viewer/2022081512/558ccff0d8b42a812b8b4654/html5/thumbnails/12.jpg)
Che cos’è il TDD?
Test automatici e unitari
unitari = non verifico l’intero sistema ma una
singola unità (classe, modulo, funzione)
![Page 13: TDD reloaded - JUGTAA 24 Ottobre 2012](https://reader036.vdocuments.net/reader036/viewer/2022081512/558ccff0d8b42a812b8b4654/html5/thumbnails/13.jpg)
e allora?
Nel TDD si usano i test automatici per guidare il design e lo sviluppo del codice
![Page 14: TDD reloaded - JUGTAA 24 Ottobre 2012](https://reader036.vdocuments.net/reader036/viewer/2022081512/558ccff0d8b42a812b8b4654/html5/thumbnails/14.jpg)
Il ritmo del TDD
![Page 15: TDD reloaded - JUGTAA 24 Ottobre 2012](https://reader036.vdocuments.net/reader036/viewer/2022081512/558ccff0d8b42a812b8b4654/html5/thumbnails/15.jpg)
Il ritmo del TDD
Red — Write a little test that doesn't work, and perhaps doesn't even compile at first.
![Page 16: TDD reloaded - JUGTAA 24 Ottobre 2012](https://reader036.vdocuments.net/reader036/viewer/2022081512/558ccff0d8b42a812b8b4654/html5/thumbnails/16.jpg)
Il ritmo del TDD
Green — Make the test work quickly, committing whatever sins necessary in the process.
Red — Write a little test that doesn't work, and perhaps doesn't even compile at first.
![Page 17: TDD reloaded - JUGTAA 24 Ottobre 2012](https://reader036.vdocuments.net/reader036/viewer/2022081512/558ccff0d8b42a812b8b4654/html5/thumbnails/17.jpg)
Il ritmo del TDD
Green — Make the test work quickly, committing whatever sins necessary in the process.
Refactor — Eliminate all of the duplication created in merely getting the test to work.
Red — Write a little test that doesn't work, and perhaps doesn't even compile at first.
![Page 18: TDD reloaded - JUGTAA 24 Ottobre 2012](https://reader036.vdocuments.net/reader036/viewer/2022081512/558ccff0d8b42a812b8b4654/html5/thumbnails/18.jpg)
Write a test
public class AdderTest { @Test public void testTwoPlusThree() { Adder a = new Adder();
assertEquals(5, a.add(2, 3)); }}
![Page 19: TDD reloaded - JUGTAA 24 Ottobre 2012](https://reader036.vdocuments.net/reader036/viewer/2022081512/558ccff0d8b42a812b8b4654/html5/thumbnails/19.jpg)
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; }}
![Page 20: TDD reloaded - JUGTAA 24 Ottobre 2012](https://reader036.vdocuments.net/reader036/viewer/2022081512/558ccff0d8b42a812b8b4654/html5/thumbnails/20.jpg)
Red bar!
Expected 5, was 0
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; }}
![Page 21: TDD reloaded - JUGTAA 24 Ottobre 2012](https://reader036.vdocuments.net/reader036/viewer/2022081512/558ccff0d8b42a812b8b4654/html5/thumbnails/21.jpg)
Do the simplest thing
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; }}
![Page 22: TDD reloaded - JUGTAA 24 Ottobre 2012](https://reader036.vdocuments.net/reader036/viewer/2022081512/558ccff0d8b42a812b8b4654/html5/thumbnails/22.jpg)
Refactor
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; }}
![Page 23: TDD reloaded - JUGTAA 24 Ottobre 2012](https://reader036.vdocuments.net/reader036/viewer/2022081512/558ccff0d8b42a812b8b4654/html5/thumbnails/23.jpg)
The procedure
1. Write a test
2. Make it compile
3. Make it pass quickly
4. Refactor to remove duplication
Expected 5, was 0
![Page 24: TDD reloaded - JUGTAA 24 Ottobre 2012](https://reader036.vdocuments.net/reader036/viewer/2022081512/558ccff0d8b42a812b8b4654/html5/thumbnails/24.jpg)
The procedure
Red
GreenRefactor
Repeat every 2-10 min.
![Page 25: TDD reloaded - JUGTAA 24 Ottobre 2012](https://reader036.vdocuments.net/reader036/viewer/2022081512/558ccff0d8b42a812b8b4654/html5/thumbnails/25.jpg)
Prima un test che fallisce
Red — Write a little test that doesn't work, and perhaps doesn't even compile at first.
![Page 26: TDD reloaded - JUGTAA 24 Ottobre 2012](https://reader036.vdocuments.net/reader036/viewer/2022081512/558ccff0d8b42a812b8b4654/html5/thumbnails/26.jpg)
Il codice visto dalla barra rossa
When we write a test, we imagine the perfect interface for our operation. We are telling ourselves a story about how the operation will look from the outside. Our story won't always come true, but it's better to start from the best-possible application program interface (API) and work backward than to make things complicated, ugly, and "realistic" from the get-go.
Kent Beck
![Page 27: TDD reloaded - JUGTAA 24 Ottobre 2012](https://reader036.vdocuments.net/reader036/viewer/2022081512/558ccff0d8b42a812b8b4654/html5/thumbnails/27.jpg)
Maialino time... :)
Green — Make the test work quickly, committing whatever sins necessary in the process.
![Page 28: TDD reloaded - JUGTAA 24 Ottobre 2012](https://reader036.vdocuments.net/reader036/viewer/2022081512/558ccff0d8b42a812b8b4654/html5/thumbnails/28.jpg)
Paga per i tuoi debiti...
Refactor — Eliminate all of the duplication created in merely getting the test to work.
![Page 29: TDD reloaded - JUGTAA 24 Ottobre 2012](https://reader036.vdocuments.net/reader036/viewer/2022081512/558ccff0d8b42a812b8b4654/html5/thumbnails/29.jpg)
Qual’è l’essenza del TDD?
L’essenza del TDD è “sapere sempre quale sarà il prossimo passo”
![Page 30: TDD reloaded - JUGTAA 24 Ottobre 2012](https://reader036.vdocuments.net/reader036/viewer/2022081512/558ccff0d8b42a812b8b4654/html5/thumbnails/30.jpg)
Qual’è l’essenza del TDD?
L’essenza del TDD è “sapere sempre quale sarà il prossimo passo”
Avere chiara la strada
![Page 31: TDD reloaded - JUGTAA 24 Ottobre 2012](https://reader036.vdocuments.net/reader036/viewer/2022081512/558ccff0d8b42a812b8b4654/html5/thumbnails/31.jpg)
L’essenza del TDD
Meno stress
![Page 32: TDD reloaded - JUGTAA 24 Ottobre 2012](https://reader036.vdocuments.net/reader036/viewer/2022081512/558ccff0d8b42a812b8b4654/html5/thumbnails/32.jpg)
L’essenza del TDD
Meno stress
• Progresso misurabile e verificabile
• Copertura di test
• Controllo dello scope
• Riduzione del bug rate
![Page 33: TDD reloaded - JUGTAA 24 Ottobre 2012](https://reader036.vdocuments.net/reader036/viewer/2022081512/558ccff0d8b42a812b8b4654/html5/thumbnails/33.jpg)
L’essenza del TDD
Progresso misurabile e verificabile
![Page 34: TDD reloaded - JUGTAA 24 Ottobre 2012](https://reader036.vdocuments.net/reader036/viewer/2022081512/558ccff0d8b42a812b8b4654/html5/thumbnails/34.jpg)
L’essenza del TDD
A piccoli passi
![Page 35: TDD reloaded - JUGTAA 24 Ottobre 2012](https://reader036.vdocuments.net/reader036/viewer/2022081512/558ccff0d8b42a812b8b4654/html5/thumbnails/35.jpg)
L’essenza del TDD
Copertura di test
![Page 36: TDD reloaded - JUGTAA 24 Ottobre 2012](https://reader036.vdocuments.net/reader036/viewer/2022081512/558ccff0d8b42a812b8b4654/html5/thumbnails/36.jpg)
L’essenza del TDD
Controllo dello scope funzionale
![Page 37: TDD reloaded - JUGTAA 24 Ottobre 2012](https://reader036.vdocuments.net/reader036/viewer/2022081512/558ccff0d8b42a812b8b4654/html5/thumbnails/37.jpg)
Uso della TODO list• Utile per gestire la complessità
• elencare i diversi aspetti del problema• NON si elencano i test da implementare
• non è TDD• Riduce lo stress• Bussola per farci capire a che punto siamo
e non ci perdere la strada• Se mi viene in mente qualcosa la scrivo
nella todolist e continuo sulla mia strada senza perdere focus
![Page 38: TDD reloaded - JUGTAA 24 Ottobre 2012](https://reader036.vdocuments.net/reader036/viewer/2022081512/558ccff0d8b42a812b8b4654/html5/thumbnails/38.jpg)
Quando usare il TDD è difficile?
• Quando si sviluppare codice multithreaded
• Quando si lavora su codice legacy
• Quando si padroneggia poco il linguaggio
di programmazione usato
![Page 39: TDD reloaded - JUGTAA 24 Ottobre 2012](https://reader036.vdocuments.net/reader036/viewer/2022081512/558ccff0d8b42a812b8b4654/html5/thumbnails/39.jpg)
Cosa non è uno unit-test?
A test is not a unit test if:
• It talks to the database• It communicates across the network• It touches the file system• It can't run at the same time as any of your
other unit tests• You have to do special things to your
environment (such as editing config files) to run it.
Michael Feathers (“A Set of Unit Testing Rules”)
![Page 40: TDD reloaded - JUGTAA 24 Ottobre 2012](https://reader036.vdocuments.net/reader036/viewer/2022081512/558ccff0d8b42a812b8b4654/html5/thumbnails/40.jpg)
The Three Rules Of TDD
• You are not allowed to write any production code unless it is to make a failing unit test pass.
• You are not allowed to write any more of a unit test than is sufficient to fail; and compilation failures are failures.
• You are not allowed to write any more production code than is sufficient to pass the one failing unit test.
Robert “Uncle Bob” Martin (“The Three Laws of TDD”)
![Page 41: TDD reloaded - JUGTAA 24 Ottobre 2012](https://reader036.vdocuments.net/reader036/viewer/2022081512/558ccff0d8b42a812b8b4654/html5/thumbnails/41.jpg)
Debugging Sucks
Testing Rocks
![Page 42: TDD reloaded - JUGTAA 24 Ottobre 2012](https://reader036.vdocuments.net/reader036/viewer/2022081512/558ccff0d8b42a812b8b4654/html5/thumbnails/42.jpg)
![Page 43: TDD reloaded - JUGTAA 24 Ottobre 2012](https://reader036.vdocuments.net/reader036/viewer/2022081512/558ccff0d8b42a812b8b4654/html5/thumbnails/43.jpg)
• by Roy Osherove
• (http://osherove.com/tdd-kata-1)
String Calculator
Coding Dojo
![Page 44: TDD reloaded - JUGTAA 24 Ottobre 2012](https://reader036.vdocuments.net/reader036/viewer/2022081512/558ccff0d8b42a812b8b4654/html5/thumbnails/44.jpg)
StringCalculator Kata
• Create a simple String calculator with a method
• int Add(string numbers)
• The method can take zero, one or two numbers, and will return their sum
• for example “” or “1” or “1,2”
• for an empty string it will return 0
Start with the simplest test case of an empty string and move to one and two numbers
![Page 45: TDD reloaded - JUGTAA 24 Ottobre 2012](https://reader036.vdocuments.net/reader036/viewer/2022081512/558ccff0d8b42a812b8b4654/html5/thumbnails/45.jpg)
StringCalculator Kata
• Remember to solve things as simply as possible so that you force yourself to write tests you did not think about
• Remember to refactor after each passing test
![Page 46: TDD reloaded - JUGTAA 24 Ottobre 2012](https://reader036.vdocuments.net/reader036/viewer/2022081512/558ccff0d8b42a812b8b4654/html5/thumbnails/46.jpg)
StringCalculator Kata
Allow the Add method to handle an unknown amount of numbers
![Page 47: TDD reloaded - JUGTAA 24 Ottobre 2012](https://reader036.vdocuments.net/reader036/viewer/2022081512/558ccff0d8b42a812b8b4654/html5/thumbnails/47.jpg)
Allow the Add method to handle new lines between numbers (instead of commas).
• the following input is ok: “1\n2,3” (will equal 6)
• the following input is NOT ok: “1,\n” (not need to prove it - just clarifying)
StringCalculator Kata
![Page 48: TDD reloaded - JUGTAA 24 Ottobre 2012](https://reader036.vdocuments.net/reader036/viewer/2022081512/558ccff0d8b42a812b8b4654/html5/thumbnails/48.jpg)
Support different delimiters
to change a delimiter, the beginning of the string will contain a separate line that looks like this:
“//[delimiter]\n[numbers…]”
for example “//;\n1;2” should return three where the default delimiter is ‘;’ .
the first line is optional.
all existing scenarios should still be supported
StringCalculator Kata
![Page 49: TDD reloaded - JUGTAA 24 Ottobre 2012](https://reader036.vdocuments.net/reader036/viewer/2022081512/558ccff0d8b42a812b8b4654/html5/thumbnails/49.jpg)
Calling Add with a negative number will throw an exception “negatives not allowed” - and the negative that was passed.
If there are multiple negatives, show all of them in the exception message
StringCalculator Kata
![Page 50: TDD reloaded - JUGTAA 24 Ottobre 2012](https://reader036.vdocuments.net/reader036/viewer/2022081512/558ccff0d8b42a812b8b4654/html5/thumbnails/50.jpg)
Numbers bigger than 1000 should be ignored.
So adding 2 + 1001 = 2
StringCalculator Kata
![Page 51: TDD reloaded - JUGTAA 24 Ottobre 2012](https://reader036.vdocuments.net/reader036/viewer/2022081512/558ccff0d8b42a812b8b4654/html5/thumbnails/51.jpg)
Delimiters can be of any length with the following format:
“//[delimiter]\n”
For example:“//[***]\n1***2***3” returns 6.
StringCalculator Kata
![Page 52: TDD reloaded - JUGTAA 24 Ottobre 2012](https://reader036.vdocuments.net/reader036/viewer/2022081512/558ccff0d8b42a812b8b4654/html5/thumbnails/52.jpg)
Make sure you can also handle multiple delimiters with length longer than one char
StringCalculator Kata