Linux Day 20091024 Test Driven Development

28

Click here to load reader

Transcript of Linux Day 20091024 Test Driven Development

Page 1: Linux Day 20091024 Test Driven Development

Breve introduzione al

Test DrivenDevelopment

Roberto [email protected]

http://www.sourcesense.it/it/agile/teamhttp://www.xpug.it

http://www.lugcr.ithttp://www.linuxday.it

Page 2: Linux Day 20091024 Test Driven Development

Test Driven Development

“ Write new code only ifan automated test has failed ”

Kent Beck

\

Page 3: Linux Day 20091024 Test Driven Development

Test Driven Development

“ Only ever write codeto fix a failing test ”

Lasse Koskela

\

Page 4: Linux Day 20091024 Test Driven Development

Test Driven Development

“ we produce well-designed, well-tested, and well-factored code

in small, verifiable steps ”James Shore

\

Page 5: Linux Day 20091024 Test Driven Development

i passi del TDD

RED GREEN REFACTORWrite a test Make it run Make it right

Test Code Design

passi piccoli, incrementali, automatizzati

Page 6: Linux Day 20091024 Test Driven Development

Write a test: RED

Scrivi un piccolo

unit test che non passa

o non compila

il test racconta una funzionalità

oppure dimostra un baco

Page 7: Linux Day 20091024 Test Driven Development

cos'è uno Unit Test

è un Testcodice che verifica

il funzionamento di altro codice

è Unitariodi una sola “unità” di codice

senza interazioni con altre “unità” o sistemi esterni

Page 8: Linux Day 20091024 Test Driven Development

un buon test

è automatizzato e ripetibileun solo click per lanciare tutti i test

l'output è consistente

output semplice: verde o rossoverifica da solo l'output

output “verboso” solo quando è rosso

Page 9: Linux Day 20091024 Test Driven Development

Make it run: GREEN

scrivi una

semplice implementazioneche faccia passare il test

L’implementazione deve essere la

soluzione più rapida e semplice, anche banale.

l’obiettivo è avere i test verdi il prima possibile.

Page 10: Linux Day 20091024 Test Driven Development

Make it right: REFACTOR

elimina gli smell

migliora il design

Questa è l'attività che prende la maggior parte del tempo.

Si fa refactor sia del codice di produzione sia del codice di test.

Page 11: Linux Day 20091024 Test Driven Development

Refactor

“ Eliminate all of the duplication createdin merely getting the test to work ”

Kent Beck

\

Page 12: Linux Day 20091024 Test Driven Development

Refactor

“ ...is about transforming the currentdesign toward a better design ”

Lasse Koskela

\

Page 13: Linux Day 20091024 Test Driven Development

Cos'è il Refactoring?

“ Is the process of changinga software system in such a way

that it does not alter theexternal behaviour of the code

yet improves its internal structure ”

Martin Fowler \

Page 14: Linux Day 20091024 Test Driven Development

Cos'è il Refactoring?

migliorare il design del codice esistente senza correre rischi

riscivere il codice non è fare refactoring

esempio di alcuni passi:

Extract method, Move method, Inline method, Extract Class,

Introduce Parametr Object, Replace temp with query,

Replace conditional with polymorphism

Page 15: Linux Day 20091024 Test Driven Development

Cos'è il Refactoring?

miglioramenti a piccoli passi

senza “rompere” l'esistenteriscivere il codice non è fare refactoring

aggiungere/modificare funzionalità non è fare refactoring

esempio di alcuni passi:

Extract method, Move method, Inline method, Extract Class,

Introduce Parametr Object, Replace temp with query,

Replace conditional with polymorphism

Page 16: Linux Day 20091024 Test Driven Development

Cosa sono i code smell?

sintomi e indizi di

qualcosa che “non va” nel codice

esempio di alcuni smell

duplicazioni, magic number, comenti sospetti, metodi lunghi, feautre envy,

lunghe liste di parametri, inappropriate intimacy, message chain, data clump

Page 17: Linux Day 20091024 Test Driven Development

Test Driven Development

RED GREEN REFACTOR Write a test Make it run Make it right

Test Code Design

Ripetere ogni pochi minuti

Page 18: Linux Day 20091024 Test Driven Development

Test Driven Development

RED GREEN

REFACTOR

Ogni 2-10 minuti

Page 19: Linux Day 20091024 Test Driven Development

Test Driven Development

è uno stile di sviluppo

è una attività di design

non è una attività di testing

codice testabile

decisioni di design poco alla volta

prospettiva corretta nel disegnare le api

focus sull'obiettivo

Page 20: Linux Day 20091024 Test Driven Development

i Test non sono lo scopo

sono lo strumentopongono nella corretta prospettiva

aiutano a scomporre i problemi e isolarli

aiutano a individuare gli errori con cicli brevi di feedback

e sono un utile prodotto “di scarto”rimangono come rete di sicurezza efficacie e duratura

Page 21: Linux Day 20091024 Test Driven Development

un esempio

Page 22: Linux Day 20091024 Test Driven Development

Punteggio del Bowling

Page 23: Linux Day 20091024 Test Driven Development

Frame

in una partita si giocano 10 frame

in ogni frame si hanno a disposizione

2 tiri per abbattere i 10 birilli

Page 24: Linux Day 20091024 Test Driven Development

Punti in un Frame

in ogni frame si prende un punto

per ogni birillo abbattuto

Esempi

0+0 = segna 0 punti

6+3 = segna 9 punti

3+1 = segna 4 punti

Page 25: Linux Day 20091024 Test Driven Development

Frame con Spare

se in un frame con i due

si abbattono tutti i 10 birilli

si prende un punto bonus per ogni birillo

abbattuto nel tiro successivo

Esempi

Tiro 1 e 9 seguito da 3 e 4 → 13 punti seguiti da 7 punti

Tiro 6 e 4 seguito da 4 e 0 → 14 punti seguiti da 4 punti

Tiro 6 e 4 seguito da 0 e 4 → 10 punti seguiti da 4 punti

Page 26: Linux Day 20091024 Test Driven Development

Frame con Strike

se in un frame con il primo tiro

si abbattono tutti i 10 birilli

non si fa il secondo tiro e

si riceve un punto bonus per ogni birillo

abbattuto nei due tiri successivi

Esempi

Tiro 10, poi 3 e 4 → 17 punti, poi 7 punti

Tiro 10, poi 0 e 4 → 14 punti, poi 4 punti

Tiro 10, poi 10, poi 2+3 → 22 punti, poi 15 punti, poi 5 punti

Page 27: Linux Day 20091024 Test Driven Development

Ultimo frame

se all'ultimo frame ottengo uno spare o uno strike

faccio i tiri necessari ad ottenere i bonus

Esempio

All'ultimo frame tiro 3+7, tiro bonus 4 → 14 punti

All'ultimo frame tiro 10, tiri bonus 3 e 4 → 17 punti

All'ultimo frame tiro 10, tiri bonus 10 e 10 → 30 punti

Page 28: Linux Day 20091024 Test Driven Development

kata

http://butunclebob.com/ArticleS.UncleBob.TheBowlingGameKata