PHP Testfest 2009: How to write PHP tests

PHP Testfest 2009: How to write PHP tests

Slides created by Sebastian Schürmann for PHP TestFest 2009.

More details: https://wiki.php.net/qa/testfesttalk

C343c439f0c144f0d63a0de256c82f79?s=128

PHP Community

May 01, 2009
Tweet

Transcript

  1. PHP Testfest 2009 How to write PHP Tests

  2. Content • facts about phptestfest & phpt • Build Process

    & Setup • Executing & Coverage • Sections of a .phpt file
  3. Testfest! and some facts about .phpt

  4. GOALS

  5. GOALS RAISE COVERAGE

  6. GOALS RAISE COVERAGE ON MULTIPLE OS

  7. GOALS RAISE COVERAGE ON MULTIPLE OS FOCUS ON PHP 5.3

  8. raise coverage

  9. SPREAD!

  10. SPREAD! KNOWHOW OF

  11. SPREAD! KNOWHOW OF UNIT-TESTING

  12. SPREAD! KNOWHOW OF UNIT-TESTING PHP-INTERNALS

  13. What is phpt ? • a way to check PHP‘s

    functions • PHP Code • Self contained • input - output check
  14. Required

  15. Required basic understanding of the PHP language

  16. Required basic understanding of the PHP language a way to

    execute the code
  17. Required basic understanding of the PHP language text editor a

    way to execute the code
  18. Build PHP build process & test setup

  19. None
  20. MAKE

  21. MAKE GCC

  22. MAKE AUTOCONF GCC

  23. MAKE AUTOCONF GCC BISON || FLEX

  24. MAKE AUTOCONF GCC BISON || FLEX RE2C

  25. MAKE AUTOCONF GCC BISON || FLEX RE2C LCONV

  26. MAKE AUTOCONF GCC BISON || FLEX LIBXML-DEV RE2C LCONV

  27. Checkout

  28. • cvs -d :pserver:cvsread@cvs.php.net:/repository login • password: phpfi • cvs

    -d :pserver:cvsread@cvs.php.net:/repository checkout -r PHP_5_3 php5
  29. Build

  30. build and run all tests ./configure && make && make

    test
  31. make clean

  32. ./configure --enable-gcov && make && make lcov Execute with code

    coverage
  33. Coverage • executed loc by the testcode • html format

    • 100% is the goal
  34. make lcov TESTS=tests/*.phpt

  35. make TESTS=tests/*.phpt

  36. • export the TEST_PHP_EXECUTABLE • use run-tests.php

  37. Test Sections Parts of a phpt

  38. • basically php script + output to evaluate • .ini,

    GET, POST etc. can be set • different ways to evaluate output Sections of a .phpt file
  39. Basics The minimum required

  40. --TEST-- • Documentation section • name the function you are

    testing • describe the test case • if testing bugs, add the bugid
  41. --CREDITS-- • for people without CVS accounts • name and

    email • (optional) testfest event and date
  42. --FILE-- • the code goes here • open with <?php

    and close with ?> • make it output something
  43. --EXPECT-- • compares ouput of script to given string •

    take care: paths will differ go for --EXPECTF--
  44. Advanced

  45. --EXPECTF-- • uses palceholders like scanf • %s for strings

    (e.g. path) • %r...%r - regex • !! Multinline
  46. --EXPECTREGEX-- • Use a regular Expression to check on output

    • A simple way for certain things • Check EXPECTF for a simpler version
  47. --GET-- --POST-- • sets get and pots values • uses

    the CGI binary instead of the CLI
  48. --SKIPIF-- • do NOT execute the test under certian conditions

    • OS based Test • 32 bit vs. 64 bit
  49. Naming Conventions

  50. Tests for bugs bug<bugid>.phpt bug17123.phpt

  51. Tests for a function's basic behaviour <functionname>_basic.phpt dba_open_basic.phpt

  52. Tests for a function's error behaviour <functionname>_error.phpt dba_open_error.phpt

  53. Tests for variations in a function's behaviour <functionname>_variation.phpt dba_open_variation.phpt

  54. General tests for extensions <extname>_<no>.phpt

  55. Write better Tests

  56. • Test only one function at a time • Change

    only one parameter • Check only one result ONE!
  57. selfcontained • keep testdata in the test • avoid external

    files if possible • document what you try to achieve
  58. external files • keep them small • one file per

    per test • avoid if possible
  59. CREDITS IMAGES • http://www.flickr.com/photos/zach_manchester/2589742713/ Zach Rathore • http://farm1.static.flickr.com/4/9261879_e5c2340937_o.jpg http://www.duncancumming.co.uk/ •

    http://www.flickr.com/photos/firewarrior/180546141/sizes/l/ http://www.flickr.com/people/firewarrior/ • http://www.flickr.com/photos/fredericksburg/3085671440/sizes/o/ http://www.flickr.com/people/fredericksburg/ • http://www.flickr.com/photos/ajax/164119589/sizes/l/ http://www.flickr.com/people/ajax/ • http://www.flickr.com/photos/thowi/26609866/sizes/l/ http://www.flickr.com/photos/thowi/ • http://www.flickr.com/photos/radiofree/475730956/sizes/l/ http://www.flickr.com/people/timwilson/
  60. CREDITS IMAGES II • http://www.flickr.com/photos/tashland/259178493/sizes/o/ http://www.flickr.com/people/tashland/ • http://www.flickr.com/photos/tracyleephoto/2925320390/sizes/o/ http://www.flickr.com/photos/tracyleephoto/ •

    http://www.flickr.com/photos/danielle_scott/2410922192/sizes/l/ http://www.flickr.com/people/danielle_scott/ • http://www.flickr.com/photos/beckzaidan/1218151329/sizes/o/ http://www.flickr.com/photos/beckzaidan/ • http://www.flickr.com/photos/bz3rk/3358291041/ credited to James WIllamor CLTblog.com.
  61. CREDITS • slides by sebs sschuermann.at.chip.de • slides created for

    PHP Testfest 2009