A Simple Test Case
Installing the SimpleTest unit tester is as simple as unpacking the tar file from sourceforge. To build some tests, however, we need to get a little bit organised. I am going to assume that the code is going into a folder called
classes
and that the test cases are going into a folder called test
. Also we’ll use a symlink, or path, to make SimpleTest available as test/simpletest
.
If this is the case, we can write a do nothing test case (
test/config_test.php
) as follows…
<?php
define('SIMPLE_TEST', 'simpletest/');
require_once(SIMPLE_TEST . 'unit_tester.php');
require_once(SIMPLE_TEST . 'reporter.php');
class
ConfigurationTest extends UnitTestCase {
function ConfigurationTest() {
$this->UnitTestCase();
}
}
$test = &new ConfigurationTest();
$test->run(new HtmlReporter());
?>
The first block includes the files needed by the tester with the proviso that the
SIMPLE_TEST
constant must point at the SimpleTest directory. The second block is the actual test case, more on this in a minute. The third block creates the test case and runs it.
To keep things simple I have compressed the entire test suite into a single, rather cluttered, file. In the real world you would only need the actual test case, as the running and grouping of tests would be in a separate runner script.
If you point your web browser at the test script it will actually run successfully…
configurationtest
1/1 test cases complete:
0 passes, 0 fails and 0 exceptions.
0 passes, 0 fails and 0 exceptions.
Having got our environment set up you probably think we are ready to code. If so, give yourself a slap on the wrist! We are actually ready to write our first test.