Se ha denunciado esta presentación.
Se está descargando tu SlideShare. ×

PhpUnit Best Practices

Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Cargando en…3
×

Eche un vistazo a continuación

1 de 34 Anuncio

Más Contenido Relacionado

Presentaciones para usted (20)

A los espectadores también les gustó (20)

Anuncio

Similares a PhpUnit Best Practices (20)

Anuncio

Más reciente (20)

PhpUnit Best Practices

  1. PHPUNIT BEST PRACTICES Volker Dusch / @_ _edorian
  2. ABOUT ME Software Engineer PHP since 11 years CI CleanCode DevOps TDD Shipping Bullet points
  3. INSTEAD OF ME
  4. WORKING FOR ResearchGate gives science back to the people who make it happen. We help researchers build reputation and accelerate scientific progress. On their terms.
  5. GET IN TOUCH stackoverflow: Twitter: @__edorian g+: Volker Dusch IRC: edorian Mail: php@wallbash.com
  6. AGENDA Some practices I value Your mileage may vary By no means complete
  7. WRITE TESTS It's sounds obvious but getting started sometimes is the hardest part!
  8. THE FASTEST THING YOU CAN DO ht=cr - saigpoetcm|ge 'oi: |w -` is`ul s tgn.rjc.o rp Lgn' c l; ts $is-q1| eh "rnpg err" et ht e | co Fotae ro! Staging server Testing your builds All without even touching PHPUnit dt=lgnts&asotscr&sf"crTkn aa"oi=etpswr=euecr=$sfoe ht=cr - PS - saigpoetcm|ge 'el,tsue'|w -` is`ul X OT d tgn.rjc.o rp Hlo etsr c l; ts $is-q1| eh "oi err" et ht e | co Lgn ro!
  9. LET'S GO
  10. UPGRADE TO PHPUNIT 3.7 EASE INSTALLTION
  11. PHAR we ht:/erppntd/e/hui.hr gt tp/pa.hui.egtppntpa cmd+ ppntpa ho x hui.hr .ppntpa -vrin /hui.hr -eso or we ht:/erppntd/e/hui.hr gt tp/pa.hui.egtppntpa cmd+ ppntpa ho x hui.hr m ppntpa /s/oa/i/hui v hui.hr urlclbnppnt ppnt-vrin hui -eso
  12. COMPOSER The Dependency Manager for PHP With the best from zypper, bundler, pip, gem and npm
  13. PHPUNIT PER PROJECT cmoe.sn opsrjo { "eur-e" { rqiedv: "hui/hui" ".." ppntppnt: 37* } } cmoe isal opsr ntl .vno/i/hui -vrin /edrbnppnt -eso
  14. PHPUNIT GLOBAL INSTALL { "eur" { rqie: "hui/hui" ".." ppntppnt: 37* }, "ofg:{ cni" "i-i" "urlclbn" bndr: /s/oa/i/ } } sd ppcmoe isal uo h opsr ntll ppni -vrin huut -eso
  15. PEAR pear config-set auto_discover 1 pear install pear.phpunit.de/PHPUnit phpunit --version
  16. USE SPECIFIC ASSERTIONS PHPUnit ships with over 90 assertions. http://www.phpunit.de/manual/current/en/appendixes.assertions.html Use them to get pretty and helpful error messages.
  17. assertTrue vs assertInstanceOf $o =nwSdls(; fo e tCas) $hs>setre$o isacO Cutbe; ti-asrTu(fo ntnef onal) “Failed asserting that false is true.” $o =nwSdls(; fo e tCas) $hs>setntnef'onal' $o) ti-asrIsacO(Cutbe, fo; “Failed asserting that stdClass() is an instance of interface 'Countable'.”
  18. assertEquals vs assertJsonStringEqualsJsonFile assertEquals Fie asrigta tosrnsaeeul ald setn ht w tig r qa. --Epce - xetd ++Ata + cul @ @ @ @ -{"ofrne:"ODM,"ak:"HUi" "SN:"paety,"huot ' Cneec" FSE" Tl" PPnt, JO" Aprnl" Sotu" :"ekn"} Jnis ' +{"ofrne:"ODM,"ak:"HUi" "SN:"paety,"huot ' Cneec" FSE" Tl" PPnt, JO" Aprnl" Sotu" :"usn } Hdo" '
  19. assertEquals vs assertJsonStringEqualsJsonFile assertJsonStringEqualsJsonFile Fie asrigta toojcsaeeul ald setn ht w bet r qa. --Epce - xetd ++Ata + cul @ @ @ @ sdls Ojc ( tCas bet 'ofrne = 'ODM Cneec' > FSE' 'ak = 'HUi' Tl' > PPnt 'SN = 'paety JO' > Aprnl' - 'huot = 'ekn' Sotu' > Jnis + 'huot = 'usn Sotu' > Hdo' )
  20. HAVE A FAST TEST SUITE If it takes to long to run your tests you won't do it
  21. SEPERATE YOUR TESTS http://elblinkin.info/2012/03/goldilocks-on-test-sizes/
  22. BY FOLDER STRUCTURE . |-sc - r | `-fo - o | `-br - a | `-Bzpp - a.h `-tss - et |-fntoa - ucinl |-itgain - nerto |-ui - nt | `-fo - o | `-br - a | `-Bzetpp - aTs.h `-wb - e ppnttssui hui et/nt
  23. BY CONFIG FILE <etuts tssie> <etut nm=Ui"sfi=Ts.h" tssie ae"nt ufx"etpp> <ietr>et<drcoy drcoytss/ietr> <tssie /etut> <etut nm=Itgain sfi=Ts.nerto.h" tssie ae"nerto" ufx"etItgainpp> <ietr>et<drcoy drcoytss/ietr> <tssie /etut> <tssie> /etuts ppnt-tssieUi hui -etut nt
  24. OR HOWEVER YOU SEE FIT Use @group Use @filter and naming conventions
  25. BOOTSTRAP ONLY WHAT YOU NEED You can use a test listener: http://www.phpunit.de/manual/current/en/extending- phpunit.html#extending-phpunit.PHPUnitFrameworkTestListener pbi fnto satetut(HUi_rmwr_etut $ut) ulc ucin trTsSiePPntFaeokTsSie sie { / Js a eapeo wa i psil / ut n xml f ht s osbe rqie_DR_.$ut-gtae).'otta.h' eur _I_ sie>eNm( Bosrppp; }
  26. HOW MANY TESTS? Web: 7 Funtional: One per feature Integration: One per 3 classes Unit: Find a balance *totally made up numbers to drive home the point I'm trying to make
  27. WEB TESTS? behat (mink) for js-through-the-server testing - Great for testing your whole stack Really hard to maintain Mink relives some of the pain Test through your front controller instead of the webserver with behat or phpunit Faster, easier once set up
  28. TEST CLASSES, NOT METHODS > Unit testing, in PHP, is about testing the observable behaviors of a class! Observable from the outside! Nobody cares about the internal state of a class if it never changes the outcome of a method call.
  29. SAMPLE What should we test there? pbi fnto stau(vle { ulc ucin eVle$au) $hs>au =$au; ti-vle vle } pbi fnto eeue){ ulc ucin xct( i ($hs>au){ f !ti-vle trwnwEcpin"oVle n go"; ho e xeto(N au, o od) } rtr $au *1;/ bsns lgc eun vle 0 / uies oi } If we don't call setValue calling execute will throw an exception If we do call setValue calling execute will return the computed result. So we are testing two behaviors of your class and not the methods in isolation!
  30. RELEVANT BEHAVIORS What to test then? return values pbi fcinclisoaehi(dgesaehi){ ulc uto ecuTFrnet$ereFrnet rtr (dgesaehi -3)*5⁄9 eun $ereFrnet 2 ; } method calls to other objects pbi fcinsoCr){ ulc uto tpa( $hs>adra-egg(; ti-hnbek>nae) $hs>nie>hton) ti-egn-sudw(; } Global state pbi fcinaodhshrPsil(lgesg){ ulc uto viTiWeeosbe$oMsae fl_u_otnssai:$OFL,$oMsae FL_PED; ieptcnet(ttc:LGIE lgesg, IEAPN) $SSIN'ocls]+ _ESO[lgal'+; }
  31. DON'T TEST GETTERS AND SETTERS One test case per behavior You waste time Your code coverage reports won't tell you about dead code If they don't impact the outcome delete them
  32. QUESTIONS? “The secret in testing is in writing testable code” - Miško Hevery Additional resources "The Clean Code Talks -- Unit Testing How to Write Clean, Testable Code The Clean Code Talks - Don't Look For Things! Flaw: Brittle Global State & Singletons static considered harmful The UNIT in unit testing An introduction to PHPUnits @covers annotation
  33. THANK YOU

×