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

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

PHP Community

May 01, 2009
Tweet

More Decks by PHP Community

Other Decks in Programming

Transcript

  1. PHP Testfest 2009
    How to write PHP Tests

    View Slide

  2. Content
    • facts about phptestfest & phpt
    • Build Process & Setup
    • Executing & Coverage
    • Sections of a .phpt file

    View Slide

  3. Testfest!
    and some facts about .phpt

    View Slide

  4. GOALS

    View Slide

  5. GOALS
    RAISE COVERAGE

    View Slide

  6. GOALS
    RAISE COVERAGE ON MULTIPLE OS

    View Slide

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

    View Slide

  8. raise coverage

    View Slide

  9. SPREAD!

    View Slide

  10. SPREAD!
    KNOWHOW OF

    View Slide

  11. SPREAD!
    KNOWHOW OF
    UNIT-TESTING

    View Slide

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

    View Slide

  13. What is phpt ?
    • a way to check PHP‘s functions
    • PHP Code
    • Self contained
    • input - output check

    View Slide

  14. Required

    View Slide

  15. Required
    basic understanding of the PHP
    language

    View Slide

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

    View Slide

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

    View Slide

  18. Build PHP
    build process & test setup

    View Slide

  19. View Slide

  20. MAKE

    View Slide

  21. MAKE
    GCC

    View Slide

  22. MAKE
    AUTOCONF
    GCC

    View Slide

  23. MAKE
    AUTOCONF
    GCC
    BISON || FLEX

    View Slide

  24. MAKE
    AUTOCONF
    GCC
    BISON || FLEX
    RE2C

    View Slide

  25. MAKE
    AUTOCONF
    GCC
    BISON || FLEX
    RE2C
    LCONV

    View Slide

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

    View Slide

  27. Checkout

    View Slide

  28. • cvs -d
    :pserver:[email protected]:/repository
    login
    • password: phpfi
    • cvs -d
    :pserver:[email protected]:/repository
    checkout -r PHP_5_3 php5

    View Slide

  29. Build

    View Slide

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

    View Slide

  31. make clean

    View Slide

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

    View Slide

  33. Coverage
    • executed loc by the testcode
    • html format
    • 100% is the goal

    View Slide

  34. make lcov
    TESTS=tests/*.phpt

    View Slide

  35. make TESTS=tests/*.phpt

    View Slide

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

    View Slide

  37. Test Sections
    Parts of a phpt

    View Slide

  38. • basically php script + output to evaluate
    • .ini, GET, POST etc. can be set
    • different ways to evaluate output
    Sections of a .phpt file

    View Slide

  39. Basics
    The minimum required

    View Slide

  40. --TEST--
    • Documentation section
    • name the function you are testing
    • describe the test case
    • if testing bugs, add the bugid

    View Slide

  41. --CREDITS--
    • for people without CVS accounts
    • name and email
    • (optional) testfest event and date

    View Slide

  42. --FILE--
    • the code goes here
    • open with
    • make it output something

    View Slide

  43. --EXPECT--
    • compares ouput of script to given string
    • take care: paths will differ go for
    --EXPECTF--

    View Slide

  44. Advanced

    View Slide

  45. --EXPECTF--
    • uses palceholders like scanf
    • %s for strings (e.g. path)
    • %r...%r - regex
    • !! Multinline

    View Slide

  46. --EXPECTREGEX--
    • Use a regular Expression to check on
    output
    • A simple way for certain things
    • Check EXPECTF for a simpler version

    View Slide

  47. --GET--
    --POST--
    • sets get and pots values
    • uses the CGI binary instead of the CLI

    View Slide

  48. --SKIPIF--
    • do NOT execute the test under certian
    conditions
    • OS based Test
    • 32 bit vs. 64 bit

    View Slide

  49. Naming Conventions

    View Slide

  50. Tests for bugs
    bug.phpt
    bug17123.phpt

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  54. General tests for
    extensions
    _.phpt

    View Slide

  55. Write better Tests

    View Slide

  56. • Test only one function at a time
    • Change only one parameter
    • Check only one result
    ONE!

    View Slide

  57. selfcontained
    • keep testdata in the test
    • avoid external files if possible
    • document what you try to achieve

    View Slide

  58. external files
    • keep them small
    • one file per per test
    • avoid if possible

    View Slide

  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/

    View Slide

  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.

    View Slide

  61. CREDITS
    • slides by sebs sschuermann.at.chip.de
    • slides created for PHP Testfest 2009

    View Slide