Testing with PHPUnit and Selenium

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

3 comments

Comments 1 - 3 of 3 previous next Post a comment

  • + silvere silvere 10 months ago
    where can i download it?
    thanks.
  • + sebastian_bergmann Sebastian Bergmann 2 years ago
    This is the problem with publishing slides: people who have not seen the presentation make assumptions solely based on the slides.

    The Selenium part of this presentation is mostly done as a series of demos (Selenium IDE, Selenium RC, PHPUnit and Selenium RC).
  • + dandv Dan Dascalescu 2 years ago
    You can skip slides 49..87 in absolute safety, as they talk about bowling scoring algorithms and have very little to do with PHPUnit.

    Why is this deck called 'Testing with PHPUnit and Selenium' when precisely only slides 108..110 talk about Selenium?
Post a comment
Embed Video
Edit your comment Cancel

Notes on slide 1

Theme created by Sakari Koivunen and Henrik Omma Released under the LGPL license.

18 Favorites

Testing with PHPUnit and Selenium - Presentation Transcript

  1. Welcome! Testing with PHPUnit & Selenium Sebastian Bergmann http://sebastian-bergmann.de/ Max Horv á th studiVZ Ltd. November 5 th 2007
  2. Who are you?
    • Your experience with
      • PHP 5?
      • OOP?
      • Testing?
        • PHPUnit?
        • Selenium?
      • Software Metrics?
  3. Why test?
    • Companies develop more and more enterprise-critical applications with PHP.
    • Tests help to make sure that these applications work correctly.
  4. Debugging Sucks, Testing Rocks! Debugging Sucks! Testing Rocks!
  5. What needs testing?
    • Web Application
      • Backend
        • Business Logic
        • Reusable Components
      • Frontend
        • Form Processing, Templates, ...
        • “Rich Interfaces” with AJAX, JSON, ...
        • Feeds, Web Services, ...
  6. And how do we test it?
    • Web Application
      • Backend
        • Functional Testing of the business logic with Unit Tests
        • Reusable Components
          • External components should have their own unit tests.
      • Frontend
        • Acceptance Tests or System Tests that are run “in the browser”
        • Testing of feeds, web services, etc. with unit tests
        • Compatibility Tests for Operating System / Browser combinations
        • Performance Tests and Security Tests
  7. Testing Software
    • Component Tests
      • Executable code fragments, so called Unit Tests , test the correctness of parts, units , of the software ( system under test )
    • System Tests
      • Conducted on a complete, integrated system to evaluate the system's compliance with its specified requirements. (Wikipedia)
    • Non-Functional Tests
      • Performance, Stability, Security, ...
  8. Testing Software
    • Developer Tests
      • Ensure that the code works correctly
    • Acceptance Tests
      • Ensure that the code does what the customer wants
  9. How do you test code? <?php class Calculator { public function add ( $a , $b ) { return $a + $b ; } } ?>
  10. How do you test code? <?php $fixture = new Calculator ; print $fixture -> add ( 0 , 1 ); $fixture = new Calculator ; print $fixture -> add ( 1 , 0 ); ?> 1 1
    • The tests are not specified in the code.
    • The tests are not automatically evaluated.
  11. How do you test code?
    • The tests are specified in the code.
    • The tests are automatically evaluated.
    • The writing of tests is tedious.
    <?php $fixture = new Calculator ; print $fixture -> add ( 0 , 1 ) == 1 ? 'pass' : 'fail' ; $fixture = new Calculator ; print $fixture -> add ( 1 , 0 ) == 1 ? 'pass' : 'fail' ; ?> pass pass
  12. How do you test code?
    • The tests are automatically evaluated.
    • assertTrue() makes the writing of tests easy.
    • The test environment is not reusable.
    <?php $fixture = new Calculator ; assertTrue ( $fixture -> add ( 0 , 1 ) == 1 ); $fixture = new Calculator ; assertTrue ( $fixture -> add ( 1 , 0 ) == 1 ); function assertTrue ( $expression ) { if (! $expression ) { throw new Exception ( 'Assertion failed.' ); } } ?>
  13. Test Tools
    • To make (code) testing viable, good tool support is needed.
    • This is where a testing framework such as PHPUnit comes into play.
    • Requirements
      • Reusable test environment
      • Strict separation of production code and test code
      • Automatic execution of test code
      • Analysis of the result
      • Easy to learn to use and easy to use
  14. Test Tools for PHP / Web
    • Unit Tests
      • PHPUnit
      • SimpleTest
    • System Tests
      • Selenium
        • PHPUnit + Selenium
    • Non-Functional Tests
      • Performance, Load, Stress, Availability, ...
        • ab, httperf, JMeter, Grinder, OpenSTA, ...
      • Security
        • Chorizo
  15. PHPUnit: Webseite http://www.phpunit.de/
  16. PHPUnit: Installation
  17. The Bowling Game Kata
    • The game consists of 10 frames as shown below.
      • In each frame the player has two opportunities to knock down 10 pins.
      • The score for the frame is the total number of pins knocked down, plus bonuses for strikes and spares.
    Introduction: Scoring Bowling
  18. The Bowling Game Kata
    • A spare is when the player knocks down all 10 pins in two tries.
      • The bonus for that frame is the number of pins knocked down by the next roll.
      • So in frame 3 below, the score is 10 (the total number knocked down) plus a bonus of 5 (the number of pins knocked down on the next roll).
    Introduction: Scoring Bowling
  19. The Bowling Game Kata
    • A strike is when the player knocks down all 10 pins on his first try.
      • The bonus for that frame is the value of the next two balls rolled.
    Introduction: Scoring Bowling
  20. The Bowling Game Kata
    • In the tenth frame a player who rolls a spare or strike is allowed to roll the extra balls to complete the frame.
    • However no more than three balls can be rolled in tenth frame.
    Introduction: Scoring Bowling
  21. The Bowling Game Kata
    • Write a class named BowlingGame that has two methods
      • roll($pins) is called each time the player rolls a ball.
        • The argument is the number of pins knocked down.
      • score() is called only at the very end of the game and returns the total score for that game.
    Introduction: Requirements
  22. The Bowling Game Kata
    • A game has 10 frames.
      • A frame has 1 or 2 rolls.
      • The 10 th frame has 2 or 3 rolls and is different from all the other frames.
    • The score() method must iterate over all the frames and calculate all their scores.
      • The score for a spare or a strike depends on the frame's successor.
    Introduction: Design
  23. The Bowling Game Kata <?php require_once 'BowlingGame.php' ; class BowlingGameTest extends PHPUnit_Framework_TestCase { } The first test
  24. The Bowling Game Kata <?php require_once 'BowlingGame.php'; class BowlingGameTest extends PHPUnit_Framework_TestCase { public function testGutterGame () { $game = new BowlingGame ; } } The first test
  25. The Bowling Game Kata <?php class BowlingGame { } The first test
  26. The Bowling Game Kata [email_address] ~ % phpunit BowlingGameTest PHPUnit 3.2.0 by Sebastian Bergmann. . Time: 0 seconds OK (1 test) The first test
  27. The Bowling Game Kata <?php require_once 'BowlingGame.php'; class BowlingGameTest extends PHPUnit_Framework_TestCase { public function testGutterGame() { $game = new BowlingGame; for ( $i = 0 ; $i < 20 ; $i ++) { $game -> roll ( 0 ); } } } The first test
  28. The Bowling Game Kata <?php class BowlingGame { public function roll ( $pins ) { } } The first test
  29. The Bowling Game Kata [email_address] ~ % phpunit BowlingGameTest PHPUnit 3.2.0 by Sebastian Bergmann. . Time: 0 seconds OK (1 test) The first test
  30. The Bowling Game Kata <?php require_once 'BowlingGame.php'; class BowlingGameTest extends PHPUnit_Framework_TestCase { public function testGutterGame() { $game = new BowlingGame; for ($i = 0; $i < 20; $i++) { $game->roll(0); } $this -> assertEquals ( 0 , $game -> score ()); } } The first test
  31. The Bowling Game Kata <?php class BowlingGame { public function roll($pins) { } public function score () { return -1 ; } } The first test
  32. The Bowling Game Kata [email_address] ~ % phpunit BowlingGameTest PHPUnit 3.2.0 by Sebastian Bergmann. F Time: 0 seconds There was 1 failure: 1) testGutterGame(BowlingGameTest) Failed asserting that <integer:-1> matches expected value <integer:0>. /home/sb/BowlingGameTest.php:14 FAILURES! Tests: 1, Failures: 1. The first test
  33. The Bowling Game Kata <?php class BowlingGame { public function roll($pins) { } public function score() { return 0 ; } } The first test
  34. The Bowling Game Kata [email_address] ~ % phpunit BowlingGameTest PHPUnit 3.2.0 by Sebastian Bergmann. . Time: 0 seconds OK (1 test) The first test
  35. This slide contains material by Brady Kelly. Interlude Test-Driven Development
    • Method of designing software, not just a method of testing software
      • Test
        • What do we want X to do?
        • How do we want to tell X to do it?
        • How will we know when X has done it?
      • Code
        • How does X do it?
    • Tests drive the development
      • Tests written before code
      • No code without tests
  36. The Bowling Game Kata <?php require_once 'BowlingGame.php'; class BowlingGameTest extends PHPUnit_Framework_TestCase { // ... public function testAllOnes () { $game = new BowlingGame ; for ( $i = 0 ; $i < 20 ; $i++ ) { $game -> roll ( 1 ); } $this -> assertEquals ( 20 , $game -> score ()); } } The second test
  37. The Bowling Game Kata [email_address] ~ % phpunit BowlingGameTest PHPUnit 3.2.0 by Sebastian Bergmann. .F Time: 0 seconds There was 1 failure: 1) testAllOnes(BowlingGameTest) Failed asserting that <integer:0> matches expected value <integer:20>. /home/sb/BowlingGameTest.php:25 FAILURES! Tests: 2, Failures: 1. The second test
  38. The Bowling Game Kata <?php class BowlingGame { protected $score = 0 ; public function roll($pins) { $this -> score += $pins ; } public function score() { return $this -> score ; } } The second test
  39. The Bowling Game Kata [email_address] ~ % phpunit BowlingGameTest PHPUnit 3.2.0 by Sebastian Bergmann. .. Time: 0 seconds OK (2 tests) The second test
  40. The Bowling Game Kata <?php require_once 'BowlingGame.php' ; class BowlingGameTest extends PHPUnit_Framework_TestCase { // ... public function testAllOnes () { $game = new BowlingGame ; for ( $i = 0 ; $i < 20 ; $i++ ) { $game -> roll ( 1 ); } $this -> assertEquals ( 20 , $game -> score ()); } } The second test
  41. The Bowling Game Kata <?php require_once 'BowlingGame.php'; class BowlingGameTest extends PHPUnit_Framework_TestCase { // ... public function testAllOnes() { $game = new BowlingGame ; for ($i = 0; $i < 20; $i++) { $game->roll(1); } $this->assertEquals(20, $game->score()); } } The second test
  42. Interlude
    • One of the most time-consuming parts of writing tests is writing the code to set the world up in a known state and then return it to its original state when the test is complete.
    • This known state is called the fixture of the test.
    Test Fixture
  43. Interlude
    • In our BowlingGameTest example the test fixture was a single object.
    • Most of the time, though, the fixture will be more complex than a simple object, and the amount of code needed to set it up will grow accordingly.
    • The actual content of the test gets lost in the noise of setting up the fixture.
      • This problem gets even worse when you write several tests with similar fixtures.
    • PHPUnit supports sharing the setup code through the setUp() and tearDown() template methods.
    Test Fixture
  44. The Bowling Game Kata <?php require_once 'BowlingGame.php'; class BowlingGameTest extends PHPUnit_Framework_TestCase { protected $game ; protected function setUp () { $this -> game = new BowlingGame ; } // ... public function testAllOnes() { for ($i = 0; $i < 20; $i++) { $this -> game -> roll ( 1 ); } $this -> assertEquals ( 20 , $this -> game -> score ()); } // ... } The second test
  45. The Bowling Game Kata <?php require_once 'BowlingGame.php'; class BowlingGameTest extends PHPUnit_Framework_TestCase { protected $game; protected function setUp() { $this->game = new BowlingGame; } // ... public function testAllOnes() { for ( $i = 0 ; $i < 20 ; $i++ ) { $this -> game -> roll ( 1 ); } $this->assertEquals(20, $this->game->score()); } // ... } The second test
  46. The Bowling Game Kata <?php require_once 'BowlingGame.php'; class BowlingGameTest extends PHPUnit_Framework_TestCase { // ... protected function rollMany ( $n , $pins ) { for ( $i = 0 ; $i < $n ; $i++ ) { $this -> game -> roll ( $pins ); } } public function testGutterGame() { $this -> rollMany ( 20 , 0 ); $this->assertEquals(0, $this->game->score()); } public function testAllOnes() { $this -> rollMany ( 20 , 1 ); $this->assertEquals(20, $this->game->score()); } } The second test
  47. The Bowling Game Kata <?php require_once 'BowlingGame.php' ; class BowlingGameTest extends PHPUnit_Framework_TestCase { // ... protected function rollMany ( $n , $pins ) { for ( $i = 0 ; $i < $n ; $i++ ) { $this -> game -> roll ( $pins ); } } public function testGutterGame () { $this -> rollMany ( 20 , 0 ); $this -> assertEquals ( 0 , $this -> game -> score ()); } public function testAllOnes () { $this -> rollMany ( 20 , 1 ); $this -> assertEquals ( 20 , $this -> game -> score ()); } } The second test
  48. Interlude
    • All unit tests run correctly.
    • The code communicates its design principles.
    • The code contains no redundancies.
    • The code contains the minimal number of classes and methods.
    Refactoring
    • A code refactoring is any change to a computer program's code which improves its readability or simplifies its structure without changing its results. (Wikipedia)
  49. The Bowling Game Kata <?php require_once 'BowlingGame.php'; class BowlingGameTest extends PHPUnit_Framework_TestCase { // ... public function testOneSpare () { $this -> game -> roll ( 5 ); $this -> game -> roll ( 5 ); // Spare $this -> game -> roll ( 3 ); $this -> rollMany ( 17 , 0 ); $this -> assertEquals ( 16 , $this -> game -> score ()); } } The third test
  50. The Bowling Game Kata [email_address] ~ % phpunit BowlingGameTest PHPUnit 3.2.0 by Sebastian Bergmann. ..F Time: 0 seconds There was 1 failure: 1) testOneSpare(BowlingGameTest) Failed asserting that <integer:13> matches expected value <integer:16>. /home/sb/BowlingGameTest.php:38 FAILURES! Tests: 3, Failures: 1. The third test
  51. The Bowling Game Kata <?php class BowlingGame { protected $score = 0 ; public function roll ( $pins ) { $this -> score += $pins ; } public function score () { return $this -> score ; } } The third test
  52. The Bowling Game Kata <?php class BowlingGame { protected $score = 0; protected $rolls = array (); public function roll($pins) { $this -> score += $pins ; $this -> rolls [] = $pins ; } public function score() { return $this->score; } } The third test
  53. The Bowling Game Kata <?php class BowlingGame { protected $rolls = array(); public function roll($pins) { $this->rolls[] = $pins; } public function score() { $score = 0 ; foreach ( $this -> rolls as $roll ) { $score += $roll ; } return $score ; } } The third test
  54. The Bowling Game Kata [email_address] ~ % phpunit BowlingGameTest PHPUnit 3.2.0 by Sebastian Bergmann. ..F Time: 0 seconds There was 1 failure: 1) testOneSpare(BowlingGameTest) Failed asserting that <integer:13> matches expected value <integer:16>. /home/sb/BowlingGameTest.php:38 FAILURES! Tests: 3, Failures: 1. The third test
  55. The Bowling Game Kata <?php class BowlingGame { // ... public function score() { $score = 0; for ( $i = 0 ; $i < count ( $this -> rolls ); $i++ ) { if ( $this -> rolls [ $i ] + $this -> rolls [ $i + 1 ] == 10 ) { // ... } $score += $this -> rolls [ $i ]; } return $score; } } The third test
  56. The Bowling Game Kata <?php class BowlingGame { // ... public function score() { $score = 0; $i = 0 ; for ( $frame = 0 ; $frame < 10 ; $frame++ ) { $score += $this -> rolls [ $i ] + $this -> rolls [ $i + 1 ]; $i += 2 ; } return $score; } } The third test
  57. The Bowling Game Kata [email_address] ~ % phpunit BowlingGameTest PHPUnit 3.2.0 by Sebastian Bergmann. ..F Time: 0 seconds There was 1 failure: 1) testOneSpare(BowlingGameTest) Failed asserting that <integer:13> matches expected value <integer:16>. /home/sb/BowlingGameTest.php:38 FAILURES! Tests: 3, Failures: 1. The third test
  58. The Bowling Game Kata <?php class BowlingGame { // ... public function score() { $score = 0; $i = 0; for ($frame = 0; $frame < 10; $frame++) { // Spare if ( $this -> rolls [ $i ] + $this -> rolls [ $i + 1 ] == 10 ) { $score += 10 + $this -> rolls [ $i + 2 ]; } else { $score += $this -> rolls [ $i ] + $this -> rolls [ $i + 1 ]; } $i += 2 ; } return $score; } } The third test
  59. The Bowling Game Kata [email_address] ~ % phpunit BowlingGameTest PHPUnit 3.2.0 by Sebastian Bergmann. ... Time: 0 seconds OK (3 tests) The third test
  60. The Bowling Game Kata <?php class BowlingGame { // ... public function score () { $score = 0 ; $i = 0 ; for ( $frame = 0 ; $frame < 10 ; $frame ++) { // Spare if ( $this -> rolls [ $i ] + $this -> rolls [ $i + 1 ] == 10 ) { $score += 10 + $this -> rolls [ $i + 2 ]; } else { $score += $this -> rolls [ $i ] + $this -> rolls [ $i + 1 ]; } $i += 2 ; } return $score ; } } The third test
  61. The Bowling Game Kata <?php class BowlingGame { // ... public function score() { $score = 0; $i = 0 ; for ($frame = 0; $frame < 10; $frame++) { // Spare if ( $this -> rolls [ $i ] + $this -> rolls [ $i + 1 ] == 10 ) { $score += 10 + $this -> rolls [ $i + 2 ]; } else { $score += $this -> rolls [ $i ] + $this -> rolls [ $i + 1 ]; } $i += 2 ; } return $score; } } The third test
  62. The Bowling Game Kata <?php class BowlingGame { // ... public function score() { $score = 0; $frameIndex = 0 ; for ($frame = 0; $frame < 10; $frame++) { // Spare if ( $this -> rolls [ $frameIndex ] + $this -> rolls [ $frameIndex + 1 ] == 10 ) { $score += 10 + $this -> rolls [ $frameIndex + 2 ]; } else { $score += $this -> rolls [ $frameIndex ] + $this -> rolls [ $frameIndex + 1 ]; } $frameIndex += 2 ; } return $score; } } The third test
  63. The Bowling Game Kata <?php class BowlingGame { // ... public function score() { $score = 0; $frameIndex = 0; for ($frame = 0; $frame < 10; $frame++) { if ( $this -> isSpare ( $frameIndex )) { $score += 10 + $this->rolls[$frameIndex + 2]; } else { $score += $this->rolls[$frameIndex] + $this->rolls[$frameIndex + 1]; } $frameIndex += 2; } return $score; } protected function isSpare ( $frameIndex ) { return $this -> rolls [ $frameIndex ] + $this -> rolls [ $frameIndex + 1 ] == 10 ; } } The third test
  64. The Bowling Game Kata <?php require_once 'BowlingGame.php' ; class BowlingGameTest extends PHPUnit_Framework_TestCase { // ... public function testOneSpare () { $this -> game -> roll ( 5 ); $this -> game -> roll ( 5 ); // Spare $this -> game -> roll ( 3 ); $this -> rollMany ( 17 , 0 ); $this -> assertEquals ( 16 , $this -> game -> score ()); } } The third test
  65. The Bowling Game Kata <?php require_once 'BowlingGame.php'; class BowlingGameTest extends PHPUnit_Framework_TestCase { // ... public function testOneSpare() { $this -> game -> roll ( 5 ); $this -> game -> roll ( 5 ); // Spare $this->game->roll(3); $this->rollMany(17, 0); $this->assertEquals(16, $this->game->score()); } } The third test
  66. The Bowling Game Kata <?php require_once 'BowlingGame.php'; class BowlingGameTest extends PHPUnit_Framework_TestCase { // ... protected function rollSpare () { $this -> game -> roll ( 5 ); $this -> game -> roll ( 5 ); } // ... public function testOneSpare() { $this -> rollSpare (); $this->game->roll(3); $this->rollMany(17, 0); $this->assertEquals(16, $this->game->score()); } } The third test
  67. The Bowling Game Kata <?php require_once 'BowlingGame.php'; class BowlingGameTest extends PHPUnit_Framework_TestCase { // ... public function testOneStrike () { $this -> game -> roll ( 10 ); // Strike $this -> game -> roll ( 3 ); $this -> game -> roll ( 4 ); $this -> rollMany ( 17 , 0 ); $this -> assertEquals ( 24 , $this -> game -> score ()); } } The fourth test
  68. The Bowling Game Kata [email_address] ~ % phpunit BowlingGameTest PHPUnit 3.2.0 by Sebastian Bergmann. ...F Time: 0 seconds There was 1 failure: 1) testOneStrike(BowlingGameTest) Failed asserting that <integer:17> matches expected value <integer:24>. /home/sb/BowlingGameTest.php:52 FAILURES! Tests: 4, Failures: 1. The fourth test
  69. The Bowling Game Kata <?php class BowlingGame { // ... public function score () { $score = 0 ; $frameIndex = 0 ; for ( $frame = 0 ; $frame < 10 ; $frame++ ) { if ( $this -> isSpare ( $frameIndex )) { $score += 10 + $this -> rolls [ $frameIndex + 2 ]; } else { $score += $this -> rolls [ $frameIndex ] + $this -> rolls [ $frameIndex + 1 ]; } $frameIndex += 2 ; } return $score ; } } The fourth test
  70. The Bowling Game Kata <?php class BowlingGame { // ... public function score() { $score = 0; $frameIndex = 0; for ($frame = 0; $frame < 10; $frame++) { // Strike if ( $this -> rolls [ $frameIndex ] == 10 ) { $score += 10 + $this -> rolls [ $frameIndex + 1 ] + $this -> rolls [ $frameIndex + 2 ]; $frameIndex ++; } // ... } return $score; } } The fourth test
  71. The Bowling Game Kata [email_address] ~ % phpunit BowlingGameTest PHPUnit 3.2.0 by Sebastian Bergmann. .... Time: 0 seconds OK (4 tests) The fourth test
  72. The Bowling Game Kata <?php class BowlingGame { // ... public function score() { $score = 0; $frameIndex = 0; for ($frame = 0; $frame < 10; $frame++) { // Strike if ( $this -> rolls [ $frameIndex ] == 10 ) { $score += 10 + $this -> rolls [ $frameIndex + 1 ] + $this -> rolls [ $frameIndex + 2 ]; $frameIndex ++; } // ... } return $score; } } The fourth test
  73. The Bowling Game Kata <?php class BowlingGame { // ... public function score() { $score = 0; $frameIndex = 0; for ($frame = 0; $frame < 10; $frame++) { if ( $this -> isStrike ( $frameIndex )) { $score += 10 + $this -> rolls [ $frameIndex + 1 ] + $this -> rolls [ $frameIndex + 2 ]; $frameIndex ++; } // ... } return $score; } protected function isStrike ( $frameIndex ) { return $this -> rolls [ $frameIndex ] == 10 ; } } The fourth test
  74. The Bowling Game Kata <?php require_once 'BowlingGame.php'; class BowlingGameTest extends PHPUnit_Framework_TestCase { // ... public function testOneStrike () { $this -> game -> roll ( 10 ); // Strike $this -> game -> roll ( 3 ); $this -> game -> roll ( 4 ); $this -> rollMany ( 17 , 0 ); $this -> assertEquals ( 24 , $this -> game -> score ()); } } The fourth test
  75. The Bowling Game Kata <?php require_once 'BowlingGame.php'; class BowlingGameTest extends PHPUnit_Framework_TestCase { // ... protected function rollStrike () { $this -> game -> roll ( 10 ); } // ... public function testOneStrike () { $this -> rollStrike (); $this -> game -> roll ( 3 ); $this -> game -> roll ( 4 ); $this -> rollMany ( 17 , 0 ); $this -> assertEquals ( 24 , $this -> game -> score ()); } } The fourth test
  76. The Bowling Game Kata [email_address] ~ % phpunit BowlingGameTest PHPUnit 3.2.0 by Sebastian Bergmann. .... Time: 0 seconds OK (4 tests) The fourth test
  77. The Bowling Game Kata <?php require_once 'BowlingGame.php'; class BowlingGameTest extends PHPUnit_Framework_TestCase { // ... public function testPerfectGame () { $this -> rollMany ( 12 , 10 ); $this -> assertEquals ( 300 , $this -> game -> score ()); } } The fifth test
  78. The Bowling Game Kata [email_address] ~ % phpunit BowlingGameTest PHPUnit 3.2.0 by Sebastian Bergmann. ..... Time: 0 seconds OK (5 tests) The fifth test
  79. The Bowling Game Kata <?php class BowlingGame { protected $rolls = array (); public function roll ( $pins ) { $this -> rolls [] = $pins ; } // ... } Final version of the BowlingGame class
  80. The Bowling Game Kata <?php class BowlingGame { // ... protected function isSpare ( $frameIndex ) { return $this -> sumOfPinsInFrame ( $frameIndex ) == 10 ; } protected function isStrike ( $frameIndex ) { return $this -> rolls [ $frameIndex ] == 10 ; } protected function sumOfPinsInFrame ( $frameIndex ) { return $this -> rolls [ $frameIndex ] + $this -> rolls [ $frameIndex + 1 ]; } } Final version of the BowlingGame class
  81. The Bowling Game Kata <?php class BowlingGame { // ... protected function spareBonus ( $frameIndex ) { return $this -> rolls [ $frameIndex + 2 ]; } protected function strikeBonus ( $frameIndex ) { return $this -> rolls [ $frameIndex + 1 ] + $this -> rolls [ $frameIndex + 2 ]; } } Final version of the BowlingGame class
  82. The Bowling Game Kata <?php class BowlingGame { // ... public function score () { $score = 0 ; $frameIndex = 0 ; for ( $frame = 0 ; $frame < 10 ; $frame++ ) { if ( $this -> isStrike ( $frameIndex )) { $score += 10 + $this -> strikeBonus ( $frameIndex ); $frameIndex ++; } else if ( $this -> isSpare ( $frameIndex )) { $score += 10 + $this -> spareBonus ( $frameIndex ); $frameIndex += 2 ; } else { $score += $this -> sumOfPinsInFrame ( $frameIndex ); $frameIndex += 2 ; } } return $score ; } } Final version of the BowlingGame class
  83. The Bowling Game Kata [email_address] ~ % phpunit BowlingGameTest PHPUnit 3.2.0 by Sebastian Bergmann. ..... Time: 0 seconds OK (5 tests) The fifth test
  84. PHPUnit <?php require_once 'BowlingGame.php' ; class BowlingGameTest extends PHPUnit_Framework_TestCase { // ... public function testGutterGame () { // ... } public function testAllOnes () { // ... } public function testOneSpare () { // ... } public function testOneStrike () { // ... } public function testPerfectGame () { // ... } } Agile Documentation
  85. PHPUnit <?php require_once 'BowlingGame.php' ; class BowlingGameTest extends PHPUnit_Framework_TestCase { // ... public function testScoreForGutterGameIs0 () { // ... } public function testScoreForAllOnesIs20 () { // ... } public function testScoreForOneSpareIs16 () { // ... } public function testScoreForOneStrikeIs24 () { // ... } public function testScoreForPerfectGameIs300 () { // ... } } Agile Documentation
  86. PHPUnit [email_address] ~ % phpunit --testdox BowlingGameTest PHPUnit 3.2.0 by Sebastian Bergmann. BowlingGame - Score for gutter game is 0 - Score for all ones is 20 - Score for one spare is 16 - Score for one strike is 24 - Score for perfect game is 300 Agile Documentation
  87. PHPUnit [email_address] ~ % phpunit --report report BowlingGameTest PHPUnit 3.2.0 by Sebastian Bergmann. ..... Time: 0 seconds OK (5 tests) Generating report, this may take a moment. Code Coverage
  88. Organizing Test Suites
    • Application/
      • Package/
        • Class (Application/Package/Class.php)
        • ...
      • ...
      • Tests/
        • AllTests.php
        • Package/
          • AllTests.php
          • ClassTest (Application/Tests/Package/ClassTest.php)
  89. Organizing Test Suites <?php require_once 'PHPUnit/Framework.php' ; require_once 'Application/Tests/Package/AllTests.php' ; class AllTests { public static function suite () { $suite = new PHPUnit_Framework_TestSuite ( 'Project' ); $suite -> addTest ( Package_AllTests :: suite ()); return $suite ; } } ?>
  90. Organizing Test Suites <?php require_once 'PHPUnit/Framework.php' ; require_once 'Application/Tests/Package/ClassTest.php' ; class Package_AllTests { public static function suite () { $suite = new PHPUnit_Framework_TestSuite ( 'Package' ); $suite -> addTestSuite ( 'Package_ClassTest' ); return $suite ; } } ?>
  91. Annotations @test <?php class Specification extends PHPUnit_Framework_TestCase { /** * @test */ public function shouldDoSomething () { } /** * @test */ public function shouldDoSomethingElse () { } }
  92. Annotations @test [email_address] ~ % phpunit --testdox Specification PHPUnit 3.2.0 by Sebastian Bergmann. Specification - Should do something - Should do something else
  93. Annotations @assert <?php class Calculator { /** * @assert (1, 2) == 3 */ public function add ( $a , $b ) { return $a + $b ; } /** * @assert (2, 1) == 1 */ public function sub ( $a , $b ) { return $a - $b ; } }
  94. Annotations @assert [email_address] ~ % phpunit Calculator PHPUnit 3.2.0 by Sebastian Bergmann. .. Time: 0 seconds OK (2 tests)
  95. Annotations @group <?php class SomeTest extends PHPUnit_Framework_TestCase { /** * @group specification */ public function testSomething () { } /** * @group regresssion * @group bug2204 */ public function testSomethingElse () { } }
  96. Annotations @group [email_address] ~ % phpunit SomeTest PHPUnit 3.2.0 by Sebastian Bergmann. .. Time: 0 seconds OK (2 tests) [email_address] ~ % phpunit --group bug2204 SomeTest PHPUnit 3.2.0 by Sebastian Bergmann. . Time: 0 seconds OK (1 test)
  97. Annotations @dataProvider <?php class DataTest extends PHPUnit_Framework_TestCase { /** * @dataProvider provider */ public function testAdd ( $a , $b , $c ) { $this -> assertEquals ( $c , $a + $b ); } public static function provider () { return array( array( 0 , 0 , 0 ), array( 0 , 1 , 1 ), array( 1 , 0 , 1 ), array( 1 , 1 , 3 ) ); } }
  98. Annotations @dataProvider [email_address] ~ % phpunit DataTest PHPUnit 3.2.0 by Sebastian Bergmann. ...F Time: 0 seconds There was 1 failure: 1) testAdd(DataTest) with data (1, 1, 3) Failed asserting that <integer:2> matches expected value <integer:3>. /home/sb/DataTest.php:9 FAILURES! Tests: 4, Failures: 1.
  99. Logging XML <?xml version= &quot;1.0&quot; encoding= &quot;UTF-8 &quot; ?> <testsuites> <testsuite name= &quot;CalculatorTest&quot; file= &quot;/home/sb/CalculatorTest.php&quot; tests= &quot;5&quot; failures= &quot;1&quot; errors= &quot;0&quot; time= &quot;0.0021438598632812&quot; > <testcase name= &quot;testAdd&quot; class= &quot;CalculatorTest&quot; file= &quot;/home/sb/CalculatorTest.php&quot; time= &quot;0.00045299530029297&quot; /> <testcase name= &quot;testAdd2&quot; class= &quot;CalculatorTest&quot; file= &quot;/home/sb/CalculatorTest.php&quot; time= &quot;0.0003299713134765&quot; /> <testcase name= &quot;testAdd3&quot; class= &quot;CalculatorTest&quot; file= &quot;/home/sb/CalculatorTest.php&quot; time= &quot;0.0003290176391601&quot; /> <testcase name= &quot;testAdd4&quot; class= &quot;CalculatorTest&quot; file= &quot;/home/sb/CalculatorTest.php&quot; time= &quot;0.0003290176391601&quot; /> <testcase name= &quot;testAdd5&quot; class= &quot;CalculatorTest&quot; file= &quot;/home/sb/CalculatorTest.php&quot; time= &quot;0.00070285797119141&quot; > <failure type= &quot;PHPUnit_Framework_ExpectationFailedException&quot; > Failed asserting that &lt;integer:3&gt; is not equal to &lt;integer:3&gt;. /home/sb/CalculatorTest.php:39 </failure> </testcase> </testsuite> </testsuites>
  100. Logging JavaScript Object Notation (JSON) {&quot;event&quot;:&quot;suiteStart&quot;,&quot;suite&quot;:&quot;CalculatorTest&quot;,&quot;tests&quot;:5} {&quot;event&quot;:&quot;test&quot;,&quot;suite&quot;:&quot;CalculatorTest&quot;,&quot;test&quot;:&quot;testAdd(CalculatorTest)&quot;, &quot;status&quot;:&quot;pass&quot;,&quot;time&quot;:0.0004529953,&quot;trace&quot;:[],&quot;message&quot;:&quot;&quot;} {&quot;event&quot;:&quot;test&quot;,&quot;suite&quot;:&quot;CalculatorTest&quot;,&quot;test&quot;:&quot;testAdd2(CalculatorTest)&quot;, &quot;status&quot;:&quot;pass&quot;,&quot;time&quot;:0.000329971313,&quot;trace&quot;:[],&quot;message&quot;:&quot;&quot;} {&quot;event&quot;:&quot;test&quot;,&quot;suite&quot;:&quot;CalculatorTest&quot;,&quot;test&quot;:&quot;testAdd3(CalculatorTest)&quot;, &quot;status&quot;:&quot;pass&quot;,&quot;time&quot;:0.000329017639,&quot;trace&quot;:[],&quot;message&quot;:&quot;&quot;} {&quot;event&quot;:&quot;test&quot;,&quot;suite&quot;:&quot;CalculatorTest&quot;,&quot;test&quot;:&quot;testAdd4(CalculatorTest)&quot;, &quot;status&quot;:&quot;pass&quot;,&quot;time&quot;:0.000329017639,&quot;trace&quot;:[],&quot;message&quot;:&quot;&quot;} {&quot;event&quot;:&quot;test&quot;,&quot;suite&quot;:&quot;CalculatorTest&quot;,&quot;test&quot;:&quot;testAdd5(CalculatorTest)&quot;, &quot;status&quot;:&quot;fail&quot;,&quot;time&quot;:0.000702857971,&quot;trace&quot;:[],&quot;message&quot;:&quot;Failed asserting that <integer:3> is not equal to <integer:3>.&quot;}
  101. Logging Test Anything Protocol (TAP) # TestSuite &quot;CalculatorTest&quot; started. ok 1 - testAdd(CalculatorTest) ok 2 - testAdd2(CalculatorTest) ok 3 - testAdd3(CalculatorTest) ok 4 - testAdd4(CalculatorTest) not ok 5 - Failure: testAdd5(CalculatorTest) # TestSuite &quot;CalculatorTest&quot; ended. 1..5
  102. Logging GraphViz
  103. PHPUnit
    • Test Framework
      • Member of the xUnit family of test frameworks
      • Mock Objects
    • Integration
      • Selenium RC
      • Bamboo, Bitten, CruiseControl, ...
    • Even More Cool Stuff :-)
      • Code Coverage and Software Metrics
      • Test Database
      • Mutation Testing (GSoC07 project)
  104. Software Metrics
    • A software metric is a measure of some property of a piece of software or its specifications
      • Lines of Code
      • Cyclomatic Complexity
      • NPath Complexity
      • Change Risk Analysis and Predictions (CRAP)
      • ...
    • Violations of rules that are based on software metrics
  105. Software Metrics
    • Class Level
      • Class Size (CSZ)
      • Class Interface Size (CIS)
      • Attribute Inheritance Factor (AIF), Attribute Hiding Factor (AHF)
      • Method Inheritance Factor (MIF), Method Hiding Factor (MHF)
      • Polymorphism Factor (PF)
      • Afferent Coupling (Ce), Efferent Coupling (Ce)
        • Instability (I = Ce / (Ce+Ca))
      • Depth of Inheritance Tree (DIT)
    Abbreviated list of supported metrics
  106. Software Metrics
    • Class Level
      • Number of Children (NOC)
      • Number of Interfaces Implemented (IMPL)
      • Number of Variables (VARS)
      • Number of Non-Private Variables (VARSnp)
      • Number of Variables (VARSi)
      • Weighted Methods per Class (WMC)
      • Weighted Non-Private Methods per Class (WMCnp)
      • Weighted Inherited Methods per Class (WMCi)
    Abbreviated list of supported metrics
  107. Software Metrics
    • Project Level
      • Number of Interfaces (INTERFS)
      • Number of Abstract Classes (CLSa)
      • Number of Concrete Classes (CLSc)
      • Number of Classes (CLS)
      • Number of Root Classes (ROOTS)
      • Number of Leaf Classes (LEAFS)
      • Maximum Depth of Intheritance Tree (maxDIT)
    Abbreviated list of supported metrics
  108. Selenium
    • Selenium
      • Test web applications in a web browser
        • Browser Compatibility Testing
        • System Functional Testing
      • Runs in the browser
    • Selenium IDE
      • IDE for Selenium tests
        • Extension for Firefox
        • Record, execute, edit, debug tests in the browser
  109. Selenium
    • Selenium RC
      • Automated execution of Selenium tests
      • Tests can be specified in any language
        • PHP Bindings: PEAR Testing_Selenium
        • PHPUnit natively speaks the Selenium RC protocol
      • One test can be executed on multiple OS / Browser combinations
    Selenium RC
  110. Selenium Selenium RC
  111. Test-Level Setup <?php class MyTest extends PHPUnit_Framework_TestCase { protected function setUp () { print &quot; MyTest::setUp()&quot; ; } protected function tearDown () { print &quot; MyTest::tearDown()&quot; ; } public function testOne () { print &quot; MyTest::testOne()&quot; ; } public function testTwo () { print &quot; MyTest::testTwo()&quot; ; } } MyTest::setUp() MyTest::testOne() MyTest::tearDown() MyTest::setUp() MyTest::testTwo() MyTest::tearDown()
  112. Suite-Level Setup <?php require_once 'MyTest.php' ; class MySuite extends PHPUnit_Framework_TestSuite { public static function suite () { return new MySuite ( 'MyTest' ); } protected function setUp () { print &quot; MySuite::setUp()&quot; ; } protected function tearDown () { print &quot; MySuite::tearDown()&quot; ; } } MySuite::setUp() MyTest::setUp() MyTest::testOne() MyTest::tearDown() MyTest::setUp() MyTest::testTwo() MyTest::tearDown() MySuite::tearDown()
  113. Suite-Level Setup <?php require_once 'MyTest.php' ; class MySuite extends PHPUnit_Framework_TestSuite { public static function suite () { return new MySuite ( 'MyTest' ); } protected function setUp () { $this -> sharedFixture = 'something' ; } } Setting up a shared fixture
  114. Suite-Level Setup <?php class MyTest extends PHPUnit_Framework_TestCase { public function testOne () { $this -> assertEquals ( 'something' , $this -> sharedFixture ); } } Using a shared fixture
  115. Testing Object Interaction
    • Tests that only test one thing are more informative than tests where failure can come from many sources
    • How can you isolate your tests from external influences?
    • Simply put, by replacing the expensive, messy, unreliable, slow, complicated resources with stubs that are automatically generated for the purpose of your tests
    • For example, you can replace what is in reality a complicated computation with a constant, at least for the purposes of a single test
    Stubs
  116. Testing Object Interaction
    • Dummy
      • Not the real object
    • Fake
      • Usable for testing but not for real job
    • Stub
      • Fake that returns canned data
    • Spy
      • Stub that records called methods, etc.
    • Mock
      • Spy with expectations
    Terminology
  117. Testing Object Interaction <?php require_once 'PHPUnit/Framework.php' ; class StubTest extends PHPUnit_Framework_TestCase { public function testStub () { } } ?> Stubs
  118. Testing Object Interaction <?php require_once 'PHPUnit/Framework.php' ; class StubTest extends PHPUnit_Framework_TestCase { public function testStub () { $stub = $this -> getMock ( 'SomeClass' ); } } ?> Stubs
  119. Testing Object Interaction <?php require_once 'PHPUnit/Framework.php' ; class StubTest extends PHPUnit_Framework_TestCase { public function testStub () { $stub = $this -> getMock ( 'SomeClass' ); $stub -> expects ( $this -> any ()) -> method ( 'doSomething' ) -> will ( $this -> returnValue ( 'foo' )); } } ?> Stubs
  120. Testing Object Interaction <?php require_once 'PHPUnit/Framework.php' ; class StubTest extends PHPUnit_Framework_TestCase { public function testStub () { $stub = $this -> getMock ( 'SomeClass' ); $stub -> expects ( $this -> any ()) -> method ( 'doSomething' ) -> will ( $this -> returnValue ( 'foo' )); // Calling $stub->doSomething() will now return // 'foo'. } } ?> Stubs
  121. Testing Object Interaction
    • Mock Objects are simulated objects that mimic the behavior of real objects in controlled ways
    • We can create a mock object to test the behavior of some other object
    • The Mock Objects system that is integrated in PHPUnit provides a fluent interface to specify the behavior of and the expectations for the mock
    Mock Objects
  122. Testing Object Interaction <?php require_once 'PHPUnit/Framework.php' ; class ObserverTest extends PHPUnit_Framework_TestCase { public function testUpdateIsCalledOnce () { } } ?> Mock Objects
  123. Testing Object Interaction <?php require_once 'PHPUnit/Framework.php' ; class ObserverTest extends PHPUnit_Framework_TestCase { public function testUpdateIsCalledOnce () { $observer = $this -> getMock ( 'Observer' , array( 'update' ) ); } } ?> Mock Objects
  124. Testing Object Interaction <?php require_once 'PHPUnit/Framework.php' ; class ObserverTest extends PHPUnit_Framework_TestCase { public function testUpdateIsCalledOnce () { $observer = $this -> getMock ( 'Observer' , array( 'update' ) ); $observer -> expects ( $this -> once ()) -> method ( 'update' ) -> with ( $this -> equalTo ( 'something' )); } } ?> Mock Objects
  125. Testing Object Interaction <?php require_once 'PHPUnit/Framework.php' ; class ObserverTest extends PHPUnit_Framework_TestCase { public function testUpdateIsCalledOnce () { $observer = $this -> getMock ( 'Observer' , array( 'update' ) ); $observer -> expects ( $this -> once ()) -> method ( 'update' ) -> with ( $this -> equalTo ( 'something' )); $subject = new Subject ; $subject -> attach ( $observer ); $subject -> doSomething (); } } ?> Mock Objects
  126. Database Testing
    • Michael Lively Jr. has ported the DbUnit extension for JUnit to PHPUnit
    • PHPUnit_Extensions_Database_TestCase
      • is used to test database-driven projects and
      • puts your database into a known state between test runs
        • This avoids problems with one test corrupting the database for other tests
      • has the ability to export and import your database data to and from XML datasets
  127. Database Testing <?php require_once 'PHPUnit/Extensions/Database/TestCase.php' ; class BankAccountDBTest extends PHPUnit_Extensions_Database_TestCase { } The BankAccountDB example
  128. Database Testing <?php require_once 'PHPUnit/Extensions/Database/TestCase.php' ; class BankAccountDBTest extends PHPUnit_Extensions_Database_TestCase { protected $pdo ; public function __construct () { $this -> pdo = new PDO ( 'sqlite::memory:' ); BankAccount :: createTable ( $this->pdo ); } } The BankAccountDB example
  129. Database Testing <?php require_once 'PHPUnit/Extensions/Database/TestCase.php' ; class BankAccountDBTest extends PHPUnit_Extensions_Database_TestCase { protected $pdo ; public function __construct () { $this -> pdo = new PDO ( 'sqlite::memory:' ); BankAccount :: createTable ( $this->pdo ); } protected function getConnection () { return $this -> createDefaultDBConnection ( $this -> pdo , 'sqlite' ); } } The BankAccountDB example
  130. Database Testing <?php require_once 'PHPUnit/Extensions/Database/TestCase.php' ; class BankAccountDBTest extends PHPUnit_Extensions_Database_TestCase { protected $pdo ; public function __construct () { $this -> pdo = new PDO ( 'sqlite::memory:' ); BankAccount :: createTable ( $this->pdo ); } protected function getConnection () { return $this -> createDefaultDBConnection ( $this -> pdo , 'sqlite' ); } protected function getDataSet () { return $this -> createFlatXMLDataSet ( '/path/to/seed.xml' ); } } The BankAccountDB example
  131. Database Testing <dataset> <account account_number= &quot;15934903649620486&quot; balance= &quot;100.00&quot; /> <account account_number= &quot;15936487230215067&quot; balance= &quot;1216.00&quot; /> <account account_number= &quot;12348612357236185&quot; balance= &quot;89.00&quot; /> <account account_number= &quot;15936487230215067&quot; balance= &quot;1216.00&quot; /> </dataset> The BankAccountDB example
  132. Database Testing <?php require_once 'PHPUnit/Extensions/Database/TestCase.php' ; class BankAccountDBTest extends PHPUnit_Extensions_Database_TestCase { // ... public function testNewAccount () { } } The BankAccountDB example
  133. Database Testing <?php require_once 'PHPUnit/Extensions/Database/TestCase.php' ; class BankAccountDBTest extends PHPUnit_Extensions_Database_TestCase { // ... public function testNewAccount () { $ba = new BankAccount ( '12345678912345678' , $this -> pdo ); } } The BankAccountDB example
  134. Database Testing <?php require_once 'PHPUnit/Extensions/Database/TestCase.php' ; class BankAccountDBTest extends PHPUnit_Extensions_Database_TestCase { // ... public function testNewAccount () { $ba = new BankAccount ( '12345678912345678' , $this -> pdo ); $set = $this -> createFlatXMLDataSet ( '/path/to/after-new-account.xml' ); } } The BankAccountDB example
  135. Database Testing <?php require_once 'PHPUnit/Extensions/Database/TestCase.php' ; class BankAccountDBTest extends PHPUnit_Extensions_Database_TestCase { // ... public function testNewAccount () { $ba = new BankAccount ( '12345678912345678' , $this -> pdo ); $set = $this -> createFlatXMLDataSet ( '/path/to/after-new-account.xml' ); $this -> assertTablesEqual ( $set -> getTable ( 'account' ), $this -> getConnection () -> createDataSet () -> getTable ( 'account' ) ); } } The BankAccountDB example
  136. Database Testing <dataset> <account account_number= &quot;15934903649620486&quot; balance= &quot;100.00&quot; /> <account account_number= &quot;15936487230215067&quot; balance= &quot;1216.00&quot; /> <account account_number= &quot;12348612357236185&quot; balance= &quot;89.00&quot; /> <account account_number= &quot;15936487230215067&quot; balance= &quot;1216.00&quot; /> <account account_number= &quot;12345678912345678&quot; balance= &quot;0.00&quot; /> </dataset> The BankAccountDB example

+ Sebastian BergmannSebastian Bergmann, 3 years ago

custom

6558 views, 18 favs, 5 embeds more stats

More info about this presentation

CC Attribution-NonCommercial-NoDerivs LicenseCC Attribution-NonCommercial-NoDerivs LicenseCC Attribution-NonCommercial-NoDerivs License

  • Total Views 6558
    • 6261 on SlideShare
    • 297 from embeds
  • Comments 3
  • Favorites 18
  • Downloads 0
Most viewed embeds
  • 288 views on http://sebastian-bergmann.de
  • 6 views on http://www.planet-php.net
  • 1 views on http://www.xianguo.com
  • 1 views on http://www.planet-php.org
  • 1 views on http://www.netvibes.com

more

All embeds
  • 288 views on http://sebastian-bergmann.de
  • 6 views on http://www.planet-php.net
  • 1 views on http://www.xianguo.com
  • 1 views on http://www.planet-php.org
  • 1 views on http://www.netvibes.com

less

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

Cancel
File a copyright complaint
Having problems? Go to our helpdesk?

Categories