tdd reloaded - jugtaa 24 ottobre 2012

53
Test-Driven Development JUG Trentino Alto-Adige Ottobre 2012 Pietro Di Bello [email protected] twitter: @pierodibello www.xpeppers.com

Upload: pietro-di-bello

Post on 26-Jun-2015

481 views

Category:

Education


0 download

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

Test-Driven Development

JUG Trentino Alto-AdigeOttobre 2012

Pietro Di [email protected]

twitter: @pierodibellowww.xpeppers.com

Page 2: TDD reloaded - JUGTAA 24 Ottobre 2012

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

Che cos’è il TDD?

Page 4: TDD reloaded - JUGTAA 24 Ottobre 2012

Che cos’è il TDD?

TDD = Test-driven development

Page 5: TDD reloaded - JUGTAA 24 Ottobre 2012

Che cos’è il TDD?

TDD = Test-driven development

è una tecnica di sviluppo

Page 6: TDD reloaded - JUGTAA 24 Ottobre 2012

Che cos’è il TDD?

TDD = Test-driven development

è una tecnica di sviluppo

inventata da uno sviluppatore

Page 7: TDD reloaded - JUGTAA 24 Ottobre 2012

Che cos’è il TDD?

TDD = Test-driven design

è una tecnica di design e di sviluppo

Page 8: TDD reloaded - JUGTAA 24 Ottobre 2012

Qual’è l’obbiettivo del TDD?

L’obbiettivo del TDD è scrivere “codice pulito che funziona”

Page 9: TDD reloaded - JUGTAA 24 Ottobre 2012

• 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

Che cos’è il TDD?

TDD = Test-driven development

guidata dalla scrittura di test automatici unitari

Page 11: TDD reloaded - JUGTAA 24 Ottobre 2012

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

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

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

Il ritmo del TDD

Page 15: TDD reloaded - JUGTAA 24 Ottobre 2012

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

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

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

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

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

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

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

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

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

The procedure

Red

GreenRefactor

Repeat every 2-10 min.

Page 25: TDD reloaded - JUGTAA 24 Ottobre 2012

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

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

Maialino time... :)

Green — Make the test work quickly, committing whatever sins necessary in the process.

Page 28: TDD reloaded - JUGTAA 24 Ottobre 2012

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

Qual’è l’essenza del TDD?

L’essenza del TDD è “sapere sempre quale sarà il prossimo passo”

Page 30: TDD reloaded - JUGTAA 24 Ottobre 2012

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

L’essenza del TDD

Meno stress

Page 32: TDD reloaded - JUGTAA 24 Ottobre 2012

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

L’essenza del TDD

Progresso misurabile e verificabile

Page 34: TDD reloaded - JUGTAA 24 Ottobre 2012

L’essenza del TDD

A piccoli passi

Page 35: TDD reloaded - JUGTAA 24 Ottobre 2012

L’essenza del TDD

Copertura di test

Page 36: TDD reloaded - JUGTAA 24 Ottobre 2012

L’essenza del TDD

Controllo dello scope funzionale

Page 37: TDD reloaded - JUGTAA 24 Ottobre 2012

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

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

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

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

Debugging Sucks

Testing Rocks

Page 42: TDD reloaded - JUGTAA 24 Ottobre 2012
Page 43: TDD reloaded - JUGTAA 24 Ottobre 2012

• by Roy Osherove

• (http://osherove.com/tdd-kata-1)

String Calculator

Coding Dojo

Page 44: TDD reloaded - JUGTAA 24 Ottobre 2012

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

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

StringCalculator Kata

Allow the Add method to handle an unknown amount of numbers

Page 47: TDD reloaded - JUGTAA 24 Ottobre 2012

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

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

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

Numbers bigger than 1000 should be ignored.

So adding 2 + 1001 = 2

StringCalculator Kata

Page 51: TDD reloaded - JUGTAA 24 Ottobre 2012

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

Make sure you can also handle multiple delimiters with length longer than one char

StringCalculator Kata

Page 53: TDD reloaded - JUGTAA 24 Ottobre 2012

Contacts

• Website www.xpeppers.com

• E-Mail [email protected]

• Twitter @xpeppers