PHPUnit - Testing Object-Oriented PHP Applications

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

    10 Favorites

    PHPUnit - Testing Object-Oriented PHP Applications - Presentation Transcript

    1. PHPUnit Testing Object-Oriented PHP Applications
    2. Who I Am
      • Computer-Science Student in Bonn, Germany.
      • Member of the PHP Development Team.
      • Author of Open Source projects like PHPUnit or phpOpenTracker.
    3. Who Are You?
      • Who has heard of Unit Tests before?
      • Who has used a Unit Testing framework before?
      • Who tests software at all?
    4. Why Unit Tests?
      • Why Unit Tests?
      • Why test software at all?
        • "I have no time to write tests!"
        • "Testing is boring and stupid!"
        • "My code is flawless, at least it is good enough!"
        • "It is the testing department's job to test, right?"
        • These "excuses" lead to a vicious circle that is hard to break.
    5. The Benefit of Unit Tests
      • "The fewer tests you write, the less productive you are and the less stable your code becomes.
      • The less productive and accurate you are, the more pressure you feel.
      • You would see the value of the immediate feedback you get from writing and saving and rerunning your own unit tests."
      Erich Gamma
    6. The Test-First Approach
      • Write the test before the production code!
        • The test motivates the production code.
        • There can be no production code that is not covered by a test at any time.
        • Improved confidence in the code.
      • Write only as much production code as needed to make the previously written test pass!
        • The production code is ready when the test passes.
    7. Refactoring
      • "Refactoring is a controlled technique for improving the design of an existing code base.
      • Its essence is applying a series of small behavior-preserving transformations, each of which "too small to be worth doing".
      • However the cumulative effect of each of these transformations is quite significant.
      • By doing them in small steps you reduce the risk of introducing errors.
      • You also avoid having the system broken while you are carrying out the restructuring.
      • This allows you to gradually refactor a system over an extended period of time."
      Martin Fowler
    8. Refactoring
      • 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.
    9. Refactoring
      • Only automated tests make the changes needed to achieve the simplest design feasible.
      • Without them you would have to test every method of every class manually.
    10. Unit Tests
      • Unit Tests have their origin in procedural programming where each procedure was considered a unit.
      • In object-oriented programming a unit may be a single method or class as well as the whole system.
      (Found on images.google.com)
    11. PHPUnit
      • PHPUnit is a regression testing framework used by the developer who implements Unit Tests in PHP.
      • It is modelled after the de-facto standard of the Java world, JUnit.
      • Work on PHPUnit for PHP 4 started in 2001. It is now deprecated.
      • PHPUnit2 is a complete rewrite that uses the new object-oriented language features of PHP 5.
      • It aims at "API similarity" with JUnit so that existing documentation can be leveraged.
    12. Getting PHPUnit2
    13. Installing PHPUnit2
    14. The BankAccount Example
      • Contract
        • A bank account's balance
          • ... is initially zero.
          • ... cannot become negative.
      • Interface
        • getBalance()
        • setBalance($balance)
        • depositMoney($amount)
        • withdrawMoney($amount)
    15. The BankAccount Example
    16. The BankAccount Example
    17. The PHPUnit2 Package
      • PHPUnit2_Framework_Assert
      • PHPUnit2_Framework_AssertionFailedError
        • PHPUnit2_Framework_ComparisonFailure
      • PHPUnit2_Framework_Test
        • PHPUnit2_Framework_TestCase
          • PHPUnit2_Extensions_ExceptionTestCase
          • PHPUnit2_Extensions_PerformanceTestCase
        • PHPUnit2_Framework_TestSuite
      • PHPUnit2_Framework_IncompleteTest
      • PHPUnit2_Framework_TestFailure
      • PHPUnit2_Framework_TestListener
      • PHPUnit2_Framework_TestResult
      • PHPUnit2_Extensions_Logger_Log
      • PHPUnit2_Extensions_Logger_XML
      • PHPUnit2_Extensions_TestDecorator
        • PHPUnit2_Extensions_TestSetup
        • PHPUnit2_Extensions_RepeatedTest
      • PHPUnit2_Runner_BaseTestRunner
        • PHPUnit2_TextUI_TestRunner
        • (PHPUnit2_GtkUI_TestRunner)
        • (PHPUnit2_WebUI_TestRunner)
      • PHPUnit2_Runner_TestCollector
        • PHPUnit2_Runner_IncludePathTestCollector
          • PHPUnit2_Runner_SimpleTestCollector
      • PHPUnit2_Runner_TestSuiteLoader
        • PHPUnit2_Runner_StandardTestSuiteLoader
      • PHPUnit2_Runner_TestRunListener
    18. PHPUnit2_Framework_Assert
      • Methods to be used to assert conditions in Unit Tests:
        • assertEquals($expected, $actual, $delta = 0, $message = '')
        • assertTrue($condition, $message = '')
        • assertFalse($condition, $message = '')
        • assertNotNull($object, $message = '')
        • assertNull($object, $message = '')
        • assertSame($expected, $actual, $message = '')
        • assertNotSame($expected, $actual, $message = '')
        • assertType($expected, $actual, $message = '')
        • assertRegExp($expected, $actual, $message = '')
      • If the expected condition cannot be asserted a PHPUnit2_ Framework_AssertionFailedError or PHPUnit2_ Framework_ComparisonFailure exception is thrown and the Unit Test results in a Failure.
      • If another exception is thrown inside the code that is executed by the Unit Test it results in an Error.
        • Failure != Error.
    19. PHPUnit2_Framework_Test
      • Common interface to be implemented by “test classes“.
    20. PHPUnit2_Framework_TestCase
      • Standard base class for Unit Tests in PHPUnit.
      • Collects code coverage information for the Unit Test if the Xdebug extension is loaded.
      • Can be sub-classed to change the test behaviour.
        • PHPUnit2_Extensions_ExceptionTestCase
          • Used to write Unit Tests that test whether or not an exception is thrown in the covered code.
        • PHPUnit2_Extensions_PerformanceTestCase
          • Used to write Unit Tests that track performance regressions.
    21. PHPUnit2_Framework_TestCase
      • Can be decorated (Decorator Pattern) to customize the test behaviour.
        • PHPUnit2_Extensions_TestDecorator
          • PHPUnit2_Extensions_RepeatedTest
            • A Decorator that runs a Unit Test repeatedly.
          • PHPUnit2_Extensions_TestSetup
            • A Decorator to set up and tear down additional fixture state.
    22. PHPUnit2_Framework_TestSuite
      • A Test Suite is a composite of Unit Tests.
        • It is used to run a collection of Unit Tests.
      • Tests can be added dynamically / manually.
        • $suite = new PHPUnit2_Framework_TestSuite; $suite->addTest(new FooTest('testFoo'));
      • Tests can be added automatically.
        • $suite = new PHPUnit2_Framework_TestSuite('FooTest');
        • This creates a Test Suite with all the methods of class FooTest that start with "test".
    23. PHPUnit2_Framework_TestResult
      • A Test Result collects the results of Unit Test execution.
      • The test progress can be observed (Observer Pattern) by attaching an object of a class that implements the PHPUnit2_ Framework_TestListener interface to the Test Result object.
    24. PHPUnit2_Framework_TestListener
    25. Executing Tests / Test Suites
      • A Test or a Test Suite is executed by a Test Runner.
        • Currently only a SAPI/CLI-based Test Runner (TextUI) is available.
          • Invoked either from the command-line (phpunit) or by using the PHPUnit2_TextUI_TestRunner class.
        • Both a PHP-GTK-based (GtkUI) and a Web-based (WebUI) Test Runner are planned.
      • Demonstration!
    26. Last Words
      • „ Understanding is a three-edged sword: my side, your side, and the truth.“
        • Do not overestimate Unit Tests.
        • But do not underestimate them, either.
        • Find out what works for you and helps you in writing better software.
      Kosh (Vorlon Ambassador to Babylon 5)
    27. License
      • These slides are available under the Creative Commons Attribution-NoDerivs-NonCommercial 1.0 license.
      • You are free to copy, distribute, display, and perform the work under the following conditions:
        • Attribution: You must give the original author credit.
        • 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 author.
      • Your fair use and other rights are in no way affected by the above.

    + Sebastian BergmannSebastian Bergmann, 4 years ago

    custom

    2379 views, 10 favs, 1 embeds more stats

    More info about this document

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

    Go to text version

    • Total Views 2379
      • 2377 on SlideShare
      • 2 from embeds
    • Comments 0
    • Favorites 10
    • Downloads 0
    Most viewed embeds
    • 2 views on https://s3.amazonaws.com

    more

    All embeds
    • 2 views on https://s3.amazonaws.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