SlideShare una empresa de Scribd logo
1 de 40
Descargar para leer sin conexión
TDD (with FLOW3)
  Karsten Dambekalns <karsten@typo3.org>




                                           Inspiring people to
                                           share
What?
Why?
How?
        Inspiring people to
        share
Test first,
  code later
TDD is about Tests
 Write tests for all your code

 •   New features come with a test

 •   Bugfixes come with a test




                                     Inspiring people to
                                     share
TDD is about Tests
 Write tests for all your code

 •   New features come with a test

 •   Bugfixes come with a test

 Write tests before your code

 •   Adding a feature means adding a test first

 •   Fixing a bug means writing a test first




                                                 Inspiring people to
                                                 share
The TDD Mantra
         Design




                  Inspiring people to
                  share
The TDD Mantra
         Design



                  Test fails




                   Inspiring people to
                  share
The TDD Mantra
         Design



                    Test fails




        Implement



                     Inspiring people to
                    share
The TDD Mantra
                 Design



  Test passes               Test fails




                Implement



                             Inspiring people to
                            share
The TDD Mantra
                 Design



  Test passes               Test fails




                Implement



                             Inspiring people to
                            share
Unit tests
  Unit tests are small programs that test your code

  They check

 •   the result of methods against expected values

 •   whether methods are (not) called as expected

  A test should

 •   be well-named

 •   check only one assertion



                                                  Inspiring people to
                                                  share
Good tests
 Should be...

 •   automated

 •   self-contained

 •   repeatable

 •   thorough

 •   small

 •   talk about the domain



                             Inspiring people to
                             share
Running unit tests
 Unit tests are usually run with a test runner

 The xUnit family is most widespread

 PHPUnit does the job for PHP

 FLOW3 has a Testing package

 •   acts as test runner

 •   provides helpful environment

 •   web-based and CLI tool



                                                 Inspiring people to
                                                 share
Running unit tests
 Unit tests are usually run with a test runner

 The xUnit family is most widespread

 PHPUnit does the job for PHP

 FLOW3 has a Testing package

 •   acts as test runner

 •   provides helpful environment

 •   web-based and CLI tool



                                                 Inspiring people to
                                                 share
Running unit tests
 Unit tests are usually run with a test runner

 The xUnit family is most widespread

 PHPUnit does the job for PHP

 FLOW3 has a Testing package

 •   acts as test runner

 •   provides helpful environment

 •   web-based and CLI tool



                                                 Inspiring people to
                                                 share
Running unit tests
 Unit tests are usually run with a test runner

 The xUnit family is most widespread

 PHPUnit does the job for PHP

 FLOW3 has a Testing package

 •   acts as test runner

 •   provides helpful environment

 •   web-based and CLI tool



                                                 Inspiring people to
                                                 share
Tests aren’t
   the goal
They make you feel good
They make you feel good
    What the tests do for you is the key

    The tests make you

•     focus on your task

•     code exactly what you need

•     think from the outside

•     a better programmer*




                                           Inspiring people to
                                           share
They make you feel good
    What the tests do for you is the key

    The tests make you

•     focus on your task

•     code exactly what you need

•     think from the outside

•     a better programmer*
                                   * decoupling helps with testing,
                                   decoupled code is easier to maintain,
                                   easier is better - q.e.d.


                                                       Inspiring people to
                                                       share
Use TDD to...
  Feel more comfortable

 •   Build confidence in your code

 •   Reduce fear of change

  Have a safety net

 •   Regression testing built in

 •   Helps with refactoring

  Provide (good) documentation through (good) tests



                                                Inspiring people to
                                                share
D for Development




                Inspiring people to
                share
D for Design




               Inspiring people to
               share
D for Design
 Writing tests first is likely to improve your code

 •   You use the API before you code

 •   You make the API fit your needs

 Unit testing ensures decoupling

 You only code what is really needed

 Constant refactoring keeps code clean




                                                     Inspiring people to
                                                     share
TDD in Practice
  Write down your next goal

  Now write a test to check this

  The test will fail, it might even break with a fatal error

  Now write the code you need to write

  If you test again, the test should now pass

  Iterate – until it passes or all features are in




                                                      Inspiring people to
                                                     share
No FLOW3?
 No TDD!
       Inspiring people to
       share
Dependencies
 Classes explicitly refer to other classes

 But you want to test a small unit

 You don't want to test

 •   The Steamer

 •   The Grinder

 You want to test

 •   if the Barista uses the right amount of coffee and the
     requested milk when asked for coffee


                                                   Inspiring people to
                                                   share
Dependencies – bad
class Barista {

     /**
      * Makes a drink
      *
      * @param string $drink
      */
     public function make($drink) {
          if ($drink === 'Tea') {
               throw new F3::Demo::Exception::NotAvailable(
                       'We don't serve no tea, Sir', 1223385110);
          }

          $coffeeGrinder = new Grinder();
          $steamer = new Steamer();

          $coffee = $coffeeGrinder->grind(9, Grinder::FINE);
          $milk = $steamer->getSteamedMilk(Steamer::FAT_LOW);
     }
}




                                                                     Inspiring people to
                                                                     share
Dependency Injection
 This methodology is referred to as the quot;Hollywood Principlequot;:
 quot;Don't call us, we'll call youquot;

 A class doesn't ask for the instance of another class but gets it
 injected

 Enforces loose coupling and high cohesion

 Allows you to mock collaborators

 Makes you a better programmer




                                                   Inspiring people to
                                                   share
Dependencies – good
class Barista {

     /**
      * @var F3::Demo::Grinder
      */
     protected $grinder;

     /**
      * @var F3::Demo::Steamer
      */
     protected $steamer;

     /**
      *
      * @param Grinder $grinder
      * @param Steamer $steamer
      */
     public function __construct(F3::Demo::Grinder $grinder, F3::Demo::Steamer $steamer) {
          $this->grinder = $grinder;
          $this->steamer = $steamer;
     }




                                                                       Inspiring people to
                                                                       share
Dependencies – good
 /**
  * Makes a drink
  *
  * @param string $drink
  */
 public function make($drink) {
      if ($drink === 'Tea') {
           throw new F3::Demo::Exception::NotAvailable(
                       'We don't serve no tea, Sir', 1223385110);
      }

      $coffee = $this->grinder->grind(9, Grinder::FINE);
      $milk = $this->steamer->getSteamedMilk(Steamer::FAT_LOW);
 }




                                                                  Inspiring people to
                                                                  share
Without Dependency
Injection you cannot
   do unit testing

                Inspiring people to
                share
So, what to
       inject?
           Inspiring people to
           share
Mocks & Stubs
 Sometimes you need to test interaction with external systems,
 like milk steamers

 A test should be small, encapsulated, stand-alone



 Mock objects allow you to use fake objects

 Stubs can be used to return hard-coded results

 Using them is actually (somewhat) easy with PHPUnit




                                                  Inspiring people to
                                                  share
FLOW3 + TDD = FUN
 FLOW3 makes Dependency Injection easy

 With Dependency Injection you can do proper unit testing

 Proper unit tests

 •   help produce reliable code

 •   make refactoring possible

 •   make you feel better

 All this means more fun while coding



                                                Inspiring people to
                                                share
Questions!

        Inspiring people to
        share
Literature
   Test-Driven Development By Example
   Kent Beck, Addison-Wesley


   Continuous Integration – Improving Software Quality and
   Reducing Risk
   Paul M. Duvall, Addison-Wesley

   xUnit Test Patterns – Refactoring Test Code
   Gerard Meszaros, Addison-Wesley




                                                 Inspiring people to
                                                 share
Links
   FLOW3
   http://flow3.typo3.org/



   PHPUnit
   http://www.phpunit.de/



   TDD in Wikipedia
   http://en.wikipedia.org/wiki/Test-driven_development




                                            Inspiring people to
                                            share
Test-Driven Development with FLOW3

Más contenido relacionado

Destacado

1.1 My Works About Proposal / Presentation
1.1 My Works About Proposal / Presentation1.1 My Works About Proposal / Presentation
1.1 My Works About Proposal / Presentation
Ruby Kuo
 
7 Habits of SEO-Centric Web Design
7 Habits of SEO-Centric Web Design7 Habits of SEO-Centric Web Design
7 Habits of SEO-Centric Web Design
Dino Baskovic
 
Cross-border learning with eTwinning
Cross-border learning with   eTwinningCross-border learning with   eTwinning
Cross-border learning with eTwinning
Rositsa Dimova
 

Destacado (17)

Semantic TYPO3
Semantic TYPO3Semantic TYPO3
Semantic TYPO3
 
Feb 2012 sustainability mls
Feb 2012 sustainability  mlsFeb 2012 sustainability  mls
Feb 2012 sustainability mls
 
Designstudionotes
DesignstudionotesDesignstudionotes
Designstudionotes
 
أنشطة مصلحة النظافة
أنشطة مصلحة النظافةأنشطة مصلحة النظافة
أنشطة مصلحة النظافة
 
Open Government and local community foundations: Getting involved
Open Government and local community foundations: Getting involvedOpen Government and local community foundations: Getting involved
Open Government and local community foundations: Getting involved
 
Aaa
AaaAaa
Aaa
 
1.1 My Works About Proposal / Presentation
1.1 My Works About Proposal / Presentation1.1 My Works About Proposal / Presentation
1.1 My Works About Proposal / Presentation
 
i18n and L10n in TYPO3 Flow
i18n and L10n in TYPO3 Flowi18n and L10n in TYPO3 Flow
i18n and L10n in TYPO3 Flow
 
7 Habits of SEO-Centric Web Design
7 Habits of SEO-Centric Web Design7 Habits of SEO-Centric Web Design
7 Habits of SEO-Centric Web Design
 
How to start an online business:7 stories of success
How to start an online business:7 stories of successHow to start an online business:7 stories of success
How to start an online business:7 stories of success
 
Project execution for paranoids
Project execution for paranoidsProject execution for paranoids
Project execution for paranoids
 
ANT
ANTANT
ANT
 
Cross-border learning with eTwinning
Cross-border learning with   eTwinningCross-border learning with   eTwinning
Cross-border learning with eTwinning
 
Endangered species in spain map
Endangered species in spain mapEndangered species in spain map
Endangered species in spain map
 
Do italia sas samolet
Do italia sas samoletDo italia sas samolet
Do italia sas samolet
 
Simone project
Simone projectSimone project
Simone project
 
Natural attractions
Natural attractionsNatural attractions
Natural attractions
 

Similar a Test-Driven Development with FLOW3

Pair Programming Presentation
Pair Programming PresentationPair Programming Presentation
Pair Programming Presentation
ThoughtWorks
 
David Nuescheler: Igniting CQ 5.3: What's New and Roadmap
David Nuescheler: Igniting CQ 5.3: What's New and RoadmapDavid Nuescheler: Igniting CQ 5.3: What's New and Roadmap
David Nuescheler: Igniting CQ 5.3: What's New and Roadmap
Day Software
 

Similar a Test-Driven Development with FLOW3 (20)

TDD (with FLOW3)
TDD (with FLOW3)TDD (with FLOW3)
TDD (with FLOW3)
 
Beyond TDD: Enabling Your Team to Continuously Deliver Software
Beyond TDD: Enabling Your Team to Continuously Deliver SoftwareBeyond TDD: Enabling Your Team to Continuously Deliver Software
Beyond TDD: Enabling Your Team to Continuously Deliver Software
 
An Introduction to Test Driven Development
An Introduction to Test Driven Development An Introduction to Test Driven Development
An Introduction to Test Driven Development
 
Don't let your tests slow you down
Don't let your tests slow you downDon't let your tests slow you down
Don't let your tests slow you down
 
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)
 
TDD Workshop (January, 2018)
TDD Workshop (January, 2018)TDD Workshop (January, 2018)
TDD Workshop (January, 2018)
 
Pair Programming Presentation
Pair Programming PresentationPair Programming Presentation
Pair Programming Presentation
 
Xp not windows xp
Xp not windows xpXp not windows xp
Xp not windows xp
 
[XP Day Vietnam 2015] XP is not windows XP
[XP Day Vietnam 2015] XP is not windows XP[XP Day Vietnam 2015] XP is not windows XP
[XP Day Vietnam 2015] XP is not windows XP
 
XP, Not Windows XP
XP, Not Windows XPXP, Not Windows XP
XP, Not Windows XP
 
[XPday.vn] XP? not Windows XP {presentation} (at) [XP Day Vietnam 2015]
[XPday.vn] XP? not Windows XP {presentation} (at) [XP Day Vietnam 2015][XPday.vn] XP? not Windows XP {presentation} (at) [XP Day Vietnam 2015]
[XPday.vn] XP? not Windows XP {presentation} (at) [XP Day Vietnam 2015]
 
How penetration testing techniques can help you improve your qa skills
How penetration testing techniques can help you improve your qa skillsHow penetration testing techniques can help you improve your qa skills
How penetration testing techniques can help you improve your qa skills
 
TDD Flow: The Mantra in Action
TDD Flow: The Mantra in ActionTDD Flow: The Mantra in Action
TDD Flow: The Mantra in Action
 
Unit testing
Unit testingUnit testing
Unit testing
 
Deployment is the new build
Deployment is the new buildDeployment is the new build
Deployment is the new build
 
The Professional Programmer
The Professional ProgrammerThe Professional Programmer
The Professional Programmer
 
Adopting A Whole Team Approach To Quality
Adopting  A  Whole  Team  Approach  To  QualityAdopting  A  Whole  Team  Approach  To  Quality
Adopting A Whole Team Approach To Quality
 
Quality of Bug Reports in Open Source
Quality of Bug Reports in Open SourceQuality of Bug Reports in Open Source
Quality of Bug Reports in Open Source
 
David Nuescheler: Igniting CQ 5.3: What's New and Roadmap
David Nuescheler: Igniting CQ 5.3: What's New and RoadmapDavid Nuescheler: Igniting CQ 5.3: What's New and Roadmap
David Nuescheler: Igniting CQ 5.3: What's New and Roadmap
 
It's all about behaviour, also in php - phpspec
It's all about behaviour, also in php - phpspecIt's all about behaviour, also in php - phpspec
It's all about behaviour, also in php - phpspec
 

Más de Karsten Dambekalns

The agile future of a ponderous project
The agile future of a ponderous projectThe agile future of a ponderous project
The agile future of a ponderous project
Karsten Dambekalns
 
How Domain-Driven Design helps you to migrate into the future
How Domain-Driven Design helps you to migrate into the futureHow Domain-Driven Design helps you to migrate into the future
How Domain-Driven Design helps you to migrate into the future
Karsten Dambekalns
 
Content Repository, Versioning and Workspaces in TYPO3 Phoenix
Content Repository, Versioning and Workspaces in TYPO3 PhoenixContent Repository, Versioning and Workspaces in TYPO3 Phoenix
Content Repository, Versioning and Workspaces in TYPO3 Phoenix
Karsten Dambekalns
 

Más de Karsten Dambekalns (20)

The Perfect Neos Project Setup
The Perfect Neos Project SetupThe Perfect Neos Project Setup
The Perfect Neos Project Setup
 
Sawubona! Content Dimensions with Neos
Sawubona! Content Dimensions with NeosSawubona! Content Dimensions with Neos
Sawubona! Content Dimensions with Neos
 
Deploying TYPO3 Neos websites using Surf
Deploying TYPO3 Neos websites using SurfDeploying TYPO3 Neos websites using Surf
Deploying TYPO3 Neos websites using Surf
 
Profiling TYPO3 Flow Applications
Profiling TYPO3 Flow ApplicationsProfiling TYPO3 Flow Applications
Profiling TYPO3 Flow Applications
 
Using Document Databases with TYPO3 Flow
Using Document Databases with TYPO3 FlowUsing Document Databases with TYPO3 Flow
Using Document Databases with TYPO3 Flow
 
FLOW3-Workshop F3X12
FLOW3-Workshop F3X12FLOW3-Workshop F3X12
FLOW3-Workshop F3X12
 
Doctrine in FLOW3
Doctrine in FLOW3Doctrine in FLOW3
Doctrine in FLOW3
 
How Git and Gerrit make you more productive
How Git and Gerrit make you more productiveHow Git and Gerrit make you more productive
How Git and Gerrit make you more productive
 
The agile future of a ponderous project
The agile future of a ponderous projectThe agile future of a ponderous project
The agile future of a ponderous project
 
How Domain-Driven Design helps you to migrate into the future
How Domain-Driven Design helps you to migrate into the futureHow Domain-Driven Design helps you to migrate into the future
How Domain-Driven Design helps you to migrate into the future
 
Content Repository, Versioning and Workspaces in TYPO3 Phoenix
Content Repository, Versioning and Workspaces in TYPO3 PhoenixContent Repository, Versioning and Workspaces in TYPO3 Phoenix
Content Repository, Versioning and Workspaces in TYPO3 Phoenix
 
Transparent Object Persistence (within FLOW3)
Transparent Object Persistence (within FLOW3)Transparent Object Persistence (within FLOW3)
Transparent Object Persistence (within FLOW3)
 
JavaScript for PHP Developers
JavaScript for PHP DevelopersJavaScript for PHP Developers
JavaScript for PHP Developers
 
Transparent Object Persistence with FLOW3
Transparent Object Persistence with FLOW3Transparent Object Persistence with FLOW3
Transparent Object Persistence with FLOW3
 
Implementing a JSR-283 Content Repository in PHP
Implementing a JSR-283 Content Repository in PHPImplementing a JSR-283 Content Repository in PHP
Implementing a JSR-283 Content Repository in PHP
 
Knowledge Management in der TYPO3 Community
Knowledge Management in der TYPO3 CommunityKnowledge Management in der TYPO3 Community
Knowledge Management in der TYPO3 Community
 
Unicode & PHP6
Unicode & PHP6Unicode & PHP6
Unicode & PHP6
 
Implementing a JSR-283 Content Repository in PHP
Implementing a JSR-283 Content Repository in PHPImplementing a JSR-283 Content Repository in PHP
Implementing a JSR-283 Content Repository in PHP
 
A Content Repository for TYPO3 5.0
A Content Repository for TYPO3 5.0A Content Repository for TYPO3 5.0
A Content Repository for TYPO3 5.0
 
Introduction to Source Code Management
Introduction to Source Code ManagementIntroduction to Source Code Management
Introduction to Source Code Management
 

Último

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Último (20)

Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 

Test-Driven Development with FLOW3

  • 1. TDD (with FLOW3) Karsten Dambekalns <karsten@typo3.org> Inspiring people to share
  • 2. What? Why? How? Inspiring people to share
  • 3. Test first, code later
  • 4. TDD is about Tests Write tests for all your code • New features come with a test • Bugfixes come with a test Inspiring people to share
  • 5. TDD is about Tests Write tests for all your code • New features come with a test • Bugfixes come with a test Write tests before your code • Adding a feature means adding a test first • Fixing a bug means writing a test first Inspiring people to share
  • 6. The TDD Mantra Design Inspiring people to share
  • 7. The TDD Mantra Design Test fails Inspiring people to share
  • 8. The TDD Mantra Design Test fails Implement Inspiring people to share
  • 9. The TDD Mantra Design Test passes Test fails Implement Inspiring people to share
  • 10. The TDD Mantra Design Test passes Test fails Implement Inspiring people to share
  • 11. Unit tests Unit tests are small programs that test your code They check • the result of methods against expected values • whether methods are (not) called as expected A test should • be well-named • check only one assertion Inspiring people to share
  • 12. Good tests Should be... • automated • self-contained • repeatable • thorough • small • talk about the domain Inspiring people to share
  • 13. Running unit tests Unit tests are usually run with a test runner The xUnit family is most widespread PHPUnit does the job for PHP FLOW3 has a Testing package • acts as test runner • provides helpful environment • web-based and CLI tool Inspiring people to share
  • 14. Running unit tests Unit tests are usually run with a test runner The xUnit family is most widespread PHPUnit does the job for PHP FLOW3 has a Testing package • acts as test runner • provides helpful environment • web-based and CLI tool Inspiring people to share
  • 15. Running unit tests Unit tests are usually run with a test runner The xUnit family is most widespread PHPUnit does the job for PHP FLOW3 has a Testing package • acts as test runner • provides helpful environment • web-based and CLI tool Inspiring people to share
  • 16. Running unit tests Unit tests are usually run with a test runner The xUnit family is most widespread PHPUnit does the job for PHP FLOW3 has a Testing package • acts as test runner • provides helpful environment • web-based and CLI tool Inspiring people to share
  • 17. Tests aren’t the goal
  • 18.
  • 19. They make you feel good
  • 20. They make you feel good What the tests do for you is the key The tests make you • focus on your task • code exactly what you need • think from the outside • a better programmer* Inspiring people to share
  • 21. They make you feel good What the tests do for you is the key The tests make you • focus on your task • code exactly what you need • think from the outside • a better programmer* * decoupling helps with testing, decoupled code is easier to maintain, easier is better - q.e.d. Inspiring people to share
  • 22. Use TDD to... Feel more comfortable • Build confidence in your code • Reduce fear of change Have a safety net • Regression testing built in • Helps with refactoring Provide (good) documentation through (good) tests Inspiring people to share
  • 23. D for Development Inspiring people to share
  • 24. D for Design Inspiring people to share
  • 25. D for Design Writing tests first is likely to improve your code • You use the API before you code • You make the API fit your needs Unit testing ensures decoupling You only code what is really needed Constant refactoring keeps code clean Inspiring people to share
  • 26. TDD in Practice Write down your next goal Now write a test to check this The test will fail, it might even break with a fatal error Now write the code you need to write If you test again, the test should now pass Iterate – until it passes or all features are in Inspiring people to share
  • 27. No FLOW3? No TDD! Inspiring people to share
  • 28. Dependencies Classes explicitly refer to other classes But you want to test a small unit You don't want to test • The Steamer • The Grinder You want to test • if the Barista uses the right amount of coffee and the requested milk when asked for coffee Inspiring people to share
  • 29. Dependencies – bad class Barista { /** * Makes a drink * * @param string $drink */ public function make($drink) { if ($drink === 'Tea') { throw new F3::Demo::Exception::NotAvailable( 'We don't serve no tea, Sir', 1223385110); } $coffeeGrinder = new Grinder(); $steamer = new Steamer(); $coffee = $coffeeGrinder->grind(9, Grinder::FINE); $milk = $steamer->getSteamedMilk(Steamer::FAT_LOW); } } Inspiring people to share
  • 30. Dependency Injection This methodology is referred to as the quot;Hollywood Principlequot;: quot;Don't call us, we'll call youquot; A class doesn't ask for the instance of another class but gets it injected Enforces loose coupling and high cohesion Allows you to mock collaborators Makes you a better programmer Inspiring people to share
  • 31. Dependencies – good class Barista { /** * @var F3::Demo::Grinder */ protected $grinder; /** * @var F3::Demo::Steamer */ protected $steamer; /** * * @param Grinder $grinder * @param Steamer $steamer */ public function __construct(F3::Demo::Grinder $grinder, F3::Demo::Steamer $steamer) { $this->grinder = $grinder; $this->steamer = $steamer; } Inspiring people to share
  • 32. Dependencies – good /** * Makes a drink * * @param string $drink */ public function make($drink) { if ($drink === 'Tea') { throw new F3::Demo::Exception::NotAvailable( 'We don't serve no tea, Sir', 1223385110); } $coffee = $this->grinder->grind(9, Grinder::FINE); $milk = $this->steamer->getSteamedMilk(Steamer::FAT_LOW); } Inspiring people to share
  • 33. Without Dependency Injection you cannot do unit testing Inspiring people to share
  • 34. So, what to inject? Inspiring people to share
  • 35. Mocks & Stubs Sometimes you need to test interaction with external systems, like milk steamers A test should be small, encapsulated, stand-alone Mock objects allow you to use fake objects Stubs can be used to return hard-coded results Using them is actually (somewhat) easy with PHPUnit Inspiring people to share
  • 36. FLOW3 + TDD = FUN FLOW3 makes Dependency Injection easy With Dependency Injection you can do proper unit testing Proper unit tests • help produce reliable code • make refactoring possible • make you feel better All this means more fun while coding Inspiring people to share
  • 37. Questions! Inspiring people to share
  • 38. Literature Test-Driven Development By Example Kent Beck, Addison-Wesley Continuous Integration – Improving Software Quality and Reducing Risk Paul M. Duvall, Addison-Wesley xUnit Test Patterns – Refactoring Test Code Gerard Meszaros, Addison-Wesley Inspiring people to share
  • 39. Links FLOW3 http://flow3.typo3.org/ PHPUnit http://www.phpunit.de/ TDD in Wikipedia http://en.wikipedia.org/wiki/Test-driven_development Inspiring people to share