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

PHPUnit Introduction

Robert Allen
September 27, 2011

PHPUnit Introduction

A Brief overview of PHPUnit features and use cases.

Robert Allen

September 27, 2011
Tweet

More Decks by Robert Allen

Other Decks in Technology

Transcript

  1. Starting a Suite •  phpunit.xml   •  Logging   • 

    Directory  Structure   •  Package  Groups   •  Providers   – Comment  based  providers   – Protected  method  providers  
  2. Types of Tests •  ExcepAons   –  Monitors  tests  for

     expected  thrown  excepAons   •  Mocks   –  Observable  expectaAons  through  the  use  of  mocks   •  Errors   –  Watch  for  a  specific  error     •  AsserAons   –  Comparison  of  actual  and  expected  condiAons   –  Most  common  used  
  3. During Development •  Test  Driven   – Define  what  this  package

     does.   – Use  doc  tags  to  define  input  and  expected  output   – Generate  Test  Skeletons  once  completed   •  Behavior  Driven   – Create  the  tests  and  define  the  Class  and  methods   – Generate  your  classes  from  the  tests  once   complete  
  4. During Debugging •  Duplicate  the  complaint   •  Write  a

     test-­‐case  that  duplicates  the   complaint   •  Write  a  test-­‐case  that  should  break  but   doesn’t   •  Write  a  test-­‐case  that  should  not  break   •  When  all  test-­‐case  pass  the  bug  is  fixed   •  All  bug  related  tests  should  idenAfy  the  Acket  
  5. Mocks and Stubs •  Why  have  mocks,  what  do  I

     get  from  them?   – Ability  to  control  the  returns   – Test  the  number  of  Ames  a  method  is  used   – Test  how  a  mock  is  used.   – Eliminate  dependency  on  outside  methods  and   data  sources.   –  While  tesAng  the  return  of  an  outside  source  is  necessary  at   Ames  by  definiAon  a  ‘unit  test’  is  a  test  on  a  ‘Unit  of  Work’   and  fall  outside  the  scope  of  this  discussion.  
  6. Testing, where to begin •  Your  next  customer  complaint.  

    •  New  development.   •  Troublesome  pieces  of  code.   •  Code  you  make  changes  to:   •  Make  a  test  to  validate  its  current  operaAon   •  Make  a  test  for  expected  change  performance   •  Make  your  changes,  test  and  repeat  All  it  works.  
  7. Continuous Integration •  A  verificaAon  that  each  commit  (integraAon)  

    works   •  Validate  rules  adherence   •  It  is  only  as  good  as  the  tests!  
  8. Resources •  h"p://www.phpunit.de/   •  Test  Driven  Development.  David  Astels.

      Copyright  ©  23.  PrenAce  Hall.  ISBN  0131016490.   •  xUnit  Test  Pa"erns:  Refactoring  Test  Code.   Gerard  Meszaros.  Copyright  ©  2007.  Addison-­‐ Wesley.  ISBN  978-­‐0131495050.   •  Test  Driven  Development  by  Example.  Kent   Beck.  Copyright  ©  2002.  Addison-­‐Wesley.  ISBN   0-­‐321-­‐14653-­‐0.