Tests PHP code by executing usage examples in the documentation.
If you have a file called example.php like this below, and execute it, the DocTest class will really execute add(20, 22) and compare the output to 42. It will output, that this test passes.
<?php // This file is example.php /** * Adds two numbers. * Usage example: * <code> * echo add(20, 22); /// 42 * </code> */ function add($a, $b) { return $a + $b; } if (__FILE__ == realpath($_SERVER['SCRIPT_FILENAME'])) { require_once 'DocTest.php'; DocTest::testfile(); } ?>
You can call the test also from another file like this:
There are some more methods to examine the result of the test. See the list below.
The used syntax is compatible to the documentation syntax of PhpDocumentor.
The praxis of doc tests is very common under python programmers. Python has a build in module for doc tests. Due it's no part of the PHP language (yet) there are several PHP classes in the web following the same principle. For example:
Sometimes you are not able to test your application only with doc tests. In some cases a more complex testing framework is usefull. I recommend PhpUnit, which has a similar functionality to JUnit for Java.This class is part of the uBook Project.
Located in /DocTest.php (line 88)
Creates a test for $testCandidate.
Determines, if the test failed or not.
Returns the last expected output of the tested code.
Returns the last line number before an assertion failed or the test terminated successfully.
Returns the real output of the tested code.
Returns the number of passed assertion before the test failed or terminated successfully.
Runs all code examples until one fails.
Creates a human readable string with the result of the test.
Documentation generated on Tue, 25 Oct 2011 19:51:47 +0200 by phpDocumentor 1.4.3