Upgrade to Pro — share decks privately, control downloads, hide ads and more …

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. units The smallest testable part of an application. Functions Classes Methods
  2. Whenever you are tempted to type something into a print

    statement or a debugger expression, write it as a test instead. – Martin Fowler “ ”
  3. Benefits of Unit Testing •  Find problems early •  Facilitate

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

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

    refactoring •  Simplify integration •  Document usage •  Design better interfaces confidence automation
  6. 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
  7. PHP Testing Tools Test Frameworks •  PHPUnit * •  SimpleTest

    •  phpt Mocking •  PHPUnit •  Mockery •  Phake Code Coverage •  PHP_CodeCoverage •  Xdebug Other Testing •  Behat •  phpspec •  Selenium
  8. Other Types of Testing White-box Testing •  Unit •  Regression

    •  Integration •  Mutation Black-box Testing •  Acceptance •  Fuzz •  Smoke •  Performance – Load – Stress
  9. 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.
  10. 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
  11. 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
  12. Best Practices •  Test one unit at a time • 

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

    Make each test independent •  Mock external state and services •  Name your tests after their intention
  14. 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
  15. 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
  16. 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;          }   }   “ ”
  17. 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)
  18. 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
  19. People to Follow •  Sebastian Bergmann – @s_bergmann •  Martin

    Fowler – @martinfowler •  Miško Hevery – @mhevery •  Chris Hartjes – @grmpyprogrammer •  Konstantin Kudryashov – @everzet •  Jeff Carouth – @jcarouth