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

Test Driven Development and Automation (PHP)

Test Driven Development and Automation (PHP)

Mahesh Salaria

February 25, 2013
Tweet

More Decks by Mahesh Salaria

Other Decks in Programming

Transcript

  1. Test  Driven  Development     &     Automa3on  (PHP)

      “All  code  is  guilty  un3l  proven  innocence”  
  2. Things  to  cover   •  What  is  TDD?   • 

    Why  should  we  do  TDD?   •  Where  should  we  run  Tests  to  make  sure  everything  is   working  fine?   •  Who  should  adopt  TDD?   •  Why  Unit  Tes3ng,  Code  coverage,  code  sniffer  and  Selenium   are  important?   •  Tools  for  PHP  Test  Driven  Development.   •  How  we  automated  tests  with  every  build  genera3on?   •  Quick  Demo  and  QA.  
  3. What  is  TDD?   •  TDD    is  a  programming

     technique  that   requires  you  to  write  actual  code  and   automated  test  code  simultaneously.  This   ensures  that  you  test  your  code—and  enables   you  to  retest  your  code  quickly  and  easily,   since  it’s  automated.   •  All  code  is  guilty  un3l  proven  innocence    
  4. TDD   •  TDD  is  an  agile  development  technique  

    prac3ce  which  combines  Refactoring  and   Test-­‐First  Development.   •  Beck's  concept  of  test-­‐driven   development  centers  on  two  basic  rules:    -­‐  Never  write  a  single  line  of  code  unless  you  have  a   failing  automated  test.    -­‐  Eliminate  duplicaBon.    
  5. Why  we  should  do  TDD?     “I  don’t  want

     to  test  because:”   •  As  a  Developer  I  feel  lazy  to  test  everything   again  and  again.  Oops!   •  I  wanted  to  release  my  product  as  soon  as   possible  to  make  it  available  to  stake  holders.   •  I  feel  it’s  a  waste  of  3me  for  me.   •  I  prefer  manual  tes3ng  (Black  Box  Tes3ng),   just  to  save  myself.  
  6. Why  we  should  do  TDD…   •  TDD  allows  us

     to  make  changes  and  test  quickly   and  efficiently.   •  To  release  the  product  as  soon  as  possible  to   make  it  available  to  stake  holders  without  bugs.   •  To  Reduce  Product  release  life  cycle.   •  To  make  sure  product  is  working  as  expected   without  any  bugs  that  we  are  aware  of.   •  To  make  sure  that  end  customers  are  geYng   good  quality  and  a  mature  product.   •  To  avoid  silly  mistakes.  
  7. Where  should  we  run  Tests  to  make   sure  everything

     is  working  fine?   •  Each  developer  should  setup  an  environment   to  make  sure  that  his  code  gets  tested  with   every  compila3on  or  he  can  test  cases   any3me.   •  Integrate  Unit  Tes3ng,  Code  Coverage  and   Code  Sniffer  with  IDE  to  make  sure  your  code   is  innocent  and  well  wri[en.  
  8. Make  it  automated  on  server  side.   •  Setup  test

     server  to  do  all  the  demo  installa3on  and   tes3ng  stuff.   •  Package  your  code.   •  Create  Self  installa3on  script.   •  Run  installa3on  with  Unit  Test  Cases.   •  Check  Code  coverage  and  Sniff  your  code.   •  Run  PHP  Selenium  test  cases  to  test  your  UI.   •  Generate  Public  Build  if  your  code  passes  all  the  tests.   •  If  test  cases  fail  then  do  not  generate  build  and  raise   RED  Signal  to  developers.  
  9. Who  should  adopt  to  TDD?   •  Every  one,  

     Either  it’s  a  product  development   company  or  Services  based  company.   “Management  support  is  essenBal.  Without  the  enBre   organizaBon  believing  that  test-­‐driven  development  is   going  to  improve  the  product,  management  may  feel   that  Bme  spent  wriBng  tests  is  wasted.”     •  Set  Strict  code  and  tes3ng  standards  if  you  really   want  painless  deployment.    
  10. Why  Unit  Tes=ng,  Code  coverage,  code  sniffer  and   Selenium

     are  important?   •  Unit  Tests  check  Code  level  test  cases  for  Classes   and  func3ons  with  defined  asser3ons.   •  Code  Coverage  ensures  that  you  covered  whole   code  in  test  cases.   •  Code  Sniffer  makes  sure  that  you  follow  strict   coding  standards.   •  Selenium  Tests  make  sure  that  your  UI  is  working   as  expected.   Above  things  ensures  that  you  are  going  to  deliver   100%  working  and  a  Quality  Product.  
  11. Tools  for  PHP  Test  Driven  Development.   •  Xdebug  (Code

     Coverage)   •  PHPUnit   •  Selenium  (PHPUnit_Selenium)   •  PHP_CodeSniffer   •  PHPUnderControl   •  IDEs  with  integrated  TDD:   –  NetBeans   –  ZendStudio   –  PHPStorm  
  12. How  to  write  test  cases?   •  Fail  To  WIN

       “First  fail  the  test  cases.  The  idea  is  to  ensure  that  the  test   really  works  and  can  catch  an  error.  Once  this  is  shown,  the   underlying  func3onality  can  be  implemented.  This  has  been   coined  the  "test-­‐driven  development  mantra",  known  as  red/ green/refactor  where  red  means  fail  and  green  is  pass.”   •  Answer  your  test  by  wri3ng  code.   •  Refactor  and  Refine  your  code.   •  Rinse  and  Repeat.   Source:  h[p://net.tutsplus.com/  
  13. Why  we  needed  TDD  for  our  Organiza3on   •  Big

     Ques3on  on  Big  Code  Base.   –  More  than  1500  PHP  Files.   –  More  than  5  Lac  lines  of  code.   –  More  than  15  Developers  working  on  same  codebase  with  their   different  coding  styles.   –  More  than  300  integrated  features.   –  Mul3ple  channels  of  data  communica3on  (XML  based  Chat,  VoIP,   Emails,  Database  queries,  REST  APIs,  Session  Based  APIs)   “Everything  is  Hard  to  test  on  every  release  cycle.  We  always  miss  and   our  customers  come  back  to  us  which  makes  us  feel  bad.”     •  We  are  s3ll  struggling  to  convince  everyone  to  write  test  cases  and   make  our  builds  based  on  100%  TDD.  
  14. How  we  automated  tests  with  every   build  genera3on?  

      •  Build  System  Integra3on  with  GitHub.   •  Unit  Test  Server  with:   –  Auto  Product  Installa3on.   –  PHPUnit  for  all  test  cases.   –  Code  Coverage  check  in  IDE  only  (Not  Strict  on  this  yet)   –  Code  Quality  Check  using  Code  Sniffer  (Less  errors  due  to  good  IDE  and  same  coding   templates  and  standards)   –   Next  Step  we  are  planning  to  add  Selenium  (Already  done  with  test  deployments)   –  Then  we  deploy  on  our  own  live  installa3on  and  tell  our  team  to  keep  eyes  on  any  error  or   excep3ons.     If  everything  goes  perfectly  fine  then  release  builds.  Else  send  error  messages  in  Internal  Jabber   bases  IM  clients  and  via  email.     It’s  a  complete  test  eco  system  to  make  sure  we  provide  a  quality  product.       This  is  how  we  deploy  our  product  to  more  than  30k  customers  including  automated  SaaS   upgrade  system.