PHPT is a test format designed for testing internal PHP functions. But you can use it to test your own functions as well.
Essentially, the PHPT format consists of a few blocks of text marked by the following delimeters:
-
--TEST--
-
--FILE--
-
--EXPECT--
Here’s an example:
#IncrementTest.php
--TEST-- increment() function - basic test for increment() --FILE-- <?php $integer = increment(1); $string = increment("one"); var_dump($integer); var_dump($string); ?> --EXPECT-- int(2) string(3) "two"
PHPT scripts are executed by “run-tests.php”
You can execute it with Pear:
pear run-tests IncrementTest.phpt
Running 1 tests FAIL increment() function - a basic test to see if it works[IncrementTest.phpt] wrote log to "C:\dev\eclipse\workspaces\aaron\MyTestClass\t\run-tests.log" TOTAL TIME: 00:03 0 PASSED TESTS 0 SKIPPED TESTS 1 FAILED TESTS: IncrementTest.phpt
You can see why it failed in the .diff file (which is a diff of the .exp and .out files.) All the generated files are:
IncrementTest.diff -- this is a diff of the expected and actual output IncrementTest.exp -- this is the expected output IncrementTest.log -- this is the same test output sent to stdout (see above) IncrementTest.out -- this is the actual output IncrementTest.php -- this is the --FILE-- section of your test IncrementTest.phpt -- this is your original test run-tests.log -- this is the summary of your test results (# of tests passed/failed/skipped)
PHPT test stubs can be autogenerated http://qa.php.net/autogenerate.php
php generate-phpt.php -f increment
Here’s a good introductory tutorial for PHPT: http://qa.php.net/write-test.php
Here’s the detailed PHPT spec http://qa.php.net/phpt_details.php