Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

Benefits of Unit Testing •  Find problems early

Slide 5

Slide 5 text

Benefits of Unit Testing •  Find problems early •  Facilitate refactoring

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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.

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

Best Practices •  Test one unit at a time

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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;          }   }   “ ”

Slide 23

Slide 23 text

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)

Slide 24

Slide 24 text

TravisCI + GitHub PR = <3

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

Questions? Presentation by Jeremy Lindblom @jeremeamia