$30 off During Our Annual Pro Sale. View Details »

Unit Testing

Unit Testing

Brief overview of unit testing and an introduction to PHPUnit and other tools. For the Seattle PHP Meetup Group.

Jeremy Lindblom

April 12, 2013
Tweet

More Decks by Jeremy Lindblom

Other Decks in Technology

Transcript

  1. Unit Testing
    Testing individual units of
    code to determine if they
    are fit for use.

    View Slide

  2. Unit Testing
    Testing individual units of
    code to determine if they
    are fit for use.
    units
    The smallest
    testable part of
    an application.
    Functions
    Classes
    Methods

    View Slide

  3. Whenever you are tempted
    to type something into a
    print statement or a
    debugger expression, write
    it as a test instead.
    – Martin Fowler


    View Slide

  4. Benefits of Unit Testing
    •  Find problems early

    View Slide

  5. Benefits of Unit Testing
    •  Find problems early
    •  Facilitate refactoring

    View Slide

  6. Benefits of Unit Testing
    •  Find problems early
    •  Facilitate refactoring
    •  Simplify integration

    View Slide

  7. Benefits of Unit Testing
    •  Find problems early
    •  Facilitate refactoring
    •  Simplify integration
    •  Document usage

    View Slide

  8. Benefits of Unit Testing
    •  Find problems early
    •  Facilitate refactoring
    •  Simplify integration
    •  Document usage
    •  Design better interfaces

    View Slide

  9. Benefits of Unit Testing
    •  Find problems early
    •  Facilitate refactoring
    •  Simplify integration
    •  Document usage
    •  Design better interfaces
    confidence
    automation

    View Slide

  10. function  add($a,  $b)  {  
           return  $a  +  $b;  
    }  
     
    function  testAdd()  {  
           $cases  =  array(array(0,    0,  0),  
                             array(4,    2,  6),  
                             array(8,  -­‐1,  7));  
     
           foreach  ($cases  as  $case)  {  
                   list($a,  $b,  $c)  =  $case;  
                   assert('add($a,  $b)  ===  $c');  
           }  
    }  
     
    A Simple
    Example

    View Slide

  11. PHP Testing Tools
    Test Frameworks
    •  PHPUnit *
    •  SimpleTest
    •  phpt
    Mocking
    •  PHPUnit
    •  Mockery
    •  Phake
    Code Coverage
    •  PHP_CodeCoverage
    •  Xdebug
    Other Testing
    •  Behat
    •  phpspec
    •  Selenium

    View Slide

  12. Other Types of Testing
    White-box Testing
    •  Unit
    •  Regression
    •  Integration
    •  Mutation
    Black-box Testing
    •  Acceptance
    •  Fuzz
    •  Smoke
    •  Performance
    – Load
    – Stress

    View Slide

  13. PHPUnit
    •  xUnit-style library by Sebastian Bergmann
    •  Well integrated and well documented
    •  Installable via Composer, PEAR, and Phar
    Features:
    •  Mocking
    •  Assertions
    •  Setup/teardown
    •  Annotations
    •  Data providers
    •  Code coverage
    •  Selenium integration
    •  Etc.

    View Slide

  14. Anatomy of a Unit Test
    1.  Setup & Fixtures
    2.  Prepare input/state
    3.  Execute the code being tested
    4.  Assertions about output/state
    5.  Teardown

    View Slide

  15. Demo: AWS SDK for PHP
    1.  Configuring and running PHPUnit
    2.  Interpreting test output
    3.  IDE Conveniences
    4.  Examining code coverage
    5.  Real test examples
    6.  Update some tests
    https://github.com/aws/aws-sdk-php

    View Slide

  16. Best Practices
    •  Test one unit at a time

    View Slide

  17. Best Practices
    •  Test one unit at a time
    •  Make each test independent

    View Slide

  18. Best Practices
    •  Test one unit at a time
    •  Make each test independent
    •  Mock external state and services

    View Slide

  19. Best Practices
    •  Test one unit at a time
    •  Make each test independent
    •  Mock external state and services
    •  Name your tests after their intention

    View Slide

  20. Best Practices
    •  Test one unit at a time
    •  Make each test independent
    •  Mock external state and services
    •  Name your tests after their intention
    •  Use good OOP and DRY principles

    View Slide

  21. Best Practices
    •  Test one unit at a time
    •  Make each test independent
    •  Mock external state and services
    •  Name your tests after their intention
    •  Use good OOP and DRY principles
    •  Write your code to be testable

    View Slide

  22. Dependency Injection
    Dependency Injection is where components
    are given their dependencies through their
    constructors, methods, or fields.
    class  Mailer  {  
           protected  $transport;  
           public  function  __construct(  
                   TransportInterface  $transport  
           )  {  
                   $this-­‐>transport  =  $transport;  
           }  
    }  


    View Slide

  23. Continuous Integration (CI)
    •  Integrate code often and automate
    your build and test processes
    •  Continuous Integration Systems
    Jenkins CI • Travis CI • Xinc
    phpUnderControl • Bamboo
    •  More tools: phpmd, phpcpd, phpcs
    (see http://phpqatools.org)

    View Slide

  24. TravisCI + GitHub PR = <3

    View Slide

  25. TDD & BDD
    TDD – Test-driven development
    •  Tests are written before the implementation
    •  Short-development cycle; Agile methods
    •  Test-writing is part of the design process
    BDD – Behavior-driven development
    •  Extends TDD; focuses on acceptance testing
    •  Business requirements define behaviors
    •  Behat or phpspec are the main PHP tools

    View Slide

  26. People to Follow
    •  Sebastian Bergmann – @s_bergmann
    •  Martin Fowler – @martinfowler
    •  Miško Hevery – @mhevery
    •  Chris Hartjes – @grmpyprogrammer
    •  Konstantin Kudryashov – @everzet
    •  Jeff Carouth – @jcarouth

    View Slide

  27. Questions?
    Presentation by
    Jeremy Lindblom
    @jeremeamia

    View Slide