TDD, a starting point...
-
Upload
francesco-fullone -
Category
Technology
-
view
2.041 -
download
1
description
Transcript of TDD, a starting point...
![Page 1: TDD, a starting point...](https://reader033.fdocuments.net/reader033/viewer/2022060109/55580ef5d8b42a5e468b476f/html5/thumbnails/1.jpg)
TDD, a starting pointA PHP perspective...
Francesco Fullone, Ideato.itff AT ideato.it
![Page 2: TDD, a starting point...](https://reader033.fdocuments.net/reader033/viewer/2022060109/55580ef5d8b42a5e468b476f/html5/thumbnails/2.jpg)
Who am I
Francesco Fullone aka Fullo
- PHP developer since 1999
- President
- and Open Source Evangelist
- CEO @
- Nerd and geek
![Page 3: TDD, a starting point...](https://reader033.fdocuments.net/reader033/viewer/2022060109/55580ef5d8b42a5e468b476f/html5/thumbnails/3.jpg)
TDD ?
![Page 4: TDD, a starting point...](https://reader033.fdocuments.net/reader033/viewer/2022060109/55580ef5d8b42a5e468b476f/html5/thumbnails/4.jpg)
Test Driven Development
![Page 5: TDD, a starting point...](https://reader033.fdocuments.net/reader033/viewer/2022060109/55580ef5d8b42a5e468b476f/html5/thumbnails/5.jpg)
Why test the code?
![Page 6: TDD, a starting point...](https://reader033.fdocuments.net/reader033/viewer/2022060109/55580ef5d8b42a5e468b476f/html5/thumbnails/6.jpg)
Developers are humans, Humans are error-prone.
![Page 7: TDD, a starting point...](https://reader033.fdocuments.net/reader033/viewer/2022060109/55580ef5d8b42a5e468b476f/html5/thumbnails/7.jpg)
Software changes and grows.
![Page 8: TDD, a starting point...](https://reader033.fdocuments.net/reader033/viewer/2022060109/55580ef5d8b42a5e468b476f/html5/thumbnails/8.jpg)
We need to confirm that the code is working after any changes.
![Page 9: TDD, a starting point...](https://reader033.fdocuments.net/reader033/viewer/2022060109/55580ef5d8b42a5e468b476f/html5/thumbnails/9.jpg)
xUNIT TDD approach.
![Page 10: TDD, a starting point...](https://reader033.fdocuments.net/reader033/viewer/2022060109/55580ef5d8b42a5e468b476f/html5/thumbnails/10.jpg)
![Page 11: TDD, a starting point...](https://reader033.fdocuments.net/reader033/viewer/2022060109/55580ef5d8b42a5e468b476f/html5/thumbnails/11.jpg)
PHPUnit.de
![Page 12: TDD, a starting point...](https://reader033.fdocuments.net/reader033/viewer/2022060109/55580ef5d8b42a5e468b476f/html5/thumbnails/12.jpg)
Easy to writeEasy to read
IsolatedComposable
![Page 13: TDD, a starting point...](https://reader033.fdocuments.net/reader033/viewer/2022060109/55580ef5d8b42a5e468b476f/html5/thumbnails/13.jpg)
An example: The bowling kata
(courtesy grabbed from Sebastian Bergmann)
![Page 14: TDD, a starting point...](https://reader033.fdocuments.net/reader033/viewer/2022060109/55580ef5d8b42a5e468b476f/html5/thumbnails/14.jpg)
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
![Page 15: TDD, a starting point...](https://reader033.fdocuments.net/reader033/viewer/2022060109/55580ef5d8b42a5e468b476f/html5/thumbnails/15.jpg)
<?phprequire_once 'BowlingGame.php';
class BowlingGameTest extends PHPUnit_Framework_TestCase{
}?>
![Page 16: TDD, a starting point...](https://reader033.fdocuments.net/reader033/viewer/2022060109/55580ef5d8b42a5e468b476f/html5/thumbnails/16.jpg)
<?phprequire_once 'BowlingGame.php';
class BowlingGameTest extends PHPUnit_Framework_TestCase{
public function testScoreForGutterGameIs0(){
}
}
![Page 17: TDD, a starting point...](https://reader033.fdocuments.net/reader033/viewer/2022060109/55580ef5d8b42a5e468b476f/html5/thumbnails/17.jpg)
<?phprequire_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());}
}
![Page 18: TDD, a starting point...](https://reader033.fdocuments.net/reader033/viewer/2022060109/55580ef5d8b42a5e468b476f/html5/thumbnails/18.jpg)
fullo@teletran ~ % phpunit --skeleton-class BowlingGameTestPHPUnit 3.4.2 by Sebastian Bergmann.Wrote skeleton for "BowlingGame" to "BowlingGame.php".
<?phpclass 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.'); }}
![Page 19: TDD, a starting point...](https://reader033.fdocuments.net/reader033/viewer/2022060109/55580ef5d8b42a5e468b476f/html5/thumbnails/19.jpg)
fullo@teletran ~ % phpunit BowlingGameTestPHPUnit 3.4.2 by Sebastian Bergmann.
E
Time: 0 seconds
There was 1 error:
1) BowlingGameTest::testScoreForGutterGameIs0RuntimeException: Not yet implemented./home/fullo/BowlingGame.php:10/home/fullo/BowlingGameTest.php:11
FAILURES!Tests: 1, Assertions: 0, Errors: 1.
![Page 20: TDD, a starting point...](https://reader033.fdocuments.net/reader033/viewer/2022060109/55580ef5d8b42a5e468b476f/html5/thumbnails/20.jpg)
<?phpclass BowlingGame{
public function roll($pins) { }
public function score() {
return 0; }}?>
![Page 21: TDD, a starting point...](https://reader033.fdocuments.net/reader033/viewer/2022060109/55580ef5d8b42a5e468b476f/html5/thumbnails/21.jpg)
fullo@teletran ~ % phpunit --colors BowlingGameTestPHPUnit 3.4.2 by Sebastian Bergmann.
.
Time: 0 seconds
OK (1 test, 1 assertion)
![Page 22: TDD, a starting point...](https://reader033.fdocuments.net/reader033/viewer/2022060109/55580ef5d8b42a5e468b476f/html5/thumbnails/22.jpg)
We have to write the tests when the code is
fresh.
![Page 23: TDD, a starting point...](https://reader033.fdocuments.net/reader033/viewer/2022060109/55580ef5d8b42a5e468b476f/html5/thumbnails/23.jpg)
<?phprequire_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());}
}
![Page 24: TDD, a starting point...](https://reader033.fdocuments.net/reader033/viewer/2022060109/55580ef5d8b42a5e468b476f/html5/thumbnails/24.jpg)
fullo@teletran ~ % phpunit –colors BowlingGameTestPHPUnit 3.4.2 by Sebastian Bergmann.
.F
Time: 0 seconds
There was 1 failure:
1) BowlingGameTest::testScoreForAllOnesIs20Failed asserting that <integer:0> matches expected value <integer:20>./home/fullo/BowlingGameTest.php:25
FAILURES!Tests: 2, Assertions: 2, Failures: 1.
![Page 25: TDD, a starting point...](https://reader033.fdocuments.net/reader033/viewer/2022060109/55580ef5d8b42a5e468b476f/html5/thumbnails/25.jpg)
<?phpclass BowlingGame{
protected $rolls = array();
public function roll($pins) { $this->rolls[] = $pins; }
public function score() {
return array_sum($this->rolls); } }
![Page 26: TDD, a starting point...](https://reader033.fdocuments.net/reader033/viewer/2022060109/55580ef5d8b42a5e468b476f/html5/thumbnails/26.jpg)
<?phprequire_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());
}
}
![Page 27: TDD, a starting point...](https://reader033.fdocuments.net/reader033/viewer/2022060109/55580ef5d8b42a5e468b476f/html5/thumbnails/27.jpg)
fullo@teletran ~ % phpunit –colors BowlingGameTestPHPUnit 3.4.2 by Sebastian Bergmann.
..F
Time: 0 seconds
There was 1 failure:
1) BowlingGameTest::testScoreForOneSpareAnd3is16Failed asserting that <integer:13> matches expected value <integer:16>./home/fullo/BowlingGameTest.php:33
FAILURES!Tests: 2, Assertions: 2, Failures: 1.
![Page 28: TDD, a starting point...](https://reader033.fdocuments.net/reader033/viewer/2022060109/55580ef5d8b42a5e468b476f/html5/thumbnails/28.jpg)
Tests can serve as an executable
specification.
![Page 29: TDD, a starting point...](https://reader033.fdocuments.net/reader033/viewer/2022060109/55580ef5d8b42a5e468b476f/html5/thumbnails/29.jpg)
fullo@teletran ~ % phpunit --testdox BowlingGameTestPHPUnit 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
![Page 30: TDD, a starting point...](https://reader033.fdocuments.net/reader033/viewer/2022060109/55580ef5d8b42a5e468b476f/html5/thumbnails/30.jpg)
Why Unit Test.
![Page 31: TDD, a starting point...](https://reader033.fdocuments.net/reader033/viewer/2022060109/55580ef5d8b42a5e468b476f/html5/thumbnails/31.jpg)
To write only clean and useful code.
![Page 32: TDD, a starting point...](https://reader033.fdocuments.net/reader033/viewer/2022060109/55580ef5d8b42a5e468b476f/html5/thumbnails/32.jpg)
To easily iterate in development design.
![Page 33: TDD, a starting point...](https://reader033.fdocuments.net/reader033/viewer/2022060109/55580ef5d8b42a5e468b476f/html5/thumbnails/33.jpg)
To check for regressions.
![Page 34: TDD, a starting point...](https://reader033.fdocuments.net/reader033/viewer/2022060109/55580ef5d8b42a5e468b476f/html5/thumbnails/34.jpg)
?
![Page 35: TDD, a starting point...](https://reader033.fdocuments.net/reader033/viewer/2022060109/55580ef5d8b42a5e468b476f/html5/thumbnails/35.jpg)
For more info see Sebastian Bergmann's Bowling Kata
Workshop!
http://www.slideshare.net/sebastian_bergmann/quality-assurance-in-
php-projects-2164371
![Page 36: TDD, a starting point...](https://reader033.fdocuments.net/reader033/viewer/2022060109/55580ef5d8b42a5e468b476f/html5/thumbnails/36.jpg)
Francesco Fulloneff AT ideato.itskype: ffullone
via Quinto Bucci 20547023 Cesena (FC)
info AT ideato.itwww.ideato.it