×
Copy
Open
Link
Embed
Share
Beginning
This slide
Copy link URL
Copy link URL
Copy iframe embed code
Copy iframe embed code
Copy javascript embed code
Copy javascript embed code
Share
Tweet
Share
Tweet
Slide 1
Slide 1 text
PHP Testfest 2009 How to write PHP Tests
Slide 2
Slide 2 text
Content • facts about phptestfest & phpt • Build Process & Setup • Executing & Coverage • Sections of a .phpt file
Slide 3
Slide 3 text
Testfest! and some facts about .phpt
Slide 4
Slide 4 text
GOALS
Slide 5
Slide 5 text
GOALS RAISE COVERAGE
Slide 6
Slide 6 text
GOALS RAISE COVERAGE ON MULTIPLE OS
Slide 7
Slide 7 text
GOALS RAISE COVERAGE ON MULTIPLE OS FOCUS ON PHP 5.3
Slide 8
Slide 8 text
raise coverage
Slide 9
Slide 9 text
SPREAD!
Slide 10
Slide 10 text
SPREAD! KNOWHOW OF
Slide 11
Slide 11 text
SPREAD! KNOWHOW OF UNIT-TESTING
Slide 12
Slide 12 text
SPREAD! KNOWHOW OF UNIT-TESTING PHP-INTERNALS
Slide 13
Slide 13 text
What is phpt ? • a way to check PHP‘s functions • PHP Code • Self contained • input - output check
Slide 14
Slide 14 text
Required
Slide 15
Slide 15 text
Required basic understanding of the PHP language
Slide 16
Slide 16 text
Required basic understanding of the PHP language a way to execute the code
Slide 17
Slide 17 text
Required basic understanding of the PHP language text editor a way to execute the code
Slide 18
Slide 18 text
Build PHP build process & test setup
Slide 19
Slide 19 text
No content
Slide 20
Slide 20 text
MAKE
Slide 21
Slide 21 text
MAKE GCC
Slide 22
Slide 22 text
MAKE AUTOCONF GCC
Slide 23
Slide 23 text
MAKE AUTOCONF GCC BISON || FLEX
Slide 24
Slide 24 text
MAKE AUTOCONF GCC BISON || FLEX RE2C
Slide 25
Slide 25 text
MAKE AUTOCONF GCC BISON || FLEX RE2C LCONV
Slide 26
Slide 26 text
MAKE AUTOCONF GCC BISON || FLEX LIBXML-DEV RE2C LCONV
Slide 27
Slide 27 text
Checkout
Slide 28
Slide 28 text
• cvs -d :pserver:
[email protected]
:/repository login • password: phpfi • cvs -d :pserver:
[email protected]
:/repository checkout -r PHP_5_3 php5
Slide 29
Slide 29 text
Build
Slide 30
Slide 30 text
build and run all tests ./configure && make && make test
Slide 31
Slide 31 text
make clean
Slide 32
Slide 32 text
./configure --enable-gcov && make && make lcov Execute with code coverage
Slide 33
Slide 33 text
Coverage • executed loc by the testcode • html format • 100% is the goal
Slide 34
Slide 34 text
make lcov TESTS=tests/*.phpt
Slide 35
Slide 35 text
make TESTS=tests/*.phpt
Slide 36
Slide 36 text
• export the TEST_PHP_EXECUTABLE • use run-tests.php
Slide 37
Slide 37 text
Test Sections Parts of a phpt
Slide 38
Slide 38 text
• basically php script + output to evaluate • .ini, GET, POST etc. can be set • different ways to evaluate output Sections of a .phpt file
Slide 39
Slide 39 text
Basics The minimum required
Slide 40
Slide 40 text
--TEST-- • Documentation section • name the function you are testing • describe the test case • if testing bugs, add the bugid
Slide 41
Slide 41 text
--CREDITS-- • for people without CVS accounts • name and email • (optional) testfest event and date
Slide 42
Slide 42 text
--FILE-- • the code goes here • open with • make it output something
Slide 43
Slide 43 text
--EXPECT-- • compares ouput of script to given string • take care: paths will differ go for --EXPECTF--
Slide 44
Slide 44 text
Advanced
Slide 45
Slide 45 text
--EXPECTF-- • uses palceholders like scanf • %s for strings (e.g. path) • %r...%r - regex • !! Multinline
Slide 46
Slide 46 text
--EXPECTREGEX-- • Use a regular Expression to check on output • A simple way for certain things • Check EXPECTF for a simpler version
Slide 47
Slide 47 text
--GET-- --POST-- • sets get and pots values • uses the CGI binary instead of the CLI
Slide 48
Slide 48 text
--SKIPIF-- • do NOT execute the test under certian conditions • OS based Test • 32 bit vs. 64 bit
Slide 49
Slide 49 text
Naming Conventions
Slide 50
Slide 50 text
Tests for bugs bug.phpt bug17123.phpt
Slide 51
Slide 51 text
Tests for a function's basic behaviour _basic.phpt dba_open_basic.phpt
Slide 52
Slide 52 text
Tests for a function's error behaviour _error.phpt dba_open_error.phpt
Slide 53
Slide 53 text
Tests for variations in a function's behaviour _variation.phpt dba_open_variation.phpt
Slide 54
Slide 54 text
General tests for extensions _.phpt
Slide 55
Slide 55 text
Write better Tests
Slide 56
Slide 56 text
• Test only one function at a time • Change only one parameter • Check only one result ONE!
Slide 57
Slide 57 text
selfcontained • keep testdata in the test • avoid external files if possible • document what you try to achieve
Slide 58
Slide 58 text
external files • keep them small • one file per per test • avoid if possible
Slide 59
Slide 59 text
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/
Slide 60
Slide 60 text
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.
Slide 61
Slide 61 text
CREDITS • slides by sebs sschuermann.at.chip.de • slides created for PHP Testfest 2009