Test reporter documentation

The different sections of the documentation are...
  1. Quickstart guide
  2. Project overview
  3. About test cases
  4. About group tests
  5. Using server stubs to simlulate objects
  6. Using mock objects to test interactions
  7. Partial mocks for superclass testing
  8. Expectation classes
  9. Displaying results
  10. Reading web page content
  11. Testing of web forms
This page...
This documentation is shipped with the SimpleTest package.

Reporting results in HTML

The default test display is minimal in the extreme. It reports success and failure with the conventional red and green bars and shows a breadcrumb trail of test groups for every failed assertion. Here's a fail...

File test

Fail: createnewfile->True assertion failed.
1/1 test cases complete. 0 passes, 1 fails and 0 exceptions.
And here all tests passed...

File test

1/1 test cases complete. 1 passes, 0 fails and 0 exceptions.
The good news is that there are several points in the display hiearchy for subclassing.

For web page based displays there is the HtmlReporter class with the following signature...

class HtmlReporter extends TestDisplay {
    public HtmlReporter() { ... }
    public void paintHeader(string $test_name) { ... }
    public void paintFooter(string $test_name) { ... }
    public void paintStart(string $test_name, integer $size) { ... }
    public void paintEnd(string $test_name, integer $size) { ... }
    public void paintFail(string $message) { ... }
    public void paintPass(string $message) { ... }
    protected string _getCss() { ... }
    public array getTestList() { ... }
    public integer getPassCount() { ... }
    public integer getFailCount() { ... }
    public integer getTestCaseCount() { ... }
    public integer getTestCaseProgress() { ... }
}
Here is what all of these methods mean. First the display methods that you will probably want to override... There are also some accessors to get information on the current state of the test suite. Use these to enrich the display...

Extending the reporter

Rather than simply modifying the existing display, you might want to produce a whole new HTML look, or even generate text or XML. Rather than override every method in TestHtmlDisplay we can take one step up the class hiearchy to TestDisplay in the simple_test.php source file. The public signature is almost the same, but the display methods start empty...

class TestDisplay extends TestReporter {
    public TestDisplay() { ... }
    public void paintHeader(string $test_name) { ... }
    public void paintFooter(string $test_name) { ... }
    public void paintStart(string $test_name, integer $size) { ... }
    public void paintEnd(string $test_name, integer $size) { ... }
    public void paintFail(string $message) { ... }
    public void paintPass(string $message) { ... }
    public array getTestList() { ... }
    public integer getPassCount() { ... }
    public integer getFailCount() { ... }
    public integer getTestCaseCount() { ... }
    public integer getTestCaseProgress() { ... }
}
A do nothing display, a blank canvas for your own creation, would be...

require_once(SIMPLE_TEST . 'simple_test.php');

class MyDisplay extends TestDisplay {
    function MyDisplay() {
        $this->TestDisplay();
    }
    function paintHeader($test_name) {
    }
    function paintFooter($test_name) {
    }
    function paintStart($test_name, $size) {
        parent::paintStart($test_name, $size);
    }
    function paintEnd($test_name, $size) {
        parent::paintEnd($test_name, $size);
    }
    function paintPass($message) {
        parent::paintPass($message);
    }
    function paintFail($message) {
        parent::paintFail($message);
    }
}
No output would come from this class until you add it.

The command line reporter

SimpleTest also ships with a minimal command line reporter. The interface mimics JUnit to some extent, but paints the failure messages as they arrive. To use the command line reporter simply substitute it for the HTML version...

<?php
    if (!defined('SIMPLE_TEST')) {
        define('SIMPLE_TEST', 'simpletest/');
    }
    require_once(SIMPLE_TEST . 'unit_tester.php');
    require_once(SIMPLE_TEST . 'reporter.php');

    $test = &new GroupTest('File test');
    $test->addTestFile('tests/file_test.php');
    $test->run(new TextReporter());
?>
Then invoke the test suite from the command line...
php file_test.php
You will need the command line version of PHP installed of course. A passing test suite looks like this...
File test
OK
Test cases run: 1/1, Failures: 0, Exceptions: 0
A failure triggers a display like this...
File test
1) True assertion failed.
	in createnewfile
FAILURES!!!
Test cases run: 1/1, Failures: 1, Exceptions: 0

One of the main reasons for using a command line driven test suite is of using the tester as part of some automated process. To function properly in shell scripts the test script should return a non-zero exit code on failure. If a test suite fails the value false is returned from the SimpleTest::run() method. We can use that result to exit the script with the desired return code...

<?php
    if (!defined('SIMPLE_TEST')) {
        define('SIMPLE_TEST', 'simpletest/');
    }
    require_once(SIMPLE_TEST . 'unit_tester.php');
    require_once(SIMPLE_TEST . 'reporter.php');

    $test = &new GroupTest('File test');
    $test->addTestFile('tests/file_test.php');
    exit ($test->run(new TextReporter()) ? 0 : 1);
?>
Of course we don't really want to create two test scripts, a command line one and a web browser one, for each test suite. The command line reporter includes a method to sniff out the run time environment...
<?php
    if (!defined('SIMPLE_TEST')) {
        define('SIMPLE_TEST', 'simpletest/');
    }
    require_once(SIMPLE_TEST . 'unit_tester.php');
    require_once(SIMPLE_TEST . 'reporter.php');

    $test = &new GroupTest('File test');
    $test->addTestFile('tests/file_test.php');
    if (TextReporter::inCli()) {
        exit ($test->run(new TextReporter()) ? 0 : 1);
    }
    $test->run(new HtmlReporter());
?>
This is the form used within SimpleTest itself.

Related resources...