workshop test drive development

29
F ocus On TDD Matteo Vaccari [email protected] @xpmatteo Agile Coach Camp Italy 2012

Upload: commit-software

Post on 14-Aug-2015

40 views

Category:

Engineering


7 download

TRANSCRIPT

Le tue aspettative?

Motivazione

Barry Boehm

Kent Beck

Running, tested features

T

F

http://www.infoq.com/interviews/jeffries-running-tested-features

Running, tested features

T

F

Running, tested features

T

F

Running, tested features

T

F

Running, tested features

T

F

T

F

T

F

Bugs?

T

F

No design?

T

F

Design every day

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 programmers (like me) most of the time

-- Kent Beck

Simple design

The code is simple enough when it:0. Runs all the tests1. Contains no duplication2. Expresses every idea that we need to express3. Has the minimum number of classes and functions

(In this order)

Kent Beck

Che vuol dire “semplice”?

Simple ≠ Easy

Rick H

ickey, Simple M

ade Easy

Design semplice: spezzare le cose in maniera che i pezzi

• Siano comprensibili in isolamento (coese - rappresentano un concetto)

• Siano componibili in nuove maniere (disaccoppiate e riusabili)

Test-Driven Development

1. Quickly add a test

2. Run all the tests, see the new one fail

3. Make a little change

4. Run all the tests, see them all succeed

5. Refactor to remove duplication

Quickly write a test

public class AdderTest { @Test public void testTwoPlusThree() { Adder a = new Adder();

assertEquals(5, a.add(2, 3)); }}

Make it compilepublic 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; }}

See it fail!

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; }}

Make a little testpublic 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; }}

Refactor to remove duplication

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; }}

The procedure

1. Write a test

2. Make it compile

3. Make it pass

4. Refactor

Expected 5, was 0

Red

GreenRefactor

Repeat every 2-10 min.

Demo: anni bisestili

• Un anno è bisestile se è divisibile per 4: 1996 è bisestile; 1995 non è bisestile

• A meno che non sia divisibile per 100: 1900 non è bisestile

• A meno che non sia divisibile per 400: 2000 è bisestile

Emily Bache, Coding Dojo Handbook

Problema: Monopoli

Vogliamo realizzare un’applicazione che gioca a Monopoli. Il computer

simula un certo numero di avversari e permette a un singolo

utente di giocare.

Monopoli semplificato• Simuliamo 4 giocatori

• La simulazione prosegue per 20 round

• Un round consiste nel turno di tutti i giocatori

• Il turno di un giocatore consiste nel lanciare i dadi e muovere il segnalino

• Le caselle si chiamano “Go!”, “Square 1”, “Square 2”, ... “Square 39”

• Non ci sono né soldi, né vincitori

Formulazione di Craig Larman, Applying UML and patterns