Unit Testing

Unit Testing

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

Ca57a7cfac69ba3abf517470f3770aae?s=128

Jeremy Lindblom

April 12, 2013
Tweet

Transcript

  1. 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
  2. 3.

    Whenever you are tempted to type something into a print

    statement or a debugger expression, write it as a test instead. – Martin Fowler “ ”
  3. 7.

    Benefits of Unit Testing •  Find problems early •  Facilitate

    refactoring •  Simplify integration •  Document usage
  4. 8.

    Benefits of Unit Testing •  Find problems early •  Facilitate

    refactoring •  Simplify integration •  Document usage •  Design better interfaces
  5. 9.

    Benefits of Unit Testing •  Find problems early •  Facilitate

    refactoring •  Simplify integration •  Document usage •  Design better interfaces confidence automation
  6. 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
  7. 11.

    PHP Testing Tools Test Frameworks •  PHPUnit * •  SimpleTest

    •  phpt Mocking •  PHPUnit •  Mockery •  Phake Code Coverage •  PHP_CodeCoverage •  Xdebug Other Testing •  Behat •  phpspec •  Selenium
  8. 12.

    Other Types of Testing White-box Testing •  Unit •  Regression

    •  Integration •  Mutation Black-box Testing •  Acceptance •  Fuzz •  Smoke •  Performance – Load – Stress
  9. 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.
  10. 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
  11. 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
  12. 18.

    Best Practices •  Test one unit at a time • 

    Make each test independent •  Mock external state and services
  13. 19.

    Best Practices •  Test one unit at a time • 

    Make each test independent •  Mock external state and services •  Name your tests after their intention
  14. 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
  15. 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
  16. 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;          }   }   “ ”
  17. 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)
  18. 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
  19. 26.

    People to Follow •  Sebastian Bergmann – @s_bergmann •  Martin

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