TDD, a starting point
A PHP perspective...
Francesco Fullone, Ideato.it
ff AT ideato.it
Who am I

Francesco Fullone aka Fullo

- PHP developer since 1999
-             President
-      and Open Source Evangelist
- CEO @


- Nerd and geek
TDD ?
Test Driven
Development
Why test the code?
Developers are humans,
Humans are error-prone.
Software changes
    and grows.
We need to confirm that
 the code is working
  after any changes.
xUNIT TDD approach.
TDD, a starting point...
PHPUnit.de
Easy to write
Easy to read
  Isolated
Composable
An example:
The bowling kata
  (courtesy grabbed from Sebastian Bergmann)
10 frames

2 rolls to knock down the 10 pins

Score for a frame is the number of pins
knocked down

Bonus for a spare (all 10 pins knocked down in
two tries): next roll

Bonus for a strike (all 10 pins knocked down in
one try): next two rolls

Extra rolls for spare or strike in the 10th frame
<?php
require_once 'BowlingGame.php';

class BowlingGameTest extends PHPUnit_Framework_TestCase
{




}
?>
<?php
require_once 'BowlingGame.php';

class BowlingGameTest extends
PHPUnit_Framework_TestCase
{
  public function testScoreForGutterGameIs0()
  {

    }

}
<?php
require_once 'BowlingGame.php';

class BowlingGameTest extends
PHPUnit_Framework_TestCase
{
  public function testScoreForGutterGameIs0()
  {
     $game = new BowlingGame;
     for ($i = 0; $i < 20; $i++) {
          $game->roll(0);
     }

        $this->assertEquals(0, $game->score());
    }

}
fullo@teletran ~ % phpunit --skeleton-class BowlingGameTest
PHPUnit 3.4.2 by Sebastian Bergmann.
Wrote skeleton for "BowlingGame" to "BowlingGame.php".

<?php
class BowlingGame
{

    public function roll()
    {
       // Remove the following line when you
       // implement this method.

        throw new RuntimeException('Not yet implemented.');
    }

    public function score()
    {
       // Remove the following line when you
       // implement this method.

        throw new RuntimeException('Not yet implemented.');
    }
}
fullo@teletran ~ % phpunit BowlingGameTest
PHPUnit 3.4.2 by Sebastian Bergmann.

E

Time: 0 seconds

There was 1 error:

1) BowlingGameTest::testScoreForGutterGameIs0
RuntimeException: Not yet implemented.
/home/fullo/BowlingGame.php:10
/home/fullo/BowlingGameTest.php:11

FAILURES!
Tests: 1, Assertions: 0, Errors: 1.
<?php
class BowlingGame
{

     public function roll($pins)
     {

     }

     public function score()
     {
        return 0;
     }
}
?>
fullo@teletran ~ % phpunit --colors BowlingGameTest
PHPUnit 3.4.2 by Sebastian Bergmann.

.

Time: 0 seconds

OK (1 test, 1 assertion)
We have to write the
tests when the code is
        fresh.
<?php
require_once 'BowlingGame.php';

class BowlingGameTest extends PHPUnit_Framework_TestCase
{
   // …

    public function testScoreForAllOnesIs20()
    {
      $game = new BowlingGame;

        for ($i = 0; $i < 20; $i++) {
            $game->roll(1);
        }

        $this->assertEquals(20, $game->score());
    }
}
fullo@teletran ~ % phpunit –colors
BowlingGameTest
PHPUnit 3.4.2 by Sebastian Bergmann.

.F

Time: 0 seconds

There was 1 failure:

1) BowlingGameTest::testScoreForAllOnesIs20
Failed asserting that <integer:0> matches
expected value <integer:20>.
/home/fullo/BowlingGameTest.php:25

FAILURES!
Tests: 2, Assertions: 2, Failures: 1.
<?php
class BowlingGame
{

    protected $rolls = array();

    public function roll($pins)
    {
       $this->rolls[] = $pins;
    }

    public function score()
    {
        return array_sum($this->rolls);
    }
}
<?php
require_once 'BowlingGame.php';

class BowlingGameTest extends PHPUnit_Framework_TestCase
{
   // …

    public function testScoreForOneSpareAnd3Is16()
    {
      $game = new BowlingGame;

        $game->roll(5);
        $game->roll(5);
        $game->roll(3);

        // a function to to roll X times
        $this->rollMany(17, 0);
        $this->assertEquals(16, $game->score());
    }

}
fullo@teletran ~ % phpunit –colors
BowlingGameTest
PHPUnit 3.4.2 by Sebastian Bergmann.

..F

Time: 0 seconds

There was 1 failure:

1) BowlingGameTest::testScoreForOneSpareAnd3is16
Failed asserting that <integer:13> matches
expected value <integer:16>.
/home/fullo/BowlingGameTest.php:33

FAILURES!
Tests: 2, Assertions: 2, Failures: 1.
Tests can serve
as an executable
  specification.
fullo@teletran ~ % phpunit --testdox BowlingGameTest
PHPUnit 3.4.2 by Sebastian Bergmann.

BowlingGame
[x] Score for gutter game is 0
[ ] Score for all ones is 20
[ ] Score for one spare and 3 is 16
[ ] Score for one strike and 3 and 4 is 24
[ ] Score for perfect game is 300
Why Unit Test.
To write only clean
 and useful code.
To easily iterate in
development design.
To check for
regressions.
?
For more info see Sebastian
     Bergmann's Bowling Kata
            Workshop!

http://www.slideshare.net/sebastian
  _bergmann/quality-assurance-in-
        php-projects-2164371
Francesco Fullone
     ff AT ideato.it
    skype: ffullone




 via Quinto Bucci 205
  47023 Cesena (FC)
    info AT ideato.it
     www.ideato.it

More Related Content

PDF
clap: Command line argument parser for Pharo
PPT
C Sharp Jn (3)
PDF
How to Vim - for beginners
PPT
Loops
DOCX
Bubble and selection
PDF
Test Driven Development Workshop
PPT
Practical Ext JS Debugging
PDF
C++ L07-Struct
clap: Command line argument parser for Pharo
C Sharp Jn (3)
How to Vim - for beginners
Loops
Bubble and selection
Test Driven Development Workshop
Practical Ext JS Debugging
C++ L07-Struct

What's hot (20)

PPT
PPTX
Test Fest 2009
PPTX
Project proposal presentation(tic tac-toe-game)
PPTX
Switch statement mcq
PDF
DEF CON 23 - COLIN O'FLYNN - dont whisper my chips
PDF
C&cpu
PDF
[LaravelConf Taiwan 2019] 編輯器之華山論劍
PDF
C++ L03-Control Structure
PPTX
Pegomock, a mocking framework for Go
PDF
Programming basics
PPTX
Session05 iteration structure
PPTX
Android taipei 20160225 淺談closure
DOCX
Super keyword
PDF
Welcome to Modern C++
PDF
Elixir @ Paris.rb
PDF
C++ L04-Array+String
PDF
C++ L02-Conversion+enum+Operators
PPTX
Project in programming
PDF
The Code
Test Fest 2009
Project proposal presentation(tic tac-toe-game)
Switch statement mcq
DEF CON 23 - COLIN O'FLYNN - dont whisper my chips
C&cpu
[LaravelConf Taiwan 2019] 編輯器之華山論劍
C++ L03-Control Structure
Pegomock, a mocking framework for Go
Programming basics
Session05 iteration structure
Android taipei 20160225 淺談closure
Super keyword
Welcome to Modern C++
Elixir @ Paris.rb
C++ L04-Array+String
C++ L02-Conversion+enum+Operators
Project in programming
The Code
Ad

Similar to TDD, a starting point... (20)

PDF
New Features PHPUnit 3.3 - Sebastian Bergmann
 
PDF
Quality Assurance in PHP projects - Sebastian Bergmann
 
PDF
Introduction to Unit Testing with PHPUnit
PPT
Unit Testing using PHPUnit
KEY
Php Unit With Zend Framework Zendcon09
KEY
PHPUnit testing to Zend_Test
PDF
Fighting Fear-Driven-Development With PHPUnit
PPTX
Test in action week 2
PPT
Test Driven Development with PHPUnit
PPTX
PHPUnit: from zero to hero
KEY
Developer testing 101: Become a Testing Fanatic
PDF
Unit testing in PHP
ZIP
Test
PPTX
Unit Testng with PHP Unit - A Step by Step Training
PDF
PHPunit and you
PPT
Unit testing
PPT
Unit testing php-unit - phing - selenium_v2
PPT
Zend Framework 2 - PHPUnit
PDF
13 PHPUnit #burningkeyboards
PDF
Joomla Code Quality Control and Automation Testing
New Features PHPUnit 3.3 - Sebastian Bergmann
 
Quality Assurance in PHP projects - Sebastian Bergmann
 
Introduction to Unit Testing with PHPUnit
Unit Testing using PHPUnit
Php Unit With Zend Framework Zendcon09
PHPUnit testing to Zend_Test
Fighting Fear-Driven-Development With PHPUnit
Test in action week 2
Test Driven Development with PHPUnit
PHPUnit: from zero to hero
Developer testing 101: Become a Testing Fanatic
Unit testing in PHP
Test
Unit Testng with PHP Unit - A Step by Step Training
PHPunit and you
Unit testing
Unit testing php-unit - phing - selenium_v2
Zend Framework 2 - PHPUnit
13 PHPUnit #burningkeyboards
Joomla Code Quality Control and Automation Testing
Ad

More from Francesco Fullone (20)

PDF
Life Cycle Design e Circular Economy: un caso reale
PDF
Okr istruzioni per l'uso - devfest
PDF
OKR, sono veramente utili alla mia azienda?
PDF
Okr per community - icms
PDF
Open Governance, un caso reale
PDF
A recommendation engine for your applications
PDF
A recommendation engine for your applications
PDF
Con te non ci lavoro
PDF
Con te non ci lavoro
PDF
Continuous budgeting
PDF
Remote working istruzioni
PDF
Remote working istruzioni
PDF
MVP & Startup, with OpenSource Software and Microsoft Azure
PDF
Remote working istruzioni
PDF
Help yourself, grow an healthy ecosystem
PDF
Outsourcing, partners or suppliers?
PDF
From brainstorming to product development
PDF
Compromises and not solution
PDF
PHP Goes Enterprise
PDF
your browser, my storage
Life Cycle Design e Circular Economy: un caso reale
Okr istruzioni per l'uso - devfest
OKR, sono veramente utili alla mia azienda?
Okr per community - icms
Open Governance, un caso reale
A recommendation engine for your applications
A recommendation engine for your applications
Con te non ci lavoro
Con te non ci lavoro
Continuous budgeting
Remote working istruzioni
Remote working istruzioni
MVP & Startup, with OpenSource Software and Microsoft Azure
Remote working istruzioni
Help yourself, grow an healthy ecosystem
Outsourcing, partners or suppliers?
From brainstorming to product development
Compromises and not solution
PHP Goes Enterprise
your browser, my storage

Recently uploaded (20)

PDF
Developing a website for English-speaking practice to English as a foreign la...
PDF
CloudStack 4.21: First Look Webinar slides
PDF
sbt 2.0: go big (Scala Days 2025 edition)
PPT
Galois Field Theory of Risk: A Perspective, Protocol, and Mathematical Backgr...
PPT
Geologic Time for studying geology for geologist
PDF
Produktkatalog für HOBO Datenlogger, Wetterstationen, Sensoren, Software und ...
PDF
Taming the Chaos: How to Turn Unstructured Data into Decisions
PDF
“A New Era of 3D Sensing: Transforming Industries and Creating Opportunities,...
PPT
Module 1.ppt Iot fundamentals and Architecture
PDF
1 - Historical Antecedents, Social Consideration.pdf
PDF
Hybrid horned lizard optimization algorithm-aquila optimizer for DC motor
PDF
Enhancing plagiarism detection using data pre-processing and machine learning...
PDF
Comparative analysis of machine learning models for fake news detection in so...
PDF
OpenACC and Open Hackathons Monthly Highlights July 2025
PDF
A proposed approach for plagiarism detection in Myanmar Unicode text
PPTX
MicrosoftCybserSecurityReferenceArchitecture-April-2025.pptx
PDF
Credit Without Borders: AI and Financial Inclusion in Bangladesh
PDF
sustainability-14-14877-v2.pddhzftheheeeee
PDF
Flame analysis and combustion estimation using large language and vision assi...
PPTX
Configure Apache Mutual Authentication
Developing a website for English-speaking practice to English as a foreign la...
CloudStack 4.21: First Look Webinar slides
sbt 2.0: go big (Scala Days 2025 edition)
Galois Field Theory of Risk: A Perspective, Protocol, and Mathematical Backgr...
Geologic Time for studying geology for geologist
Produktkatalog für HOBO Datenlogger, Wetterstationen, Sensoren, Software und ...
Taming the Chaos: How to Turn Unstructured Data into Decisions
“A New Era of 3D Sensing: Transforming Industries and Creating Opportunities,...
Module 1.ppt Iot fundamentals and Architecture
1 - Historical Antecedents, Social Consideration.pdf
Hybrid horned lizard optimization algorithm-aquila optimizer for DC motor
Enhancing plagiarism detection using data pre-processing and machine learning...
Comparative analysis of machine learning models for fake news detection in so...
OpenACC and Open Hackathons Monthly Highlights July 2025
A proposed approach for plagiarism detection in Myanmar Unicode text
MicrosoftCybserSecurityReferenceArchitecture-April-2025.pptx
Credit Without Borders: AI and Financial Inclusion in Bangladesh
sustainability-14-14877-v2.pddhzftheheeeee
Flame analysis and combustion estimation using large language and vision assi...
Configure Apache Mutual Authentication

TDD, a starting point...

  • 1. TDD, a starting point A PHP perspective... Francesco Fullone, Ideato.it ff AT ideato.it
  • 2. Who am I Francesco Fullone aka Fullo - PHP developer since 1999 - President - and Open Source Evangelist - CEO @ - Nerd and geek
  • 5. Why test the code?
  • 6. Developers are humans, Humans are error-prone.
  • 7. Software changes and grows.
  • 8. We need to confirm that the code is working after any changes.
  • 12. Easy to write Easy to read Isolated Composable
  • 13. An example: The bowling kata (courtesy grabbed from Sebastian Bergmann)
  • 14. 10 frames 2 rolls to knock down the 10 pins Score for a frame is the number of pins knocked down Bonus for a spare (all 10 pins knocked down in two tries): next roll Bonus for a strike (all 10 pins knocked down in one try): next two rolls Extra rolls for spare or strike in the 10th frame
  • 15. <?php require_once 'BowlingGame.php'; class BowlingGameTest extends PHPUnit_Framework_TestCase { } ?>
  • 16. <?php require_once 'BowlingGame.php'; class BowlingGameTest extends PHPUnit_Framework_TestCase { public function testScoreForGutterGameIs0() { } }
  • 17. <?php require_once 'BowlingGame.php'; class BowlingGameTest extends PHPUnit_Framework_TestCase { public function testScoreForGutterGameIs0() { $game = new BowlingGame; for ($i = 0; $i < 20; $i++) { $game->roll(0); } $this->assertEquals(0, $game->score()); } }
  • 18. fullo@teletran ~ % phpunit --skeleton-class BowlingGameTest PHPUnit 3.4.2 by Sebastian Bergmann. Wrote skeleton for "BowlingGame" to "BowlingGame.php". <?php class BowlingGame { public function roll() { // Remove the following line when you // implement this method. throw new RuntimeException('Not yet implemented.'); } public function score() { // Remove the following line when you // implement this method. throw new RuntimeException('Not yet implemented.'); } }
  • 19. fullo@teletran ~ % phpunit BowlingGameTest PHPUnit 3.4.2 by Sebastian Bergmann. E Time: 0 seconds There was 1 error: 1) BowlingGameTest::testScoreForGutterGameIs0 RuntimeException: Not yet implemented. /home/fullo/BowlingGame.php:10 /home/fullo/BowlingGameTest.php:11 FAILURES! Tests: 1, Assertions: 0, Errors: 1.
  • 20. <?php class BowlingGame { public function roll($pins) { } public function score() { return 0; } } ?>
  • 21. fullo@teletran ~ % phpunit --colors BowlingGameTest PHPUnit 3.4.2 by Sebastian Bergmann. . Time: 0 seconds OK (1 test, 1 assertion)
  • 22. We have to write the tests when the code is fresh.
  • 23. <?php require_once 'BowlingGame.php'; class BowlingGameTest extends PHPUnit_Framework_TestCase { // … public function testScoreForAllOnesIs20() { $game = new BowlingGame; for ($i = 0; $i < 20; $i++) { $game->roll(1); } $this->assertEquals(20, $game->score()); } }
  • 24. fullo@teletran ~ % phpunit –colors BowlingGameTest PHPUnit 3.4.2 by Sebastian Bergmann. .F Time: 0 seconds There was 1 failure: 1) BowlingGameTest::testScoreForAllOnesIs20 Failed asserting that <integer:0> matches expected value <integer:20>. /home/fullo/BowlingGameTest.php:25 FAILURES! Tests: 2, Assertions: 2, Failures: 1.
  • 25. <?php class BowlingGame { protected $rolls = array(); public function roll($pins) { $this->rolls[] = $pins; } public function score() { return array_sum($this->rolls); } }
  • 26. <?php require_once 'BowlingGame.php'; class BowlingGameTest extends PHPUnit_Framework_TestCase { // … public function testScoreForOneSpareAnd3Is16() { $game = new BowlingGame; $game->roll(5); $game->roll(5); $game->roll(3); // a function to to roll X times $this->rollMany(17, 0); $this->assertEquals(16, $game->score()); } }
  • 27. fullo@teletran ~ % phpunit –colors BowlingGameTest PHPUnit 3.4.2 by Sebastian Bergmann. ..F Time: 0 seconds There was 1 failure: 1) BowlingGameTest::testScoreForOneSpareAnd3is16 Failed asserting that <integer:13> matches expected value <integer:16>. /home/fullo/BowlingGameTest.php:33 FAILURES! Tests: 2, Assertions: 2, Failures: 1.
  • 28. Tests can serve as an executable specification.
  • 29. fullo@teletran ~ % phpunit --testdox BowlingGameTest PHPUnit 3.4.2 by Sebastian Bergmann. BowlingGame [x] Score for gutter game is 0 [ ] Score for all ones is 20 [ ] Score for one spare and 3 is 16 [ ] Score for one strike and 3 and 4 is 24 [ ] Score for perfect game is 300
  • 31. To write only clean and useful code.
  • 32. To easily iterate in development design.
  • 34. ?
  • 35. For more info see Sebastian Bergmann's Bowling Kata Workshop! http://www.slideshare.net/sebastian _bergmann/quality-assurance-in- php-projects-2164371
  • 36. Francesco Fullone ff AT ideato.it skype: ffullone via Quinto Bucci 205 47023 Cesena (FC) info AT ideato.it www.ideato.it