SlideShare una empresa de Scribd logo
1 de 65
Introduzione al
Test Driven Development
Andrea Francia
http://www.andreafrancia.it
Contents
Concepts
Definition
Example1: Parser
Example2: Text To Speech
Concepts
Test First
Automate tests
What is a Automated
Test?
Automated Test Lifecycle
Giordano Scalzo: http://www.slideshare.net/giordano/tdd-iphonefordummies
Automated Test Lifecycle
Giordano Scalzo: http://www.slideshare.net/giordano/tdd-iphonefordummies
Automated Test Lifecycle
SUT: System Under Test
Giordano Scalzo: http://www.slideshare.net/giordano/tdd-iphonefordummies
Automated Test Lifecycle
SUT: System Under Test
Giordano Scalzo: http://www.slideshare.net/giordano/tdd-iphonefordummies
Automated Test Lifecycle
SUT: System Under Test
Giordano Scalzo: http://www.slideshare.net/giordano/tdd-iphonefordummies
Example of a Manual Test:
nextLine()
public static void main(String[] args) {
String text = "first linensecond line";
Scanner scanner = new Scanner(text);
System.out.println(scanner.nextLine()); // prints
"first line"
System.out.println(scanner.nextLine()); // prints
"second line”
}
Code:
assertEquals(expected, actual);
PASS FAIL
expected == actual
yes no
Using JUnit
@Test public void howNextLineWorks() throws IOException {
String text = "first linensecond line";
Scanner scanner = new Scanner(text);
assertEquals(”first line", scanner.nextLine());
assertEquals(”second line", scanner.nextLine());
}
Code:
Output:
What is Test Driven
Development?
What is TDD?
Test-driven development (TDD) is a software
development process that relies on the repetition of a
very short development cycle:
Giordano Scalzo: http://www.slideshare.net/giordano/tdd-iphonefordummies
Tim Ottinger:http://agileinaflash.blogspot.com/2009/02/red-green-refactor.html
RED
first the developer writes a failing automated test case
that defines a desired new behaviour (of the software),
GREEN
then produces code to pass that test …
Refactor
and finally refactors the new code to acceptable standards.
Example:
Writing “df” Parser
The df output
[andreafrancia@deneb Dropbox]$ df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/disk0s2 243862672 135971832 107634840 56% /
/dev/disk0s1 243862672 135971832 107634840 56% /tmp
/dev/disk1s2 243862672 135971832 107634840 56% /opt
devfs 109 109 0 100% /dev
Mount Points
The problem
List all mount points
The Problem
Write a method that extract the list of the mount points
from the output of df.
First of all decompose
the problem!
Decomposition
Parse the output of “df” process …
When there are no mounted volumes
When there is only one volume mounted
When there are many volumes
When the a volume contains whitespaces
First Cycle
Parse the output of “df” process
When there are no volumes
When there is only one volume
When there are many volumes
When the a volume contains whitespaces
@Test
public void whenNoVolumes() {
String input = "Filesystem 1024-blocks Used Available Capacity
Mounted on”;
List<String> result = parseDfOutput(input);
assertEquals(emptyList(), result);
}
@Test
public void whenNoVolumes() {
String input = "Filesystem 1024-blocks Used Available Capacity
Mounted on”;
List<String> result = parseDfOutput(input);
assertEquals(emptyList(), result);
}
private List<String> parseDfOutput(String string) {
return null;
}
@Test
public void whenNoVolumes() {
String input = "Filesystem 1024-blocks Used Available Capacity
Mounted on”;
List<String> result = parseDfOutput(input);
assertEquals(emptyList(), result);
}
private List<String> parseDfOutput(String string) {
return emptyList();
}
No need for refactoring
Second Cycle
Parse the output of “df” process
When there are no volumes
When there is only one volume
When there are many volumes
When the a volume contains whitespaces
@Test public void whenOneVolume() {
List<String> result = parseDfOutput(""
+ "Filesystem 1024-blocks Used Available Capacity Mounted
onn"
+ "/dev/disk0s2 243862672 135479924 108126748 56% /");
assertEquals(asList("/"), result);
}
private List<String> parseDfOutput(String string) {
return emptyList();
}
private List<String> parseDfOutput(String string) {
List<String> result = new ArrayList<String>();
return result;
}
private List<String> parseDfOutput(String input) {
List<String> result = new ArrayList<String>();
Scanner scanner = new Scanner(input);
scanner.nextLine(); // skip header
if(scanner.hasNextLine()) {
String line = scanner.nextLine();
Scanner scanner1 = new Scanner(line);
scanner1.next(); // skip Filesystem
scanner1.next(); // skip 1024-blocks
scanner1.next(); // skip Used
scanner1.next(); // skip Available
scanner1.next(); // skip Capacity
String mountPoint = scanner1.next();
result.add(mountPoint);
}
return result;
}
private List<String> parseDfOutput(String input) {
List<String> result = new ArrayList<String>();
Scanner lines = new Scanner(input);
lines.nextLine(); // skip header
if(lines.hasNextLine()) {
String line = lines.nextLine();
Scanner values = new Scanner(line);
values.next(); // skip Filesystem
values.next(); // skip 1024-blocks
values.next(); // skip Used
values.next(); // skip Available
values.next(); // skip Capacity
String mountPoint = values.next();
result.add(mountPoint);
}
return result;
}
private List<String> parseDfOutput(String input) {
List<String> result = new ArrayList<String>();
Scanner lines = new Scanner(input);
lines.nextLine(); // skip header
if(lines.hasNextLine()) {
result.add(parseMountPoint(lines.nextLine()));
}
return result;
}
private String parseMountPoint(String line) {
Scanner values = new Scanner(line);
values.next(); // skip Filesystem
values.next(); // skip 1024-blocks
values.next(); // skip Used
values.next(); // skip Available
values.next(); // skip Capacity
String mountPoint = values.next();
return mountPoint;
}
Third Cycle
Parse the output of “df” process
When there are no volumes
When there is only one volume
When there are many volumes
When the a volume contains whitespaces
@Test public void whenMultipleVolume() {
List<String> result = parseDfOutput(""
+ "Filesystem 1024-blocks Used Available Capacity Mounted
onn"
+ "/dev/disk0s2 243862672 135479924 108126748 56% /n"
+ "/dev/disk0s2 243862672 135479924 108126748 56%
/media/diskn"
+ "/dev/disk0s2 243862672 135479924 108126748 56%
/tmpn");
assertEquals(asList("/", "/media/disk", "/tmp"), result);
}
@Test public void whenMultipleVolume() {
List<String> result = parseDfOutput(""
+ "Filesystem 1024-blocks Used Available Capacity Mounted
onn"
+ "/dev/disk0s2 243862672 135479924 108126748 56% /n"
+ "/dev/disk0s2 243862672 135479924 108126748 56%
/media/diskn"
+ "/dev/disk0s2 243862672 135479924 108126748 56%
/tmpn");
assertEquals(asList("/", "/media/disk", "/tmp"), result);
}
private List<String> parseDfOutput(String input) {
List<String> result = new ArrayList<String>();
Scanner lines = new Scanner(input);
lines.nextLine(); // skip header
if(lines.hasNextLine()) {
String line = lines.nextLine();
String mountPoint = parseMountPoint(line);
result.add(mountPoint);
}
return result;
}
private List<String> parseDfOutput(String input) {
List<String> result = new ArrayList<String>();
Scanner lines = new Scanner(input);
lines.nextLine(); // skip header
while(lines.hasNextLine()) {
String line = lines.nextLine();
String mountPoint = parseMountPoint(line);
result.add(mountPoint);
}
return result;
}
TDD Rules
TDD Rules
Test First
Test for All Features
Remove all duplication
(always)
Some hints
Keep tests running and passing
Keeps test small
Don’t mix phases (Red, Green,
Refactor)
Don’t mix unit tests with integration
tests
Test only one feature per test
Examples from my last
work
Regole applicate
Ogni feature deve essere sviluppata secondo il
TDD
Partire dai test di accettazione
Ritardare le decisioni di design all’ultimo
momento responsabile
Applicare un principio di design solo dopo aver
avuto la prova che sia utile in quel specifico caso
Prima feature: produrre
testo pronunciabile
Test di accettazione
public class HoroscopeTest {
@Test public void
userListenOroscope() {
Horoscope horoscope = new Horoscope();
horoscope.saveDivination("22-GIU-10", "Ariete",
"Sarai molto fortunato.");
String result =
horoscope.GET("/horoscope/ariete.txt");
assertEquals("22 giugno 2010: Ariete, Sarai
molto fortunato.”, result);
}
No test  No code
Quando scrivo il codice di produzione scrivo il
minimo necessario a far passare il test
Se il minimo non mi convince (è troppo stupido),
vuol dire che manca una specifica funzionale 
cioè manca un test.
Prima di scrivere una qualsiasi riga di codice in
più aggiungo un test che la richieda.
@Test public void
shouldStoreDivinationsForMultipleSigns() {
Horoscope horoscope = new Horoscope();
horoscope.saveDivination("22-GIU-10", "Ariete",
"for ariete");
horoscope.saveDivination("22-GIU-10", "Toro",
"for toro");
assertEquals("22 giugno 2010: Ariete, for ariete",
horoscope.GET("/horoscope/ariete.txt"));
assertEquals("22 giugno 2010: Toro, for toro",
horoscope.GET("/horoscope/toro.txt"));
}
Cerco di non anticipare il
design
Prima di affrontare lo sviluppo faccio una veloce sessione
di design
Non implemento nessuna decisione fino a che non si
rende necessaria
E.g. anche servirà un DAO per adesso salvo tutto in RAM
Seconda Feature:
oroscopo ascoltabile
@Test public void howToCreateMp3() {
Horoscope horoscope = new Horoscope(
aFakeSyntetizerWhichReturns(
aMp3Stream()));
horoscope.saveDivination("22-GIU-10", "Ariete",
"divination");
assertThat(horoscope.GET(
"/horoscope/ariete.mp3").asByteArray(),
is(equalTo(aMp3Stream())));
}
Resource
Ora il GET restituisce una Resource
 public Resource GET(String path) {...}
Il client decide quale rappresentazione usare:
horoscope.GET("/horoscope/ariete/divination.txt").asString());
horoscope.GET("/horoscope/ariete/divination.mp3").asByteArray();
Terza feature: la
persistenza
Stato delle cose
Al momento tutto viene persistito in memoria (in una
HashMap)
Non esiste ancora un oggetto DAO, tutto viene fatto
dall’unica class Horoscope
Estrazione del comportamento
public class MemoryDivinationRepo {
Divination lastDivinationForSign(String sign);
void saveDivinationFromPronounce(String sign,
String pronounce);
};
public interface Divination {
String asText();
byte[] asMp3();
};
Estrazione dell’interfaccia
public class MemoryDivinationRepo
implements DivinationRepo {...}
public interface DivinationRepo {
Divination lastDivinationForSign(String sign);
void saveDivinationFromPronounce(String sign,
String pronounce);
};
Caratterizzazione del
comportamento
L’interfaccia è una minima parte del contratto, la parte
più importante è il comportamento che l’oggetto
dovrebbe avere.
Il comportamento lo estraggo con dei test di
caratterizzazione
Caratterizzazione di
MemoryDivinationRepo
public class MemoryDivinationRepoTest {
@Test public void
shouldStoreDivinationPronounceForASign() {...}
@Test public void
shouldReplyWithAPronouncedMp3() {...}
@Test public void
shouldStoreDivinationPronounceForMultipleSigns()
{...}
@Test public void
shouldOverrideDivinationPronounce() {...}
}
Thanks
Andrea Francia
www.andreafrancia.it
blog.andreafrancia.it
andrea@andreafrancia.it
Extra slides
Example of an Automated Test
@Test public void shouldParsePath() {
String content =
"[Trash Info]n"
+ "Path=/home/andrea/foo.txtn"
+ "DeletionDate=2010-08-23T12:59:14n";
String path = Parser.parsePath(content);
assertEquals("/home/andrea/foo.txt”,
path);
}
Testing Frameworks
http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks
•PHPUnit
•SimpleTest
•Google C++ Testing Framework
•CppUnitLite
•CppUnit
•JUnit
•TestNG
•PyUnit
•Nose

Más contenido relacionado

La actualidad más candente

15 tips to improve your unit tests (Droidcon Berlin 2016 Barcamp)
15 tips to improve your unit tests (Droidcon Berlin 2016 Barcamp)15 tips to improve your unit tests (Droidcon Berlin 2016 Barcamp)
15 tips to improve your unit tests (Droidcon Berlin 2016 Barcamp)Danny Preussler
 
Automated testing in Python and beyond
Automated testing in Python and beyondAutomated testing in Python and beyond
Automated testing in Python and beyonddn
 
Tomasz Polanski - Automated mobile testing 2016 - Testing: why, when, how
Tomasz Polanski - Automated mobile testing 2016 - Testing: why, when, howTomasz Polanski - Automated mobile testing 2016 - Testing: why, when, how
Tomasz Polanski - Automated mobile testing 2016 - Testing: why, when, howTomasz Polanski
 
Python-nose: A unittest-based testing framework for Python that makes writing...
Python-nose: A unittest-based testing framework for Python that makes writing...Python-nose: A unittest-based testing framework for Python that makes writing...
Python-nose: A unittest-based testing framework for Python that makes writing...Timo Stollenwerk
 
MUTANTS KILLER (Revised) - PIT: state of the art of mutation testing system
MUTANTS KILLER (Revised) - PIT: state of the art of mutation testing system MUTANTS KILLER (Revised) - PIT: state of the art of mutation testing system
MUTANTS KILLER (Revised) - PIT: state of the art of mutation testing system Tarin Gamberini
 
JUnit & Mockito, first steps
JUnit & Mockito, first stepsJUnit & Mockito, first steps
JUnit & Mockito, first stepsRenato Primavera
 
Advanced junit and mockito
Advanced junit and mockitoAdvanced junit and mockito
Advanced junit and mockitoMathieu Carbou
 
unittest in 5 minutes
unittest in 5 minutesunittest in 5 minutes
unittest in 5 minutesRay Toal
 
Javaone2008 Bof 5101 Groovytesting
Javaone2008 Bof 5101 GroovytestingJavaone2008 Bof 5101 Groovytesting
Javaone2008 Bof 5101 GroovytestingAndres Almiray
 
Unit Testing Presentation
Unit Testing PresentationUnit Testing Presentation
Unit Testing Presentationnicobn
 
Google mock for dummies
Google mock for dummiesGoogle mock for dummies
Google mock for dummiesHarry Potter
 
Python Unit Test
Python Unit TestPython Unit Test
Python Unit TestDavid Xie
 
Python Testing Fundamentals
Python Testing FundamentalsPython Testing Fundamentals
Python Testing Fundamentalscbcunc
 

La actualidad más candente (19)

15 tips to improve your unit tests (Droidcon Berlin 2016 Barcamp)
15 tips to improve your unit tests (Droidcon Berlin 2016 Barcamp)15 tips to improve your unit tests (Droidcon Berlin 2016 Barcamp)
15 tips to improve your unit tests (Droidcon Berlin 2016 Barcamp)
 
Automated testing in Python and beyond
Automated testing in Python and beyondAutomated testing in Python and beyond
Automated testing in Python and beyond
 
Golang dot-testing-lite
Golang dot-testing-liteGolang dot-testing-lite
Golang dot-testing-lite
 
Tomasz Polanski - Automated mobile testing 2016 - Testing: why, when, how
Tomasz Polanski - Automated mobile testing 2016 - Testing: why, when, howTomasz Polanski - Automated mobile testing 2016 - Testing: why, when, how
Tomasz Polanski - Automated mobile testing 2016 - Testing: why, when, how
 
Modern Python Testing
Modern Python TestingModern Python Testing
Modern Python Testing
 
Python unittest
Python unittestPython unittest
Python unittest
 
JUnit Pioneer
JUnit PioneerJUnit Pioneer
JUnit Pioneer
 
Python-nose: A unittest-based testing framework for Python that makes writing...
Python-nose: A unittest-based testing framework for Python that makes writing...Python-nose: A unittest-based testing framework for Python that makes writing...
Python-nose: A unittest-based testing framework for Python that makes writing...
 
MUTANTS KILLER (Revised) - PIT: state of the art of mutation testing system
MUTANTS KILLER (Revised) - PIT: state of the art of mutation testing system MUTANTS KILLER (Revised) - PIT: state of the art of mutation testing system
MUTANTS KILLER (Revised) - PIT: state of the art of mutation testing system
 
Pyunit
PyunitPyunit
Pyunit
 
JUnit & Mockito, first steps
JUnit & Mockito, first stepsJUnit & Mockito, first steps
JUnit & Mockito, first steps
 
Advanced junit and mockito
Advanced junit and mockitoAdvanced junit and mockito
Advanced junit and mockito
 
unittest in 5 minutes
unittest in 5 minutesunittest in 5 minutes
unittest in 5 minutes
 
Javaone2008 Bof 5101 Groovytesting
Javaone2008 Bof 5101 GroovytestingJavaone2008 Bof 5101 Groovytesting
Javaone2008 Bof 5101 Groovytesting
 
JUnit 5
JUnit 5JUnit 5
JUnit 5
 
Unit Testing Presentation
Unit Testing PresentationUnit Testing Presentation
Unit Testing Presentation
 
Google mock for dummies
Google mock for dummiesGoogle mock for dummies
Google mock for dummies
 
Python Unit Test
Python Unit TestPython Unit Test
Python Unit Test
 
Python Testing Fundamentals
Python Testing FundamentalsPython Testing Fundamentals
Python Testing Fundamentals
 

Destacado

Tutti i miei sbagli (Errori di un wannabe Open Source Developer)
Tutti i miei sbagli (Errori di un wannabe Open Source Developer)Tutti i miei sbagli (Errori di un wannabe Open Source Developer)
Tutti i miei sbagli (Errori di un wannabe Open Source Developer)Andrea Francia
 
Piccolo coding dojo (milano xpug 2013-04-11)
Piccolo coding dojo (milano xpug 2013-04-11)Piccolo coding dojo (milano xpug 2013-04-11)
Piccolo coding dojo (milano xpug 2013-04-11)Andrea Francia
 
Subversion @ JUG Milano 11 dic 2009
Subversion @ JUG Milano 11 dic 2009Subversion @ JUG Milano 11 dic 2009
Subversion @ JUG Milano 11 dic 2009Andrea Francia
 
Tutti i miei sbagli, versione 7 Marzo 2012 al XPUG mi
Tutti i miei sbagli, versione 7 Marzo 2012 al XPUG miTutti i miei sbagli, versione 7 Marzo 2012 al XPUG mi
Tutti i miei sbagli, versione 7 Marzo 2012 al XPUG miAndrea Francia
 
Working Effectively with Legacy Code (draft)
Working Effectively with Legacy Code (draft)Working Effectively with Legacy Code (draft)
Working Effectively with Legacy Code (draft)Andrea Francia
 
Writing a Crawler with Python and TDD
Writing a Crawler with Python and TDDWriting a Crawler with Python and TDD
Writing a Crawler with Python and TDDAndrea Francia
 
20070514 introduction to test ng and its application for test driven gui deve...
20070514 introduction to test ng and its application for test driven gui deve...20070514 introduction to test ng and its application for test driven gui deve...
20070514 introduction to test ng and its application for test driven gui deve...Will Shen
 
Bade Smells in Code
Bade Smells in CodeBade Smells in Code
Bade Smells in CodeWill Shen
 
Monogdb in general
Monogdb in generalMonogdb in general
Monogdb in generalIdo Kanner
 
Google mock for dummies
Google mock for dummiesGoogle mock for dummies
Google mock for dummiesLuis Goldster
 

Destacado (12)

Tutti i miei sbagli (Errori di un wannabe Open Source Developer)
Tutti i miei sbagli (Errori di un wannabe Open Source Developer)Tutti i miei sbagli (Errori di un wannabe Open Source Developer)
Tutti i miei sbagli (Errori di un wannabe Open Source Developer)
 
Piccolo coding dojo (milano xpug 2013-04-11)
Piccolo coding dojo (milano xpug 2013-04-11)Piccolo coding dojo (milano xpug 2013-04-11)
Piccolo coding dojo (milano xpug 2013-04-11)
 
Subversion @ JUG Milano 11 dic 2009
Subversion @ JUG Milano 11 dic 2009Subversion @ JUG Milano 11 dic 2009
Subversion @ JUG Milano 11 dic 2009
 
Tutti i miei sbagli, versione 7 Marzo 2012 al XPUG mi
Tutti i miei sbagli, versione 7 Marzo 2012 al XPUG miTutti i miei sbagli, versione 7 Marzo 2012 al XPUG mi
Tutti i miei sbagli, versione 7 Marzo 2012 al XPUG mi
 
Working Effectively with Legacy Code (draft)
Working Effectively with Legacy Code (draft)Working Effectively with Legacy Code (draft)
Working Effectively with Legacy Code (draft)
 
Le 12 pratiche
Le 12 praticheLe 12 pratiche
Le 12 pratiche
 
Writing a Crawler with Python and TDD
Writing a Crawler with Python and TDDWriting a Crawler with Python and TDD
Writing a Crawler with Python and TDD
 
20070514 introduction to test ng and its application for test driven gui deve...
20070514 introduction to test ng and its application for test driven gui deve...20070514 introduction to test ng and its application for test driven gui deve...
20070514 introduction to test ng and its application for test driven gui deve...
 
Bade Smells in Code
Bade Smells in CodeBade Smells in Code
Bade Smells in Code
 
Monogdb in general
Monogdb in generalMonogdb in general
Monogdb in general
 
Google mock for dummies
Google mock for dummiesGoogle mock for dummies
Google mock for dummies
 
TDD anche su iOS
TDD anche su iOSTDD anche su iOS
TDD anche su iOS
 

Similar a Introduzione al TDD

Appium TestNG Framework and Multi-Device Automation Execution
Appium TestNG Framework and Multi-Device Automation ExecutionAppium TestNG Framework and Multi-Device Automation Execution
Appium TestNG Framework and Multi-Device Automation ExecutionpCloudy
 
33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good Tests33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good TestsTomek Kaczanowski
 
2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good Tests2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good TestsTomek Kaczanowski
 
16. Java stacks and queues
16. Java stacks and queues16. Java stacks and queues
16. Java stacks and queuesIntro C# Book
 
Learning Java 1 – Introduction
Learning Java 1 – IntroductionLearning Java 1 – Introduction
Learning Java 1 – Introductioncaswenson
 
Lambda Chops - Recipes for Simpler, More Expressive Code
Lambda Chops - Recipes for Simpler, More Expressive CodeLambda Chops - Recipes for Simpler, More Expressive Code
Lambda Chops - Recipes for Simpler, More Expressive CodeIan Robertson
 
Some testing - Everything you should know about testing to go with @pedro_g_s...
Some testing - Everything you should know about testing to go with @pedro_g_s...Some testing - Everything you should know about testing to go with @pedro_g_s...
Some testing - Everything you should know about testing to go with @pedro_g_s...Sergio Arroyo
 
Grails unit testing
Grails unit testingGrails unit testing
Grails unit testingpleeps
 
Confitura 2012 Bad Tests, Good Tests
Confitura 2012 Bad Tests, Good TestsConfitura 2012 Bad Tests, Good Tests
Confitura 2012 Bad Tests, Good TestsTomek Kaczanowski
 
GeeCON 2012 Bad Tests, Good Tests
GeeCON 2012 Bad Tests, Good TestsGeeCON 2012 Bad Tests, Good Tests
GeeCON 2012 Bad Tests, Good TestsTomek Kaczanowski
 

Similar a Introduzione al TDD (20)

Appium TestNG Framework and Multi-Device Automation Execution
Appium TestNG Framework and Multi-Device Automation ExecutionAppium TestNG Framework and Multi-Device Automation Execution
Appium TestNG Framework and Multi-Device Automation Execution
 
3 j unit
3 j unit3 j unit
3 j unit
 
33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good Tests33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good Tests
 
Java practical
Java practicalJava practical
Java practical
 
2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good Tests2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good Tests
 
16. Java stacks and queues
16. Java stacks and queues16. Java stacks and queues
16. Java stacks and queues
 
Good Practices On Test Automation
Good Practices On Test AutomationGood Practices On Test Automation
Good Practices On Test Automation
 
Learning Java 1 – Introduction
Learning Java 1 – IntroductionLearning Java 1 – Introduction
Learning Java 1 – Introduction
 
J Unit
J UnitJ Unit
J Unit
 
Java programs
Java programsJava programs
Java programs
 
Lambda Chops - Recipes for Simpler, More Expressive Code
Lambda Chops - Recipes for Simpler, More Expressive CodeLambda Chops - Recipes for Simpler, More Expressive Code
Lambda Chops - Recipes for Simpler, More Expressive Code
 
Some testing - Everything you should know about testing to go with @pedro_g_s...
Some testing - Everything you should know about testing to go with @pedro_g_s...Some testing - Everything you should know about testing to go with @pedro_g_s...
Some testing - Everything you should know about testing to go with @pedro_g_s...
 
Grails unit testing
Grails unit testingGrails unit testing
Grails unit testing
 
srgoc
srgocsrgoc
srgoc
 
Confitura 2012 Bad Tests, Good Tests
Confitura 2012 Bad Tests, Good TestsConfitura 2012 Bad Tests, Good Tests
Confitura 2012 Bad Tests, Good Tests
 
Unit testing - A&BP CC
Unit testing - A&BP CCUnit testing - A&BP CC
Unit testing - A&BP CC
 
Java programs
Java programsJava programs
Java programs
 
GeeCON 2012 Bad Tests, Good Tests
GeeCON 2012 Bad Tests, Good TestsGeeCON 2012 Bad Tests, Good Tests
GeeCON 2012 Bad Tests, Good Tests
 
Lab4
Lab4Lab4
Lab4
 
02 - Prepcode
02 - Prepcode02 - Prepcode
02 - Prepcode
 

Más de Andrea Francia

Baby Steps TripServiceKata
Baby Steps TripServiceKataBaby Steps TripServiceKata
Baby Steps TripServiceKataAndrea Francia
 
TDD on Legacy Code - Voxxed Days Milano 2019
TDD on Legacy Code - Voxxed Days Milano 2019TDD on Legacy Code - Voxxed Days Milano 2019
TDD on Legacy Code - Voxxed Days Milano 2019Andrea Francia
 
Lavorare con codice legacy “non testabile” - Incontro DevOps - 8 marzo 2019 -...
Lavorare con codice legacy “non testabile” - Incontro DevOps - 8 marzo 2019 -...Lavorare con codice legacy “non testabile” - Incontro DevOps - 8 marzo 2019 -...
Lavorare con codice legacy “non testabile” - Incontro DevOps - 8 marzo 2019 -...Andrea Francia
 
Kata in Bash a DevOpsHeroes 2018 a Parma
Kata in Bash a DevOpsHeroes 2018 a ParmaKata in Bash a DevOpsHeroes 2018 a Parma
Kata in Bash a DevOpsHeroes 2018 a ParmaAndrea Francia
 
User Stories - Andrea Francia @ WeDev 7 novembre 2018
User Stories - Andrea Francia @ WeDev 7 novembre 2018User Stories - Andrea Francia @ WeDev 7 novembre 2018
User Stories - Andrea Francia @ WeDev 7 novembre 2018Andrea Francia
 
Le pratiche ingegneristiche di eXtreme Programming
Le pratiche ingegneristiche di eXtreme ProgrammingLe pratiche ingegneristiche di eXtreme Programming
Le pratiche ingegneristiche di eXtreme ProgrammingAndrea Francia
 
Test-Driven Development su Codice Esistente
Test-Driven Development su Codice EsistenteTest-Driven Development su Codice Esistente
Test-Driven Development su Codice EsistenteAndrea Francia
 
Le 12 pratiche - Un introduzione a XP (Mini Italian Agile Day)
Le 12 pratiche - Un introduzione a XP (Mini Italian Agile Day)Le 12 pratiche - Un introduzione a XP (Mini Italian Agile Day)
Le 12 pratiche - Un introduzione a XP (Mini Italian Agile Day)Andrea Francia
 
Introduzione a eXtreme Programming
Introduzione a eXtreme ProgrammingIntroduzione a eXtreme Programming
Introduzione a eXtreme ProgrammingAndrea Francia
 
Test-Driven Development e Sviluppo Incrementale (TDD-Milano 2017-01-10)
Test-Driven Development e Sviluppo Incrementale (TDD-Milano 2017-01-10)Test-Driven Development e Sviluppo Incrementale (TDD-Milano 2017-01-10)
Test-Driven Development e Sviluppo Incrementale (TDD-Milano 2017-01-10)Andrea Francia
 

Más de Andrea Francia (12)

Baby Steps TripServiceKata
Baby Steps TripServiceKataBaby Steps TripServiceKata
Baby Steps TripServiceKata
 
TDD on Legacy Code - Voxxed Days Milano 2019
TDD on Legacy Code - Voxxed Days Milano 2019TDD on Legacy Code - Voxxed Days Milano 2019
TDD on Legacy Code - Voxxed Days Milano 2019
 
Lavorare con codice legacy “non testabile” - Incontro DevOps - 8 marzo 2019 -...
Lavorare con codice legacy “non testabile” - Incontro DevOps - 8 marzo 2019 -...Lavorare con codice legacy “non testabile” - Incontro DevOps - 8 marzo 2019 -...
Lavorare con codice legacy “non testabile” - Incontro DevOps - 8 marzo 2019 -...
 
Kata in Bash a DevOpsHeroes 2018 a Parma
Kata in Bash a DevOpsHeroes 2018 a ParmaKata in Bash a DevOpsHeroes 2018 a Parma
Kata in Bash a DevOpsHeroes 2018 a Parma
 
User Stories - Andrea Francia @ WeDev 7 novembre 2018
User Stories - Andrea Francia @ WeDev 7 novembre 2018User Stories - Andrea Francia @ WeDev 7 novembre 2018
User Stories - Andrea Francia @ WeDev 7 novembre 2018
 
Le pratiche ingegneristiche di eXtreme Programming
Le pratiche ingegneristiche di eXtreme ProgrammingLe pratiche ingegneristiche di eXtreme Programming
Le pratiche ingegneristiche di eXtreme Programming
 
Test-Driven Development su Codice Esistente
Test-Driven Development su Codice EsistenteTest-Driven Development su Codice Esistente
Test-Driven Development su Codice Esistente
 
Come si applica l'OCP
Come si applica l'OCPCome si applica l'OCP
Come si applica l'OCP
 
Le 12 pratiche - Un introduzione a XP (Mini Italian Agile Day)
Le 12 pratiche - Un introduzione a XP (Mini Italian Agile Day)Le 12 pratiche - Un introduzione a XP (Mini Italian Agile Day)
Le 12 pratiche - Un introduzione a XP (Mini Italian Agile Day)
 
Introduzione a eXtreme Programming
Introduzione a eXtreme ProgrammingIntroduzione a eXtreme Programming
Introduzione a eXtreme Programming
 
Test-Driven Development e Sviluppo Incrementale (TDD-Milano 2017-01-10)
Test-Driven Development e Sviluppo Incrementale (TDD-Milano 2017-01-10)Test-Driven Development e Sviluppo Incrementale (TDD-Milano 2017-01-10)
Test-Driven Development e Sviluppo Incrementale (TDD-Milano 2017-01-10)
 
Bash-Only Deployment
Bash-Only DeploymentBash-Only Deployment
Bash-Only Deployment
 

Último

Cheap Rate Call girls Kalkaji 9205541914 shot 1500 night
Cheap Rate Call girls Kalkaji 9205541914 shot 1500 nightCheap Rate Call girls Kalkaji 9205541914 shot 1500 night
Cheap Rate Call girls Kalkaji 9205541914 shot 1500 nightDelhi Call girls
 
VIP Kolkata Call Girl Gariahat 👉 8250192130 Available With Room
VIP Kolkata Call Girl Gariahat 👉 8250192130  Available With RoomVIP Kolkata Call Girl Gariahat 👉 8250192130  Available With Room
VIP Kolkata Call Girl Gariahat 👉 8250192130 Available With Roomdivyansh0kumar0
 
Recommendable # 971589162217 # philippine Young Call Girls in Dubai By Marina...
Recommendable # 971589162217 # philippine Young Call Girls in Dubai By Marina...Recommendable # 971589162217 # philippine Young Call Girls in Dubai By Marina...
Recommendable # 971589162217 # philippine Young Call Girls in Dubai By Marina...home
 
Verified Trusted Call Girls Adugodi💘 9352852248 Good Looking standard Profil...
Verified Trusted Call Girls Adugodi💘 9352852248  Good Looking standard Profil...Verified Trusted Call Girls Adugodi💘 9352852248  Good Looking standard Profil...
Verified Trusted Call Girls Adugodi💘 9352852248 Good Looking standard Profil...kumaririma588
 
VIP Call Girls Bhiwandi Ananya 8250192130 Independent Escort Service Bhiwandi
VIP Call Girls Bhiwandi Ananya 8250192130 Independent Escort Service BhiwandiVIP Call Girls Bhiwandi Ananya 8250192130 Independent Escort Service Bhiwandi
VIP Call Girls Bhiwandi Ananya 8250192130 Independent Escort Service BhiwandiSuhani Kapoor
 
Peaches App development presentation deck
Peaches App development presentation deckPeaches App development presentation deck
Peaches App development presentation decktbatkhuu1
 
Best VIP Call Girls Noida Sector 47 Call Me: 8448380779
Best VIP Call Girls Noida Sector 47 Call Me: 8448380779Best VIP Call Girls Noida Sector 47 Call Me: 8448380779
Best VIP Call Girls Noida Sector 47 Call Me: 8448380779Delhi Call girls
 
VIP Russian Call Girls in Saharanpur Deepika 8250192130 Independent Escort Se...
VIP Russian Call Girls in Saharanpur Deepika 8250192130 Independent Escort Se...VIP Russian Call Girls in Saharanpur Deepika 8250192130 Independent Escort Se...
VIP Russian Call Girls in Saharanpur Deepika 8250192130 Independent Escort Se...Suhani Kapoor
 
VIP Call Girls Service Mehdipatnam Hyderabad Call +91-8250192130
VIP Call Girls Service Mehdipatnam Hyderabad Call +91-8250192130VIP Call Girls Service Mehdipatnam Hyderabad Call +91-8250192130
VIP Call Girls Service Mehdipatnam Hyderabad Call +91-8250192130Suhani Kapoor
 
(AISHA) Ambegaon Khurd Call Girls Just Call 7001035870 [ Cash on Delivery ] P...
(AISHA) Ambegaon Khurd Call Girls Just Call 7001035870 [ Cash on Delivery ] P...(AISHA) Ambegaon Khurd Call Girls Just Call 7001035870 [ Cash on Delivery ] P...
(AISHA) Ambegaon Khurd Call Girls Just Call 7001035870 [ Cash on Delivery ] P...ranjana rawat
 
Cheap Rate Call girls Malviya Nagar 9205541914 shot 1500 night
Cheap Rate Call girls Malviya Nagar 9205541914 shot 1500 nightCheap Rate Call girls Malviya Nagar 9205541914 shot 1500 night
Cheap Rate Call girls Malviya Nagar 9205541914 shot 1500 nightDelhi Call girls
 
Editorial design Magazine design project.pdf
Editorial design Magazine design project.pdfEditorial design Magazine design project.pdf
Editorial design Magazine design project.pdftbatkhuu1
 
The_Canvas_of_Creative_Mastery_Newsletter_April_2024_Version.pdf
The_Canvas_of_Creative_Mastery_Newsletter_April_2024_Version.pdfThe_Canvas_of_Creative_Mastery_Newsletter_April_2024_Version.pdf
The_Canvas_of_Creative_Mastery_Newsletter_April_2024_Version.pdfAmirYakdi
 
VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...
VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...
VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...Call Girls in Nagpur High Profile
 
Top Rated Pune Call Girls Koregaon Park ⟟ 6297143586 ⟟ Call Me For Genuine S...
Top Rated  Pune Call Girls Koregaon Park ⟟ 6297143586 ⟟ Call Me For Genuine S...Top Rated  Pune Call Girls Koregaon Park ⟟ 6297143586 ⟟ Call Me For Genuine S...
Top Rated Pune Call Girls Koregaon Park ⟟ 6297143586 ⟟ Call Me For Genuine S...Call Girls in Nagpur High Profile
 
VIP Call Girls Service Bhagyanagar Hyderabad Call +91-8250192130
VIP Call Girls Service Bhagyanagar Hyderabad Call +91-8250192130VIP Call Girls Service Bhagyanagar Hyderabad Call +91-8250192130
VIP Call Girls Service Bhagyanagar Hyderabad Call +91-8250192130Suhani Kapoor
 
AMBER GRAIN EMBROIDERY | Growing folklore elements | Root-based materials, w...
AMBER GRAIN EMBROIDERY | Growing folklore elements |  Root-based materials, w...AMBER GRAIN EMBROIDERY | Growing folklore elements |  Root-based materials, w...
AMBER GRAIN EMBROIDERY | Growing folklore elements | Root-based materials, w...BarusRa
 
Kurla Call Girls Pooja Nehwal📞 9892124323 ✅ Vashi Call Service Available Nea...
Kurla Call Girls Pooja Nehwal📞 9892124323 ✅  Vashi Call Service Available Nea...Kurla Call Girls Pooja Nehwal📞 9892124323 ✅  Vashi Call Service Available Nea...
Kurla Call Girls Pooja Nehwal📞 9892124323 ✅ Vashi Call Service Available Nea...Pooja Nehwal
 

Último (20)

Cheap Rate Call girls Kalkaji 9205541914 shot 1500 night
Cheap Rate Call girls Kalkaji 9205541914 shot 1500 nightCheap Rate Call girls Kalkaji 9205541914 shot 1500 night
Cheap Rate Call girls Kalkaji 9205541914 shot 1500 night
 
escort service sasti (*~Call Girls in Prasad Nagar Metro❤️9953056974
escort service sasti (*~Call Girls in Prasad Nagar Metro❤️9953056974escort service sasti (*~Call Girls in Prasad Nagar Metro❤️9953056974
escort service sasti (*~Call Girls in Prasad Nagar Metro❤️9953056974
 
young call girls in Pandav nagar 🔝 9953056974 🔝 Delhi escort Service
young call girls in Pandav nagar 🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Pandav nagar 🔝 9953056974 🔝 Delhi escort Service
young call girls in Pandav nagar 🔝 9953056974 🔝 Delhi escort Service
 
VIP Kolkata Call Girl Gariahat 👉 8250192130 Available With Room
VIP Kolkata Call Girl Gariahat 👉 8250192130  Available With RoomVIP Kolkata Call Girl Gariahat 👉 8250192130  Available With Room
VIP Kolkata Call Girl Gariahat 👉 8250192130 Available With Room
 
Recommendable # 971589162217 # philippine Young Call Girls in Dubai By Marina...
Recommendable # 971589162217 # philippine Young Call Girls in Dubai By Marina...Recommendable # 971589162217 # philippine Young Call Girls in Dubai By Marina...
Recommendable # 971589162217 # philippine Young Call Girls in Dubai By Marina...
 
Verified Trusted Call Girls Adugodi💘 9352852248 Good Looking standard Profil...
Verified Trusted Call Girls Adugodi💘 9352852248  Good Looking standard Profil...Verified Trusted Call Girls Adugodi💘 9352852248  Good Looking standard Profil...
Verified Trusted Call Girls Adugodi💘 9352852248 Good Looking standard Profil...
 
VIP Call Girls Bhiwandi Ananya 8250192130 Independent Escort Service Bhiwandi
VIP Call Girls Bhiwandi Ananya 8250192130 Independent Escort Service BhiwandiVIP Call Girls Bhiwandi Ananya 8250192130 Independent Escort Service Bhiwandi
VIP Call Girls Bhiwandi Ananya 8250192130 Independent Escort Service Bhiwandi
 
Peaches App development presentation deck
Peaches App development presentation deckPeaches App development presentation deck
Peaches App development presentation deck
 
Best VIP Call Girls Noida Sector 47 Call Me: 8448380779
Best VIP Call Girls Noida Sector 47 Call Me: 8448380779Best VIP Call Girls Noida Sector 47 Call Me: 8448380779
Best VIP Call Girls Noida Sector 47 Call Me: 8448380779
 
VIP Russian Call Girls in Saharanpur Deepika 8250192130 Independent Escort Se...
VIP Russian Call Girls in Saharanpur Deepika 8250192130 Independent Escort Se...VIP Russian Call Girls in Saharanpur Deepika 8250192130 Independent Escort Se...
VIP Russian Call Girls in Saharanpur Deepika 8250192130 Independent Escort Se...
 
VIP Call Girls Service Mehdipatnam Hyderabad Call +91-8250192130
VIP Call Girls Service Mehdipatnam Hyderabad Call +91-8250192130VIP Call Girls Service Mehdipatnam Hyderabad Call +91-8250192130
VIP Call Girls Service Mehdipatnam Hyderabad Call +91-8250192130
 
(AISHA) Ambegaon Khurd Call Girls Just Call 7001035870 [ Cash on Delivery ] P...
(AISHA) Ambegaon Khurd Call Girls Just Call 7001035870 [ Cash on Delivery ] P...(AISHA) Ambegaon Khurd Call Girls Just Call 7001035870 [ Cash on Delivery ] P...
(AISHA) Ambegaon Khurd Call Girls Just Call 7001035870 [ Cash on Delivery ] P...
 
Cheap Rate Call girls Malviya Nagar 9205541914 shot 1500 night
Cheap Rate Call girls Malviya Nagar 9205541914 shot 1500 nightCheap Rate Call girls Malviya Nagar 9205541914 shot 1500 night
Cheap Rate Call girls Malviya Nagar 9205541914 shot 1500 night
 
Editorial design Magazine design project.pdf
Editorial design Magazine design project.pdfEditorial design Magazine design project.pdf
Editorial design Magazine design project.pdf
 
The_Canvas_of_Creative_Mastery_Newsletter_April_2024_Version.pdf
The_Canvas_of_Creative_Mastery_Newsletter_April_2024_Version.pdfThe_Canvas_of_Creative_Mastery_Newsletter_April_2024_Version.pdf
The_Canvas_of_Creative_Mastery_Newsletter_April_2024_Version.pdf
 
VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...
VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...
VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...
 
Top Rated Pune Call Girls Koregaon Park ⟟ 6297143586 ⟟ Call Me For Genuine S...
Top Rated  Pune Call Girls Koregaon Park ⟟ 6297143586 ⟟ Call Me For Genuine S...Top Rated  Pune Call Girls Koregaon Park ⟟ 6297143586 ⟟ Call Me For Genuine S...
Top Rated Pune Call Girls Koregaon Park ⟟ 6297143586 ⟟ Call Me For Genuine S...
 
VIP Call Girls Service Bhagyanagar Hyderabad Call +91-8250192130
VIP Call Girls Service Bhagyanagar Hyderabad Call +91-8250192130VIP Call Girls Service Bhagyanagar Hyderabad Call +91-8250192130
VIP Call Girls Service Bhagyanagar Hyderabad Call +91-8250192130
 
AMBER GRAIN EMBROIDERY | Growing folklore elements | Root-based materials, w...
AMBER GRAIN EMBROIDERY | Growing folklore elements |  Root-based materials, w...AMBER GRAIN EMBROIDERY | Growing folklore elements |  Root-based materials, w...
AMBER GRAIN EMBROIDERY | Growing folklore elements | Root-based materials, w...
 
Kurla Call Girls Pooja Nehwal📞 9892124323 ✅ Vashi Call Service Available Nea...
Kurla Call Girls Pooja Nehwal📞 9892124323 ✅  Vashi Call Service Available Nea...Kurla Call Girls Pooja Nehwal📞 9892124323 ✅  Vashi Call Service Available Nea...
Kurla Call Girls Pooja Nehwal📞 9892124323 ✅ Vashi Call Service Available Nea...
 

Introduzione al TDD

  • 1. Introduzione al Test Driven Development Andrea Francia http://www.andreafrancia.it
  • 4. What is a Automated Test?
  • 5. Automated Test Lifecycle Giordano Scalzo: http://www.slideshare.net/giordano/tdd-iphonefordummies
  • 6. Automated Test Lifecycle Giordano Scalzo: http://www.slideshare.net/giordano/tdd-iphonefordummies
  • 7. Automated Test Lifecycle SUT: System Under Test Giordano Scalzo: http://www.slideshare.net/giordano/tdd-iphonefordummies
  • 8. Automated Test Lifecycle SUT: System Under Test Giordano Scalzo: http://www.slideshare.net/giordano/tdd-iphonefordummies
  • 9. Automated Test Lifecycle SUT: System Under Test Giordano Scalzo: http://www.slideshare.net/giordano/tdd-iphonefordummies
  • 10. Example of a Manual Test: nextLine() public static void main(String[] args) { String text = "first linensecond line"; Scanner scanner = new Scanner(text); System.out.println(scanner.nextLine()); // prints "first line" System.out.println(scanner.nextLine()); // prints "second line” } Code:
  • 12. Using JUnit @Test public void howNextLineWorks() throws IOException { String text = "first linensecond line"; Scanner scanner = new Scanner(text); assertEquals(”first line", scanner.nextLine()); assertEquals(”second line", scanner.nextLine()); } Code: Output:
  • 13. What is Test Driven Development?
  • 14. What is TDD? Test-driven development (TDD) is a software development process that relies on the repetition of a very short development cycle: Giordano Scalzo: http://www.slideshare.net/giordano/tdd-iphonefordummies Tim Ottinger:http://agileinaflash.blogspot.com/2009/02/red-green-refactor.html
  • 15. RED first the developer writes a failing automated test case that defines a desired new behaviour (of the software),
  • 16. GREEN then produces code to pass that test …
  • 17. Refactor and finally refactors the new code to acceptable standards.
  • 18.
  • 20. The df output [andreafrancia@deneb Dropbox]$ df Filesystem 1K-blocks Used Available Use% Mounted on /dev/disk0s2 243862672 135971832 107634840 56% / /dev/disk0s1 243862672 135971832 107634840 56% /tmp /dev/disk1s2 243862672 135971832 107634840 56% /opt devfs 109 109 0 100% /dev Mount Points
  • 21. The problem List all mount points
  • 22. The Problem Write a method that extract the list of the mount points from the output of df.
  • 23. First of all decompose the problem!
  • 24. Decomposition Parse the output of “df” process … When there are no mounted volumes When there is only one volume mounted When there are many volumes When the a volume contains whitespaces
  • 25. First Cycle Parse the output of “df” process When there are no volumes When there is only one volume When there are many volumes When the a volume contains whitespaces
  • 26. @Test public void whenNoVolumes() { String input = "Filesystem 1024-blocks Used Available Capacity Mounted on”; List<String> result = parseDfOutput(input); assertEquals(emptyList(), result); }
  • 27.
  • 28. @Test public void whenNoVolumes() { String input = "Filesystem 1024-blocks Used Available Capacity Mounted on”; List<String> result = parseDfOutput(input); assertEquals(emptyList(), result); } private List<String> parseDfOutput(String string) { return null; }
  • 29. @Test public void whenNoVolumes() { String input = "Filesystem 1024-blocks Used Available Capacity Mounted on”; List<String> result = parseDfOutput(input); assertEquals(emptyList(), result); } private List<String> parseDfOutput(String string) { return emptyList(); }
  • 30. No need for refactoring
  • 31. Second Cycle Parse the output of “df” process When there are no volumes When there is only one volume When there are many volumes When the a volume contains whitespaces
  • 32. @Test public void whenOneVolume() { List<String> result = parseDfOutput("" + "Filesystem 1024-blocks Used Available Capacity Mounted onn" + "/dev/disk0s2 243862672 135479924 108126748 56% /"); assertEquals(asList("/"), result); }
  • 33. private List<String> parseDfOutput(String string) { return emptyList(); }
  • 34. private List<String> parseDfOutput(String string) { List<String> result = new ArrayList<String>(); return result; }
  • 35. private List<String> parseDfOutput(String input) { List<String> result = new ArrayList<String>(); Scanner scanner = new Scanner(input); scanner.nextLine(); // skip header if(scanner.hasNextLine()) { String line = scanner.nextLine(); Scanner scanner1 = new Scanner(line); scanner1.next(); // skip Filesystem scanner1.next(); // skip 1024-blocks scanner1.next(); // skip Used scanner1.next(); // skip Available scanner1.next(); // skip Capacity String mountPoint = scanner1.next(); result.add(mountPoint); } return result; }
  • 36. private List<String> parseDfOutput(String input) { List<String> result = new ArrayList<String>(); Scanner lines = new Scanner(input); lines.nextLine(); // skip header if(lines.hasNextLine()) { String line = lines.nextLine(); Scanner values = new Scanner(line); values.next(); // skip Filesystem values.next(); // skip 1024-blocks values.next(); // skip Used values.next(); // skip Available values.next(); // skip Capacity String mountPoint = values.next(); result.add(mountPoint); } return result; }
  • 37. private List<String> parseDfOutput(String input) { List<String> result = new ArrayList<String>(); Scanner lines = new Scanner(input); lines.nextLine(); // skip header if(lines.hasNextLine()) { result.add(parseMountPoint(lines.nextLine())); } return result; } private String parseMountPoint(String line) { Scanner values = new Scanner(line); values.next(); // skip Filesystem values.next(); // skip 1024-blocks values.next(); // skip Used values.next(); // skip Available values.next(); // skip Capacity String mountPoint = values.next(); return mountPoint; }
  • 38. Third Cycle Parse the output of “df” process When there are no volumes When there is only one volume When there are many volumes When the a volume contains whitespaces
  • 39. @Test public void whenMultipleVolume() { List<String> result = parseDfOutput("" + "Filesystem 1024-blocks Used Available Capacity Mounted onn" + "/dev/disk0s2 243862672 135479924 108126748 56% /n" + "/dev/disk0s2 243862672 135479924 108126748 56% /media/diskn" + "/dev/disk0s2 243862672 135479924 108126748 56% /tmpn"); assertEquals(asList("/", "/media/disk", "/tmp"), result); }
  • 40. @Test public void whenMultipleVolume() { List<String> result = parseDfOutput("" + "Filesystem 1024-blocks Used Available Capacity Mounted onn" + "/dev/disk0s2 243862672 135479924 108126748 56% /n" + "/dev/disk0s2 243862672 135479924 108126748 56% /media/diskn" + "/dev/disk0s2 243862672 135479924 108126748 56% /tmpn"); assertEquals(asList("/", "/media/disk", "/tmp"), result); }
  • 41. private List<String> parseDfOutput(String input) { List<String> result = new ArrayList<String>(); Scanner lines = new Scanner(input); lines.nextLine(); // skip header if(lines.hasNextLine()) { String line = lines.nextLine(); String mountPoint = parseMountPoint(line); result.add(mountPoint); } return result; }
  • 42. private List<String> parseDfOutput(String input) { List<String> result = new ArrayList<String>(); Scanner lines = new Scanner(input); lines.nextLine(); // skip header while(lines.hasNextLine()) { String line = lines.nextLine(); String mountPoint = parseMountPoint(line); result.add(mountPoint); } return result; }
  • 44. TDD Rules Test First Test for All Features Remove all duplication (always)
  • 45. Some hints Keep tests running and passing Keeps test small Don’t mix phases (Red, Green, Refactor) Don’t mix unit tests with integration tests Test only one feature per test
  • 46. Examples from my last work
  • 47. Regole applicate Ogni feature deve essere sviluppata secondo il TDD Partire dai test di accettazione Ritardare le decisioni di design all’ultimo momento responsabile Applicare un principio di design solo dopo aver avuto la prova che sia utile in quel specifico caso
  • 49. Test di accettazione public class HoroscopeTest { @Test public void userListenOroscope() { Horoscope horoscope = new Horoscope(); horoscope.saveDivination("22-GIU-10", "Ariete", "Sarai molto fortunato."); String result = horoscope.GET("/horoscope/ariete.txt"); assertEquals("22 giugno 2010: Ariete, Sarai molto fortunato.”, result); }
  • 50. No test  No code Quando scrivo il codice di produzione scrivo il minimo necessario a far passare il test Se il minimo non mi convince (è troppo stupido), vuol dire che manca una specifica funzionale  cioè manca un test. Prima di scrivere una qualsiasi riga di codice in più aggiungo un test che la richieda.
  • 51. @Test public void shouldStoreDivinationsForMultipleSigns() { Horoscope horoscope = new Horoscope(); horoscope.saveDivination("22-GIU-10", "Ariete", "for ariete"); horoscope.saveDivination("22-GIU-10", "Toro", "for toro"); assertEquals("22 giugno 2010: Ariete, for ariete", horoscope.GET("/horoscope/ariete.txt")); assertEquals("22 giugno 2010: Toro, for toro", horoscope.GET("/horoscope/toro.txt")); }
  • 52. Cerco di non anticipare il design Prima di affrontare lo sviluppo faccio una veloce sessione di design Non implemento nessuna decisione fino a che non si rende necessaria E.g. anche servirà un DAO per adesso salvo tutto in RAM
  • 54. @Test public void howToCreateMp3() { Horoscope horoscope = new Horoscope( aFakeSyntetizerWhichReturns( aMp3Stream())); horoscope.saveDivination("22-GIU-10", "Ariete", "divination"); assertThat(horoscope.GET( "/horoscope/ariete.mp3").asByteArray(), is(equalTo(aMp3Stream()))); }
  • 55. Resource Ora il GET restituisce una Resource  public Resource GET(String path) {...} Il client decide quale rappresentazione usare: horoscope.GET("/horoscope/ariete/divination.txt").asString()); horoscope.GET("/horoscope/ariete/divination.mp3").asByteArray();
  • 57. Stato delle cose Al momento tutto viene persistito in memoria (in una HashMap) Non esiste ancora un oggetto DAO, tutto viene fatto dall’unica class Horoscope
  • 58. Estrazione del comportamento public class MemoryDivinationRepo { Divination lastDivinationForSign(String sign); void saveDivinationFromPronounce(String sign, String pronounce); }; public interface Divination { String asText(); byte[] asMp3(); };
  • 59. Estrazione dell’interfaccia public class MemoryDivinationRepo implements DivinationRepo {...} public interface DivinationRepo { Divination lastDivinationForSign(String sign); void saveDivinationFromPronounce(String sign, String pronounce); };
  • 60. Caratterizzazione del comportamento L’interfaccia è una minima parte del contratto, la parte più importante è il comportamento che l’oggetto dovrebbe avere. Il comportamento lo estraggo con dei test di caratterizzazione
  • 61. Caratterizzazione di MemoryDivinationRepo public class MemoryDivinationRepoTest { @Test public void shouldStoreDivinationPronounceForASign() {...} @Test public void shouldReplyWithAPronouncedMp3() {...} @Test public void shouldStoreDivinationPronounceForMultipleSigns() {...} @Test public void shouldOverrideDivinationPronounce() {...} }
  • 64. Example of an Automated Test @Test public void shouldParsePath() { String content = "[Trash Info]n" + "Path=/home/andrea/foo.txtn" + "DeletionDate=2010-08-23T12:59:14n"; String path = Parser.parsePath(content); assertEquals("/home/andrea/foo.txt”, path); }
  • 65. Testing Frameworks http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks •PHPUnit •SimpleTest •Google C++ Testing Framework •CppUnitLite •CppUnit •JUnit •TestNG •PyUnit •Nose

Notas del editor

  1. Questa presentazione è una breve introduzione al Test Driven Development. Questa è una presentazione rivolta a chi: - ha sentito parlare della tecnica - che non l’ha ancora provata o ha appena iniziato - e che vorrebbe saperne qualcosa di più. Non c’è nessun argomento avanzato, solo un assaggio. Gli obiettivi della presentazione sono: spiegare di cosa si tratta fornirvi un idea di quello che permette di fare spiegare i vantaggi dell’approcio Non è una trattazione completa dell’argomento. Per riuscire ad usarla con profitto sarebbe necessario almeno leggere alcuni libri che eventualmente dopo indicherò oppure meglio ancora lavorare con qualcuno bravo che la usa.
  2. Questi sono I contenuti della presenazione. Partiremo dallo spiegare brevemente i concetti che sono dietro alla tecnica. Esporremo la meccanica alla base della del processo. Infine vedremo due esemp. Un esempio sullo sviluppo in TDD di un parser. Un esempio tratto dal un mio recente lavoro.
  3. Io ho individuato due concetti alla base del TDD. Test First Il primo è scrivere il test prima. Cioe’ scriverlo prima che il codice di produzione sia creato. Questo perche’ l’altra strategia, il test after, non funziona. O almeno funziona male. I problemi con il test after sono che: - alla fine il test non viene scritto - testare del codice che non e’ pensato per essere testato puo’ essere veramente molto difficile Invece con il test first si riesce a definire a priori sia l’interfaccia del codice da sviluppare sia il comportamento che il codice deve avere. Automate your tests L’altro aspetto fondamentale è che tutti i test vengono automatizzati. Il motivo è semplice testare a mano è noioso. Ricompilare, avviare il programma, inserire gli input necessari per attivare quel particolare pezzo di codice che stiamo scrivendo è lungo e tedioso. Scrivere test automatici richiede un certo tempo, ma dopo aver fatto un po’ di pratica con gli strumenti il tempo è di meno di quello necessario per scrivere il test automatico.
  4. L’interfaccia si evolve  La GET() non era adatta per gestire sia testo che gli mp3 Nasce il concetto di Resource L’interfaccia si evolve per supportare le nuove funzionalità senza rovinare il design
  5. Nota: refactoring anche dei test scritti precedentemente che saranno adattati alla nuova API.
  6. Nella slide è riportato un esempio di test. Questo esempio di test riguarda lo sviluppo di un applicazione che gestisce il cestino di Linux. Quando in buttiamo un file nel cestino il sistema si deve ricordare alcuni metadati come il path da cui il file e’ stato “rimosso” e la data in cui questa cosa e’ successa. In Linux queste informazioni sono memorizzate in un file di testo che ha una sintassi simile alla stringa content che vedete nelle slide. Questo è un test per il metodo parse() della classe TrashInfo e verifica che sia in grado di leggere il contenuto del file passato in input. La caratteristica importante di questi test è che il controllo dell’esito è automatico, in altre parole che non é necessaria l’ispezione o il controllo da parte dello sviluppatore. L’altro aspetto importante é l’esecuzione é molto veloce perché non ci sono operazioni di I/O. Questo tipo di test è chiamato test di unità.