SlideShare une entreprise Scribd logo
1  sur  53
Télécharger pour lire hors ligne
Test-Driven Development


            Pietro Di Bello
      pietro.di.bello@xpeppers.com
          twitter: @pierodibello
           www.xpeppers.com



     JUG Trentino Alto-Adige
          Ottobre 2012
Test-Driven Development
        reloaded

            Pietro Di Bello
      pietro.di.bello@xpeppers.com
          twitter: @pierodibello
           www.xpeppers.com



     JUG Trentino Alto-Adige
          Ottobre 2012
Che cos’è il TDD?
Che cos’è il TDD?

TDD = Test-driven development
Che cos’è il TDD?

TDD = Test-driven development


   è una tecnica di sviluppo
Che cos’è il TDD?

TDD = Test-driven development


   è una tecnica di sviluppo
 inventata da uno sviluppatore
Che cos’è il TDD?

TDD = Test-driven design


è una tecnica di design e di
         sviluppo
Qual’è l’obbiettivo del TDD?

     L’obbiettivo del TDD è scrivere
      “codice pulito che funziona”
Qual’è l’obbiettivo del TDD?
                  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
Che cos’è il TDD?

TDD = Test-driven development


  guidata dalla scrittura di test
       automatici unitari
Che cos’è il TDD?

      Test automatici e unitari


      automatici = le verifiche
      le fa la macchina al posto
                 mio
Che cos’è il TDD?

       Test automatici e unitari


         unitari = non verifico
        l’intero sistema ma una
          singola unità (classe,
            modulo, funzione)
e allora?

Nel TDD si usano i test
automatici per guidare il design
e lo sviluppo del codice
Il ritmo del TDD
Il ritmo del TDD
Red — Write a little test that doesn't
work, and perhaps doesn't even
compile at first.
Il ritmo del TDD
Red — Write a little test that doesn't
work, and perhaps doesn't even
compile at first.
Green — Make the test work quickly,
committing whatever sins necessary in
the process.
Il ritmo del TDD
Red — Write a little test that doesn't
work, and perhaps doesn't even
compile at first.
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.
Write a test


public class AdderTest {
    @Test
    public void testTwoPlusThree() {
        Adder a = new Adder();
        assertEquals(5, a.add(2, 3));
    }
}
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; }
}
Red bar!

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; }
}
Expected 5, was 0
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; }
}
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; }
}
The procedure


1. Write a test

2. Make it compile
        Expected 5, was 0
3. Make it pass quickly

4. Refactor to remove duplication
The procedure

            Red

Refactor             Green




   Repeat every 2-10 min.
Prima un test che fallisce
Red — Write a little test that doesn't
work, and perhaps doesn't even
compile at first.
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
Maialino time... :)
Green — Make the test work quickly,
committing whatever sins necessary in
the process.
Paga per i tuoi debiti...
Refactor — Eliminate all of the
duplication created in merely getting
the test to work.
Qual’è l’essenza del TDD?
L’essenza del TDD è “sapere sempre
quale sarà il prossimo passo”
Qual’è l’essenza del TDD?
L’essenza del TDD è “sapere sempre
quale sarà il prossimo passo”
Avere chiara la strada
L’essenza del TDD

Meno stress
L’essenza del TDD

  Meno stress
• Progresso misurabile e verificabile
• Copertura di test
• Controllo dello scope
• Riduzione del bug rate
L’essenza del TDD

Progresso misurabile e verificabile
L’essenza del TDD

     A piccoli passi
L’essenza del TDD
    Copertura di test
L’essenza del TDD
Controllo dello scope funzionale
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
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
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”)
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”)
Debugging
  Sucks

            Testing
             Rocks
Coding Dojo
String Calculator



          • by Roy Osherove
          • (http://osherove.com/tdd-kata-1)
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
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
StringCalculator Kata



Allow the Add method to handle an unknown
amount of numbers
StringCalculator Kata



 Allow the Add method to handle new lines between
 numbers (instead of commas).

• the following input is ok:  “1n2,3”  (will equal 6)
• the following input is NOT ok:  “1,n” (not need
 to prove it - just clarifying)
StringCalculator Kata

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


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


Numbers bigger than 1000 should be ignored.

So adding 2 + 1001 = 2
StringCalculator Kata

Delimiters can be of any length with the
following format:

“//[delimiter]n”

For example:
“//[***]n1***2***3” returns 6.
StringCalculator Kata

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



•   Website www.xpeppers.com

•   E-Mail info@xpeppers.com

•   Twitter @xpeppers

Contenu connexe

Tendances

An Introduction to Test Driven Development
An Introduction to Test Driven Development An Introduction to Test Driven Development
An Introduction to Test Driven Development CodeOps Technologies LLP
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven DevelopmentConsulthinkspa
 
TDD CrashCourse Part2: TDD
TDD CrashCourse Part2: TDDTDD CrashCourse Part2: TDD
TDD CrashCourse Part2: TDDDavid Rodenas
 
Refactoring Legacy Code
Refactoring Legacy CodeRefactoring Legacy Code
Refactoring Legacy CodeAdam Culp
 
Overview on TDD (Test Driven Development) & ATDD (Acceptance Test Driven Deve...
Overview on TDD (Test Driven Development) & ATDD (Acceptance Test Driven Deve...Overview on TDD (Test Driven Development) & ATDD (Acceptance Test Driven Deve...
Overview on TDD (Test Driven Development) & ATDD (Acceptance Test Driven Deve...Zohirul Alam Tiemoon
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Developmentguy_davis
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Developmentguestc8093a6
 
TDD (Test Driven Design)
TDD (Test Driven Design)TDD (Test Driven Design)
TDD (Test Driven Design)nedirtv
 
Test driven development - Zombie proof your code
Test driven development - Zombie proof your codeTest driven development - Zombie proof your code
Test driven development - Zombie proof your codePascal Larocque
 
Dependency Injection in iOS
Dependency Injection in iOSDependency Injection in iOS
Dependency Injection in iOSPablo Villar
 
iOS Test-Driven Development
iOS Test-Driven DevelopmentiOS Test-Driven Development
iOS Test-Driven DevelopmentPablo Villar
 
Test-Driven Development (TDD)
Test-Driven Development (TDD)Test-Driven Development (TDD)
Test-Driven Development (TDD)Brian Rasmussen
 
Test Driven Development (TDD)
Test Driven Development (TDD)Test Driven Development (TDD)
Test Driven Development (TDD)David Ehringer
 
TDD Walkthrough - Encryption
TDD Walkthrough - EncryptionTDD Walkthrough - Encryption
TDD Walkthrough - EncryptionPeterKha2
 
TDD and Simple Design Workshop - Session 1 - March 2019
TDD and Simple Design Workshop - Session 1 - March 2019TDD and Simple Design Workshop - Session 1 - March 2019
TDD and Simple Design Workshop - Session 1 - March 2019Paulo Clavijo
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven DevelopmentDhaval Dalal
 
Refactoring Tips by Martin Fowler
Refactoring Tips by Martin FowlerRefactoring Tips by Martin Fowler
Refactoring Tips by Martin FowlerIgor Crvenov
 

Tendances (20)

An Introduction to Test Driven Development
An Introduction to Test Driven Development An Introduction to Test Driven Development
An Introduction to Test Driven Development
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
TDD CrashCourse Part2: TDD
TDD CrashCourse Part2: TDDTDD CrashCourse Part2: TDD
TDD CrashCourse Part2: TDD
 
TDD - Agile
TDD - Agile TDD - Agile
TDD - Agile
 
Refactoring Legacy Code
Refactoring Legacy CodeRefactoring Legacy Code
Refactoring Legacy Code
 
Overview on TDD (Test Driven Development) & ATDD (Acceptance Test Driven Deve...
Overview on TDD (Test Driven Development) & ATDD (Acceptance Test Driven Deve...Overview on TDD (Test Driven Development) & ATDD (Acceptance Test Driven Deve...
Overview on TDD (Test Driven Development) & ATDD (Acceptance Test Driven Deve...
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
TDD (Test Driven Design)
TDD (Test Driven Design)TDD (Test Driven Design)
TDD (Test Driven Design)
 
Test driven development - Zombie proof your code
Test driven development - Zombie proof your codeTest driven development - Zombie proof your code
Test driven development - Zombie proof your code
 
Dependency Injection in iOS
Dependency Injection in iOSDependency Injection in iOS
Dependency Injection in iOS
 
iOS Test-Driven Development
iOS Test-Driven DevelopmentiOS Test-Driven Development
iOS Test-Driven Development
 
Test-Driven Development (TDD)
Test-Driven Development (TDD)Test-Driven Development (TDD)
Test-Driven Development (TDD)
 
Test Driven Development (TDD)
Test Driven Development (TDD)Test Driven Development (TDD)
Test Driven Development (TDD)
 
TDD Walkthrough - Encryption
TDD Walkthrough - EncryptionTDD Walkthrough - Encryption
TDD Walkthrough - Encryption
 
TDD and Simple Design Workshop - Session 1 - March 2019
TDD and Simple Design Workshop - Session 1 - March 2019TDD and Simple Design Workshop - Session 1 - March 2019
TDD and Simple Design Workshop - Session 1 - March 2019
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
ES3-2020-05 Testing
ES3-2020-05 TestingES3-2020-05 Testing
ES3-2020-05 Testing
 
TDD with RSpec
TDD with RSpecTDD with RSpec
TDD with RSpec
 
Refactoring Tips by Martin Fowler
Refactoring Tips by Martin FowlerRefactoring Tips by Martin Fowler
Refactoring Tips by Martin Fowler
 

En vedette

Retrospective, StandUp Meeting e Daily Journal
Retrospective, StandUp Meeting e Daily JournalRetrospective, StandUp Meeting e Daily Journal
Retrospective, StandUp Meeting e Daily JournalPietro Di Bello
 
"Il dilettevole giuoco dell'oca" coding dojo
"Il dilettevole giuoco dell'oca" coding dojo"Il dilettevole giuoco dell'oca" coding dojo
"Il dilettevole giuoco dell'oca" coding dojoPietro Di Bello
 
Continuous Delivery su progetti Java: cosa abbiamo imparato facendoci del male
Continuous Delivery su progetti Java: cosa abbiamo imparato facendoci del maleContinuous Delivery su progetti Java: cosa abbiamo imparato facendoci del male
Continuous Delivery su progetti Java: cosa abbiamo imparato facendoci del malePietro Di Bello
 
Hiring Great People: how we improved our recruiting process to build and grow...
Hiring Great People: how we improved our recruiting process to build and grow...Hiring Great People: how we improved our recruiting process to build and grow...
Hiring Great People: how we improved our recruiting process to build and grow...Pietro Di Bello
 
Vivere per raccontarla: l’importanza del daily journal in un team agile
Vivere per raccontarla: l’importanza del daily journal in un team agileVivere per raccontarla: l’importanza del daily journal in un team agile
Vivere per raccontarla: l’importanza del daily journal in un team agilePietro Di Bello
 

En vedette (6)

Retrospective, StandUp Meeting e Daily Journal
Retrospective, StandUp Meeting e Daily JournalRetrospective, StandUp Meeting e Daily Journal
Retrospective, StandUp Meeting e Daily Journal
 
"Il dilettevole giuoco dell'oca" coding dojo
"Il dilettevole giuoco dell'oca" coding dojo"Il dilettevole giuoco dell'oca" coding dojo
"Il dilettevole giuoco dell'oca" coding dojo
 
Scrum In A Nutshell
Scrum In A NutshellScrum In A Nutshell
Scrum In A Nutshell
 
Continuous Delivery su progetti Java: cosa abbiamo imparato facendoci del male
Continuous Delivery su progetti Java: cosa abbiamo imparato facendoci del maleContinuous Delivery su progetti Java: cosa abbiamo imparato facendoci del male
Continuous Delivery su progetti Java: cosa abbiamo imparato facendoci del male
 
Hiring Great People: how we improved our recruiting process to build and grow...
Hiring Great People: how we improved our recruiting process to build and grow...Hiring Great People: how we improved our recruiting process to build and grow...
Hiring Great People: how we improved our recruiting process to build and grow...
 
Vivere per raccontarla: l’importanza del daily journal in un team agile
Vivere per raccontarla: l’importanza del daily journal in un team agileVivere per raccontarla: l’importanza del daily journal in un team agile
Vivere per raccontarla: l’importanza del daily journal in un team agile
 

Similaire à TDD reloaded - JUGTAA 24 Ottobre 2012

Tdd is not about testing (OOP)
Tdd is not about testing (OOP)Tdd is not about testing (OOP)
Tdd is not about testing (OOP)Gianluca Padovani
 
Introducción práctica a Test-Driven Development (TDD)
Introducción práctica a Test-Driven Development (TDD)Introducción práctica a Test-Driven Development (TDD)
Introducción práctica a Test-Driven Development (TDD)Software Craftsmanship Alicante
 
Introducción práctica a TDD
Introducción práctica a TDDIntroducción práctica a TDD
Introducción práctica a TDDrubocoptero
 
Test Driven iOS Development (TDD)
Test Driven iOS Development (TDD)Test Driven iOS Development (TDD)
Test Driven iOS Development (TDD)Babul Mirdha
 
Tdd is not about testing (C++ version)
Tdd is not about testing (C++ version)Tdd is not about testing (C++ version)
Tdd is not about testing (C++ version)Gianluca Padovani
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Developmentjakubkoci
 
Test Driven Development on Android (Kotlin Kenya)
Test Driven Development on Android (Kotlin Kenya)Test Driven Development on Android (Kotlin Kenya)
Test Driven Development on Android (Kotlin Kenya)Danny Preussler
 
How to complement TDD with static analysis
How to complement TDD with static analysisHow to complement TDD with static analysis
How to complement TDD with static analysisPVS-Studio
 
Introduzione allo Unit Testing
Introduzione allo Unit TestingIntroduzione allo Unit Testing
Introduzione allo Unit TestingStefano Ottaviani
 
Test-Driven Development In Action
Test-Driven Development In ActionTest-Driven Development In Action
Test-Driven Development In ActionJon Kruger
 
Pair programming and introduction to TDD
Pair programming and introduction to TDDPair programming and introduction to TDD
Pair programming and introduction to TDDArati Joshi
 
Getting started with Test Driven Development - Ferdous Mahmud Shaon
Getting started with Test Driven Development - Ferdous Mahmud ShaonGetting started with Test Driven Development - Ferdous Mahmud Shaon
Getting started with Test Driven Development - Ferdous Mahmud ShaonCefalo
 
Getting started with Test Driven Development
Getting started with Test Driven DevelopmentGetting started with Test Driven Development
Getting started with Test Driven DevelopmentFerdous Mahmud Shaon
 

Similaire à TDD reloaded - JUGTAA 24 Ottobre 2012 (20)

TDD a piccoli passi
TDD a piccoli passiTDD a piccoli passi
TDD a piccoli passi
 
Tdd is not about testing (OOP)
Tdd is not about testing (OOP)Tdd is not about testing (OOP)
Tdd is not about testing (OOP)
 
Introducción práctica a Test-Driven Development (TDD)
Introducción práctica a Test-Driven Development (TDD)Introducción práctica a Test-Driven Development (TDD)
Introducción práctica a Test-Driven Development (TDD)
 
Introducción práctica a TDD
Introducción práctica a TDDIntroducción práctica a TDD
Introducción práctica a TDD
 
Test Driven iOS Development (TDD)
Test Driven iOS Development (TDD)Test Driven iOS Development (TDD)
Test Driven iOS Development (TDD)
 
Tdd is not about testing (C++ version)
Tdd is not about testing (C++ version)Tdd is not about testing (C++ version)
Tdd is not about testing (C++ version)
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Test Driven Development on Android (Kotlin Kenya)
Test Driven Development on Android (Kotlin Kenya)Test Driven Development on Android (Kotlin Kenya)
Test Driven Development on Android (Kotlin Kenya)
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
How to complement TDD with static analysis
How to complement TDD with static analysisHow to complement TDD with static analysis
How to complement TDD with static analysis
 
Introduzione allo Unit Testing
Introduzione allo Unit TestingIntroduzione allo Unit Testing
Introduzione allo Unit Testing
 
TDD talk
TDD talkTDD talk
TDD talk
 
Test-Driven Development In Action
Test-Driven Development In ActionTest-Driven Development In Action
Test-Driven Development In Action
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Pair programming and introduction to TDD
Pair programming and introduction to TDDPair programming and introduction to TDD
Pair programming and introduction to TDD
 
TDD Best Practices
TDD Best PracticesTDD Best Practices
TDD Best Practices
 
Getting started with Test Driven Development - Ferdous Mahmud Shaon
Getting started with Test Driven Development - Ferdous Mahmud ShaonGetting started with Test Driven Development - Ferdous Mahmud Shaon
Getting started with Test Driven Development - Ferdous Mahmud Shaon
 
Getting started with Test Driven Development
Getting started with Test Driven DevelopmentGetting started with Test Driven Development
Getting started with Test Driven Development
 
Test Driven
Test DrivenTest Driven
Test Driven
 
Tdd in practice
Tdd in practiceTdd in practice
Tdd in practice
 

Plus de Pietro Di Bello

Lessons learned in surviving to Technical Debt
Lessons learned in surviving to Technical DebtLessons learned in surviving to Technical Debt
Lessons learned in surviving to Technical DebtPietro Di Bello
 
Surviving to a Legacy Codebase - Codemotion Berlin 2018 Edition
Surviving to a Legacy Codebase - Codemotion Berlin 2018 EditionSurviving to a Legacy Codebase - Codemotion Berlin 2018 Edition
Surviving to a Legacy Codebase - Codemotion Berlin 2018 EditionPietro Di Bello
 
Surviving to a Legacy Codebase - Voxxed Days Ticino Edition
Surviving to a Legacy Codebase - Voxxed Days Ticino EditionSurviving to a Legacy Codebase - Voxxed Days Ticino Edition
Surviving to a Legacy Codebase - Voxxed Days Ticino EditionPietro Di Bello
 
Hiring Great People: how we improved our recruiting process to build and grow...
Hiring Great People: how we improved our recruiting process to build and grow...Hiring Great People: how we improved our recruiting process to build and grow...
Hiring Great People: how we improved our recruiting process to build and grow...Pietro Di Bello
 
Breaking the ice with agile - cinque strade per rompere il ghiaccio e introdu...
Breaking the ice with agile - cinque strade per rompere il ghiaccio e introdu...Breaking the ice with agile - cinque strade per rompere il ghiaccio e introdu...
Breaking the ice with agile - cinque strade per rompere il ghiaccio e introdu...Pietro Di Bello
 
A brief intro to TDD for a JUG-TAA event
A brief intro to TDD for a JUG-TAA eventA brief intro to TDD for a JUG-TAA event
A brief intro to TDD for a JUG-TAA eventPietro Di Bello
 

Plus de Pietro Di Bello (6)

Lessons learned in surviving to Technical Debt
Lessons learned in surviving to Technical DebtLessons learned in surviving to Technical Debt
Lessons learned in surviving to Technical Debt
 
Surviving to a Legacy Codebase - Codemotion Berlin 2018 Edition
Surviving to a Legacy Codebase - Codemotion Berlin 2018 EditionSurviving to a Legacy Codebase - Codemotion Berlin 2018 Edition
Surviving to a Legacy Codebase - Codemotion Berlin 2018 Edition
 
Surviving to a Legacy Codebase - Voxxed Days Ticino Edition
Surviving to a Legacy Codebase - Voxxed Days Ticino EditionSurviving to a Legacy Codebase - Voxxed Days Ticino Edition
Surviving to a Legacy Codebase - Voxxed Days Ticino Edition
 
Hiring Great People: how we improved our recruiting process to build and grow...
Hiring Great People: how we improved our recruiting process to build and grow...Hiring Great People: how we improved our recruiting process to build and grow...
Hiring Great People: how we improved our recruiting process to build and grow...
 
Breaking the ice with agile - cinque strade per rompere il ghiaccio e introdu...
Breaking the ice with agile - cinque strade per rompere il ghiaccio e introdu...Breaking the ice with agile - cinque strade per rompere il ghiaccio e introdu...
Breaking the ice with agile - cinque strade per rompere il ghiaccio e introdu...
 
A brief intro to TDD for a JUG-TAA event
A brief intro to TDD for a JUG-TAA eventA brief intro to TDD for a JUG-TAA event
A brief intro to TDD for a JUG-TAA event
 

Dernier

Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxDr. Ravikiran H M Gowda
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxPooja Bhuva
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxJisc
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Pooja Bhuva
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structuredhanjurrannsibayan2
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jisc
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...Amil baba
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxUmeshTimilsina1
 

Dernier (20)

Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 

TDD reloaded - JUGTAA 24 Ottobre 2012

  • 1. Test-Driven Development Pietro Di Bello pietro.di.bello@xpeppers.com twitter: @pierodibello www.xpeppers.com JUG Trentino Alto-Adige Ottobre 2012
  • 2. Test-Driven Development reloaded Pietro Di Bello pietro.di.bello@xpeppers.com twitter: @pierodibello www.xpeppers.com JUG Trentino Alto-Adige Ottobre 2012
  • 4. Che cos’è il TDD? TDD = Test-driven development
  • 5. Che cos’è il TDD? TDD = Test-driven development è una tecnica di sviluppo
  • 6. Che cos’è il TDD? TDD = Test-driven development è una tecnica di sviluppo inventata da uno sviluppatore
  • 7. Che cos’è il TDD? TDD = Test-driven design è una tecnica di design e di sviluppo
  • 8. Qual’è l’obbiettivo del TDD? L’obbiettivo del TDD è scrivere “codice pulito che funziona”
  • 9. Qual’è l’obbiettivo del TDD? 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
  • 10. Che cos’è il TDD? TDD = Test-driven development guidata dalla scrittura di test automatici unitari
  • 11. Che cos’è il TDD? Test automatici e unitari automatici = le verifiche le fa la macchina al posto mio
  • 12. Che cos’è il TDD? Test automatici e unitari unitari = non verifico l’intero sistema ma una singola unità (classe, modulo, funzione)
  • 13. e allora? Nel TDD si usano i test automatici per guidare il design e lo sviluppo del codice
  • 15. Il ritmo del TDD Red — Write a little test that doesn't work, and perhaps doesn't even compile at first.
  • 16. Il ritmo del TDD Red — Write a little test that doesn't work, and perhaps doesn't even compile at first. Green — Make the test work quickly, committing whatever sins necessary in the process.
  • 17. Il ritmo del TDD Red — Write a little test that doesn't work, and perhaps doesn't even compile at first. 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.
  • 18. Write a test public class AdderTest { @Test public void testTwoPlusThree() { Adder a = new Adder(); assertEquals(5, a.add(2, 3)); } }
  • 19. 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; } }
  • 20. Red bar! 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; } } Expected 5, was 0
  • 21. 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; } }
  • 22. 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; } }
  • 23. The procedure 1. Write a test 2. Make it compile Expected 5, was 0 3. Make it pass quickly 4. Refactor to remove duplication
  • 24. The procedure Red Refactor Green Repeat every 2-10 min.
  • 25. Prima un test che fallisce Red — Write a little test that doesn't work, and perhaps doesn't even compile at first.
  • 26. 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
  • 27. Maialino time... :) Green — Make the test work quickly, committing whatever sins necessary in the process.
  • 28. Paga per i tuoi debiti... Refactor — Eliminate all of the duplication created in merely getting the test to work.
  • 29. Qual’è l’essenza del TDD? L’essenza del TDD è “sapere sempre quale sarà il prossimo passo”
  • 30. Qual’è l’essenza del TDD? L’essenza del TDD è “sapere sempre quale sarà il prossimo passo” Avere chiara la strada
  • 32. L’essenza del TDD Meno stress • Progresso misurabile e verificabile • Copertura di test • Controllo dello scope • Riduzione del bug rate
  • 33. L’essenza del TDD Progresso misurabile e verificabile
  • 34. L’essenza del TDD A piccoli passi
  • 35. L’essenza del TDD Copertura di test
  • 36. L’essenza del TDD Controllo dello scope funzionale
  • 37. 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
  • 38. 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
  • 39. 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”)
  • 40. 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”)
  • 41. Debugging Sucks Testing Rocks
  • 42.
  • 43. Coding Dojo String Calculator • by Roy Osherove • (http://osherove.com/tdd-kata-1)
  • 44. 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
  • 45. 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
  • 46. StringCalculator Kata Allow the Add method to handle an unknown amount of numbers
  • 47. StringCalculator Kata Allow the Add method to handle new lines between numbers (instead of commas). • the following input is ok:  “1n2,3”  (will equal 6) • the following input is NOT ok:  “1,n” (not need to prove it - just clarifying)
  • 48. StringCalculator Kata 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
  • 49. StringCalculator Kata 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
  • 50. StringCalculator Kata Numbers bigger than 1000 should be ignored. So adding 2 + 1001 = 2
  • 51. StringCalculator Kata Delimiters can be of any length with the following format: “//[delimiter]n” For example: “//[***]n1***2***3” returns 6.
  • 52. StringCalculator Kata Make sure you can also handle multiple delimiters with length longer than one char
  • 53. Contacts • Website www.xpeppers.com • E-Mail info@xpeppers.com • Twitter @xpeppers