PHP Test SuiteFiled Under: PHP Scripts
Having used a number of test suites for C++, I know how terrible they can be. Most are overly complex, trying to accomplish far more than you would ever expect or need. So when I found myself needing a test system for PHP 5, I didn’t even look for a free one, I just spent the half hour to roll my own.
Simply enough, I named it TestPHP (original I know). It consists of three public functions. As far as I’m concerned, thats the maximum number of exposed functions for a test system. The “test” function taks a condition (boolean) and a message. If the condition evaluates to false, the test fails, otherwise it passes. When the “end” function is called, it just echoes html with all the messages from the tests. It is really easy to use. Just include “TestPHP.php” then call the three functions like in the following example.
include(’TestPHP.php’);
include(’TestClass.php’);
TestPHP::start(”TestClass”); // Begin the test
$test = new TestClass(); // Instantiate the TestClass
$sum = $test->add(4, 5);
TestPHP::test($sum == 9, “The sum of 4 and 5 should be equal to 9″);
TestPHP::end(); // End the test
It outputs straight HTML. So just pointing your browser to a file with the above content will output the test results. Here is a sample of a PHP class’ test results, its not formatted at all, but that can be easily added. XML output might be a good option, but then you would need an XSLT file.
This system is specifically designed for testing PHP classes. Simply include “TestPHP.php” along with the class you want to test, and start testing all your class’ member functions. Because the “test” function is static, it can even be used from within the class you are testing.
You can download a sample of the PHP test system, upload the three files to your webserver, and point your browser towards test.php. It does require PHP 5.
Resources:
- TestPHP sample test output
- Download TestPHP sample program (requires PHP 5)
Tags: downloads, PHP Scripts, scripts, testing
- Permalink
- admin
- 23 Mar 2009 10:27 PM
- Comments (0)