Testing PHP Applications with PHPUnit 3

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.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    24 Favorites

    Testing PHP Applications with PHPUnit 3 - Presentation Transcript

    1. Testing PHP Applications with PHPUnit 3 Sebastian Bergmann November 2 nd 2006
    2. Who I Am
      • Sebastian Bergmann.
      • Born 1978.
      • System Developer at eZ Systems AS.
      • Committer: PHP, PEAR, Gentoo Linux, ...
      • Creator of Open Source PHP projects such as PHPUnit.
      • Author on PHP and related topics.
    3. Who Are You?
      • What is your PHP background?
        • OOP?
        • PHP 5?
        • PHPUnit?
    4. The Need for Testing
      • PHP has evolved from a niche language for small websites to a powerful tool making strong inroads into large-scale, business-critical Web-based systems.
      • For instance, financial institutions use PHP to develop and maintain solutions for Basel II Credit Rating.
      • Critical business logic like this needs to work correctly.
      • But how do you ensure that it does?
      • You test it, of course.
    5. Gateway Drug to Test Infection <?php class Calculator { public function add ( $a , $b ) { return $a + $b ; } } ?>
    6. Gateway Drug to Test Infection <?php class Calculator { /** * @test (0,0) == 0 * @test (0,1) == 1 * @test (1,0) == 1 * @test (1,1) == 2 * @test (1,2) != 3 */ public function add ( $a , $b ) { return $a + $b ; } } ?>
    7. Gateway Drug to Test Infection
    8. Start from the Beginning
      • We just used the Skeleton Generator of PHPUnit 3 to automatically generate (and run) tests from source code annotations.
      • While this is handy for simple test cases like the ones in the previous example, it does not cater well to more complex test scenarios.
      • So let us take a step back and start from the beginning ...
    9. Testing Software
      • Unit Testing
        • Runnable code-fragments (Unit Tests) test the correctness of parts (units) of the software.
      • System Testing
        • Conducted on a complete, integrated system to evaluate the system's compliance with its specified requirements. (from Wikipedia)
      • Non-Functional Testing
        • Performance.
        • Stability.
        • Usability.
    10. Tool Support for Testing
      • 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.
    11. Testing PHP Applications
      • Unit Testing
        • PHPUnit
        • SimpleTest
      • System Testing
        • Selenium
              • PHPUnit + Selenium
      • Non-Functional Tests
        • Performance, Load, Stress, Reliability, Availability
          • ab, httperf, JMeter, Grinder, OpenSTA, ...
        • Security
          • Chorizo
    12. PHPUnit http://www.phpunit.de/
    13. Installing PHPUnit
    14. PHPUnit TextUI TestRunner
    15. Writing a Test Case <?php require_once 'PHPUnit/Framework.php' ; require_once 'Calculator.php' ; class CalculatorTest extends PHPUnit_Framework_TestCase { public function testAdd () { $calculator = new Calculator ; $this -> assertEquals ( 0 , $calculator -> add ( 0 , 0 )); $this -> assertEquals ( 1 , $calculator -> add ( 0 , 1 )); $this -> assertEquals ( 1 , $calculator -> add ( 1 , 0 )); $this -> assertEquals ( 2 , $calculator -> add ( 1 , 1 )); $this -> assertNotEquals ( 3 , $calculator -> add ( 1 , 2 )); } } ?>
    16. Running a Test Case
    17. Writing a Test Case <?php require_once 'PHPUnit/Framework.php' ; require_once 'Calculator.php' ; class CalculatorTest extends PHPUnit_Framework_TestCase { protected $calculator ; protected function setUp () { $this -> calculator = new Calculator ; } public function testAdd () { $this -> assertEquals ( 0 , $this -> calculator -> add ( 0 , 0 )); } // ... ?>
    18. Running a Test Case
    19. Testing Practices
      • You can always write more tests.
      • However, you will quickly find that only a fraction of the tests you can imagine are actually useful.
      • What you want is to write tests that fail even though you think they should work, or tests that succeed even though you think they should fail.
      • Another way to think of it is in cost / benefit terms.
      • You want to write tests that will pay you back with information. (Erich Gamma)
    20. Testing Practices
      • During Development
        • When you need to make a change to the internal structure of the software you are working on to make it easier to understand and cheaper to modify without changing its observable behavior, a test suite is invaluable in applying these so called refactorings safely.
        • Otherwise, you might not notice the system breaking while you are carrying out the restructuring.
    21. Testing Practices
      • During Debugging
        • Verify that you can reproduce the defect.
        • Find the smallest-scale demonstration of the defect in the code.
        • Write an automated test that fails now but will succeed when the defect is fixed.
        • Fix the defect.
      • Finding the smallest reliable reproduction of the defect gives you the opportunity to really examine the cause of the defect.
        • The new test ensures that you really fix it.
        • The old tests ensure that you do not introduce a new defect.
    22. The PHPUnit API
    23. Organizing Tests into Suites
      • Application/
        • Package/
          • Class (Application/Package/Class.php)
          • ...
        • ...
        • Tests/
          • AllTests.php
          • Package/
            • AllTests.php
            • ClassTest (Application/Tests/Package/ClassTest.php)
    24. Organizing Tests into Suites <?php require_once 'PHPUnit/Framework/TestSuite.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 ; } } ?>
    25. Organizing Tests into Suites <?php require_once 'PHPUnit/Framework/TestSuite.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 ; } } ?>
    26. Who Uses PHPUnit
      • eZ Systems AS
        • eZ Components
          • ~ 2.500 tests
          • Custom test runner based on PHPUnit_TextUI_TestRunner
      • Zend Technolgies Ltd.
        • Zend Framework
          • ~ 400 tests (main library), ~ 1.000 tests (incubator)
      • PHP-GTK
      • IBM SDO
      • Agavi
      • Creole
      • Propel
      • Phing
      • Serendipity
      • ...
    27. PHPUnit at eZ Systems AS
      • New eZ Component
        • Write a design document.
          • Discussion, Review, ...
        • Write the tests.
        • Write the code.
      • Existing eZ Component
        • No code change without a ticket in the issue tracker.
        • No code change without running the existing tests.
          • No bug fix without new test(s) for the bug.
        • No feature addition without tests for the new feature.
    28. You Have Seen Nothing Yet
      • We only scratched the surface.
        • {Writing | Organizing | Running} tests.
      • PHPUnit can do much more:
        • Mock Objects (port of jMock).
        • Code Coverage (needs Xdebug).
        • Incomplete tests (port of junitour) and Skipped Tests.
        • Agile Documentation (port of TestDox).
        • Logging of test execution in XML, TAP, or AT&T GraphViz markup and to PEAR::Log targets.
        • Integrates with CruiseControl, Selenium, PHPEclipse, and Phing.
    29. Code Coverage
    30. Code Coverage
    31. Code Coverage
    32. Code Coverage
    33. Code Coverage
    34. Code Coverage
    35. Code Coverage
    36. Code Coverage
    37. Code Coverage
    38. Code Coverage
    39. Code Coverage
    40. Code Coverage
    41. Code Coverage
    42. Code Coverage
    43. Selenium Integration
    44.  
    45. Selenium Integration
    46. Selenium Integration
    47. Selenium Integration
    48. Selenium Integration
    49. CruiseControl Integration
    50. CruiseControl Integration
    51. CruiseControl Integration
    52. CruiseControl Integration
    53. CruiseControl Integration
    54. CruiseControl Integration
    55. CruiseControl Integration
    56. CruiseControl Integration
    57. CruiseControl Integration
    58. CruiseControl Integration
    59. CruiseControl Integration
    60. PHPEclipse Integration
    61. PHPEclipse Integration
    62. PHPEclipse Integration
    63. Shameless Plug ;-)
    64. License
      • These slides are available under the Creative Commons Attribution-NonCommercial-NoDerivs 2.5 license.
      • You are free to copy, distribute, display, and perform the work under the following conditions:
        • Attribution: You must attribute the work in the manner specified by the author or licensor.
        • Noncommercial: You may not use this work for commercial purposes.
        • No Derivative Works: You may not alter, transform, or build upon this work.
        • For any reuse or distribution, you must make clear to others the license terms of this work.
        • Any of these conditions can be waived if you get permission from the copyright holder.
      • Your fair use and other rights are in no way affected by the above.

    + Sebastian BergmannSebastian Bergmann, 4 years ago

    custom

    8881 views, 24 favs, 5 embeds more stats

    Presentation at the Zend/PHP Conference & Expo 2006 more

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 8881
      • 8682 on SlideShare
      • 199 from embeds
    • Comments 0
    • Favorites 24
    • Downloads 0
    Most viewed embeds
    • 137 views on http://sebastian-bergmann.de
    • 51 views on http://php-applicationdesign.blogspot.com
    • 9 views on http://omaarr.wordpress.com
    • 1 views on https://s3.amazonaws.com
    • 1 views on http://209.85.135.104

    more

    All embeds
    • 137 views on http://sebastian-bergmann.de
    • 51 views on http://php-applicationdesign.blogspot.com
    • 9 views on http://omaarr.wordpress.com
    • 1 views on https://s3.amazonaws.com
    • 1 views on http://209.85.135.104

    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