Slides created by Sebastian Schürmann for PHP TestFest 2009.
More details: https://wiki.php.net/qa/testfesttalk
PHP Testfest 2009How to write PHP Tests
View Slide
Content• facts about phptestfest & phpt• Build Process & Setup• Executing & Coverage• Sections of a .phpt file
Testfest!and some facts about .phpt
GOALS
GOALSRAISE COVERAGE
GOALSRAISE COVERAGE ON MULTIPLE OS
GOALSRAISE COVERAGE ON MULTIPLE OS FOCUS ON PHP 5.3
raise coverage
SPREAD!
SPREAD!KNOWHOW OF
SPREAD!KNOWHOW OFUNIT-TESTING
SPREAD!KNOWHOW OFUNIT-TESTINGPHP-INTERNALS
What is phpt ?• a way to check PHP‘s functions• PHP Code• Self contained• input - output check
Required
Requiredbasic understanding of the PHPlanguage
Requiredbasic understanding of the PHPlanguagea way to execute thecode
Requiredbasic understanding of the PHPlanguagetext editora way to execute thecode
Build PHPbuild process & test setup
MAKE
MAKEGCC
MAKEAUTOCONFGCC
MAKEAUTOCONFGCCBISON || FLEX
MAKEAUTOCONFGCCBISON || FLEXRE2C
MAKEAUTOCONFGCCBISON || FLEXRE2CLCONV
MAKEAUTOCONFGCCBISON || FLEXLIBXML-DEVRE2CLCONV
Checkout
• cvs -d:pserver:[email protected]:/repositorylogin• password: phpfi• cvs -d:pserver:[email protected]:/repositorycheckout -r PHP_5_3 php5
Build
build and run all tests./configure && make && make test
make clean
./configure --enable-gcov && make &&make lcovExecute with codecoverage
Coverage• executed loc by the testcode• html format• 100% is the goal
make lcovTESTS=tests/*.phpt
make TESTS=tests/*.phpt
• export the TEST_PHP_EXECUTABLE• use run-tests.php
Test SectionsParts of a phpt
• basically php script + output to evaluate• .ini, GET, POST etc. can be set• different ways to evaluate outputSections of a .phpt file
BasicsThe minimum required
--TEST--• Documentation section• name the function you are testing• describe the test case• if testing bugs, add the bugid
--CREDITS--• for people without CVS accounts• name and email• (optional) testfest event and date
--FILE--• the code goes here• open with • make it output something
--EXPECT--• compares ouput of script to given string• take care: paths will differ go for--EXPECTF--
Advanced
--EXPECTF--• uses palceholders like scanf• %s for strings (e.g. path)• %r...%r - regex• !! Multinline
--EXPECTREGEX--• Use a regular Expression to check onoutput• A simple way for certain things• Check EXPECTF for a simpler version
--GET----POST--• sets get and pots values• uses the CGI binary instead of the CLI
--SKIPIF--• do NOT execute the test under certianconditions• OS based Test• 32 bit vs. 64 bit
Naming Conventions
Tests for bugsbug.phptbug17123.phpt
Tests for a function's basicbehaviour_basic.phptdba_open_basic.phpt
Tests for a function's errorbehaviour_error.phptdba_open_error.phpt
Tests for variations in afunction's behaviour_variation.phptdba_open_variation.phpt
General tests forextensions_.phpt
Write better Tests
• Test only one function at a time• Change only one parameter• Check only one resultONE!
selfcontained• keep testdata in the test• avoid external files if possible• document what you try to achieve
external files• keep them small• one file per per test• avoid if possible
CREDITS IMAGES• http://www.flickr.com/photos/zach_manchester/2589742713/Zach Rathore• http://farm1.static.flickr.com/4/9261879_e5c2340937_o.jpghttp://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/
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.
CREDITS• slides by sebs sschuermann.at.chip.de• slides created for PHP Testfest 2009