Previous Up Next
Partial mocks Simple Test PHP Unit Test Framework Expectations

Reporting

SimpleTest pretty much follows the MVC-ish pattern (Model-View-Controller). The reporter classes are the view and the model is your test cases and their hiearchy. The controller is mostly hidden from the user of SimpleTest unless you want to change how the test cases are actually run, in which case it is possible to override the runner objects from within the test case. As usual with MVC, the controller is mostly undefined and there are other places to control the test run.

Table of Contents

Reporting results in HTML

The default HTML 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...

  1. class HtmlReporter extends SimpleReporter {
  2.     public __construct($encoding... }
  3.     public makeDry(boolean $is_dry... }
  4.     public void paintHeader(string $test_name... }
  5.     public void sendNoCacheHeaders(... }
  6.     public void paintFooter(string $test_name... }
  7.     public void paintGroupStart(string $test_nameinteger $size... }
  8.     public void paintGroupEnd(string $test_name... }
  9.     public void paintCaseStart(string $test_name... }
  10.     public void paintCaseEnd(string $test_name... }
  11.     public void paintMethodStart(string $test_name... }
  12.     public void paintMethodEnd(string $test_name... }
  13.     public void paintFail(string $message... }
  14.     public void paintPass(string $message... }
  15.     public void paintError(string $message... }
  16.     public void paintException(string $message... }
  17.     public void paintMessage(string $message... }
  18.     public void paintFormattedMessage(string $message... }
  19.     protected string getCss(... }
  20.     public array getTestList(... }
  21.     public integer getPassCount(... }
  22.     public integer getFailCount(... }
  23.     public integer getExceptionCount(... }
  24.     public integer getTestCaseCount(... }
  25.     public integer getTestCaseProgress(... }
  26. }
Here is what some of these methods mean. First the display methods that you will probably want to override... HtmlReporter(string $encoding) is the constructor. Note that the unit test sets up the link to the display rather than the other way around. The display is a mostly passive receiver of test events. This allows easy adaption of the display for other test systems beside unit tests, such as monitoring servers. The encoding is the character encoding you wish to display the test output in. In order to correctly render debug output when using the web tester, this should match the encoding of the site you are trying to test. The available character set strings are described in the PHP html_entities() function. void paintHeader(string $test_name) is called once at the very start of the test when the first start event arrives. The first start event is usually delivered by the top level group test and so this is where $test_name comes from. It paints the page title, CSS, body tag, etc. It returns nothing (void). void paintFooter(string $test_name) Called at the very end of the test to close any tags opened by the page header. By default it also displays the red/green bar and the final count of results. Actually the end of the test happens when a test end event comes in with the same name as the one that started it all at the same level. The tests nest you see. Closing the last test finishes the display. void paintMethodStart(string $test_name) is called at the start of each test method. The name normally comes from method name. The other test start events behave the same way except that the group test one tells the reporter how large it is in number of held test cases. This is so that the reporter can display a progress bar as the runner churns through the test cases. void paintMethodEnd(string $test_name) backs out of the test started with the same name. void paintFail(string $message) paints a failure. By default it just displays the word fail, a breadcrumbs trail showing the current test nesting and the message issued by the assertion. void paintPass(string $message) by default does nothing. string getCss() Returns the CSS styles as a string for the page header method. Additional styles have to be appended here if you are not overriding the page header. You will want to use this method in an overriden page header if you want to include the original CSS. There are also some accessors to get information on the current state of the test suite. Use these to enrich the display... array getTestList() is the first convenience method for subclasses. Lists the current nesting of the tests as a list of test names. The first, top level test case, is first in the list and the current test method will be last. integer getPassCount() returns the number of passes chalked up so far. Needed for the display at the end. integer getFailCount() is likewise the number of fails so far. integer getExceptionCount() is likewise the number of errors so far. integer getTestCaseCount() is the total number of test cases in the test run. This includes the grouping tests themselves. integer getTestCaseProgress() is the number of test cases completed so far. One simple modification is to get the HtmlReporter to display the passes as well as the failures and errors...
  1. class ReporterShowingPasses extends HtmlReporter {
  2.     
  3.     function paintPass($message{
  4.         parent::paintPass($message);
  5.         print "<span class=\"pass\">Pass</span>: ";
  6.         $breadcrumb $this->getTestList();
  7.         array_shift($breadcrumb);
  8.         print implode("-&gt;"$breadcrumb);
  9.         print "-&gt;$message<br />\n";
  10.     }
  11.     
  12.     protected function getCss({
  13.         return parent::getCss(' .pass { color: green; }';
  14.     }
  15. }

One method that was glossed over was the makeDry() method. If you run this method, with no parameters, on the reporter before the test suite is run no actual test methods will be called. You will still get the events of entering and leaving the test methods and test cases, but no passes or failures etc, because the test code will not actually be executed.

The reason for this is to allow for more sophistcated GUI displays that allow the selection of individual test cases. In order to build a list of possible tests they need a report on the test structure for drawing, say a tree view of the test suite. With a reporter set to dry run that just sends drawing events this is easily accomplished.

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 HtmlReporter we can take one step up the class hiearchy to SimpleReporter in the simple_test.php source file.

A do nothing display, a blank canvas for your own creation, would be...

  1. require_once('simpletest/simpletest.php');
  2.  
  3. class MyDisplay extends SimpleReporter {
  4.     
  5.     function paintHeader($test_name}
  6.     
  7.     function paintFooter($test_name}
  8.     
  9.     function paintStart($test_name$size{
  10.         parent::paintStart($test_name$size);
  11.     }
  12.     
  13.     function paintEnd($test_name$size{
  14.         parent::paintEnd($test_name$size);
  15.     }
  16.     
  17.     function paintPass($message{
  18.         parent::paintPass($message);
  19.     }
  20.     
  21.     function paintFail($message{
  22.         parent::paintFail($message);
  23.     }
  24.     
  25.     function paintError($message{
  26.         parent::paintError($message);
  27.     }
  28.     
  29.     function paintException($exception{
  30.         parent::paintException($exception);
  31.     }
  32. }
No output would come from this class until you add it.

The catch with using this low level class is that you must explicitely invoke it in the test script. The "autorun" facility will not be able to use it's runime context (whether it's running in a web browser or the command line) to select the reporter.

You explicitely invoke the test runner like so...

  1. <?php
  2. require_once('simpletest/autorun.php');
  3.  
  4. $test new TestSuite('File test');
  5. $test->addFile('tests/file_test.php');
  6. $test->run(new MyReporter());
  7. ?>
...perhaps like this...
  1. <?php
  2. require_once('simpletest/simpletest.php');
  3. require_once('my_reporter.php');
  4.  
  5. class MyTest extends TestSuite {
  6.     function __construct({
  7.         parent::__construct();
  8.         $this->addFile('tests/file_test.php');
  9.     }
  10. }
  11.  
  12. $test new MyTest();
  13. $test->run(new MyReporter());
  14. ?>
We'll show how to fit in with "autorun" later.

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 explicitely, substitute it for the HTML version...

  1. <?php
  2. require_once('simpletest/autorun.php');
  3.  
  4. $test new TestSuite('File test');
  5. $test->addFile('tests/file_test.php');
  6. $test->run(new TextReporter());
  7. ?>
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, Passes: 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, Passes: 0, 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...

  1. <?php
  2. require_once('simpletest/autorun.php');
  3.  
  4. $test new TestSuite('File test');
  5. $test->addFile('tests/file_test.php');
  6. exit ($test->run(new TextReporter()) 1);
  7. ?>
Of course we wouldn'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...
  1. <?php
  2. require_once('simpletest/autorun.php');
  3.  
  4. $test new TestSuite('File test');
  5. $test->addFile('tests/file_test.php');
  6. if (TextReporter::inCli()) {
  7.     exit ($test->run(new TextReporter()) 1);
  8. }
  9. $test->run(new HtmlReporter());
  10. ?>
This is the form used within SimpleTest itself. When you use the "autorun.php", and no test has been run by the end, this is pretty much the code that SimpleTest will run for you implicitely.

In other words, this is gives the same result...

  1. <?php
  2. require_once('simpletest/autorun.php');
  3.  
  4. class MyTest extends TestSuite {
  5.     function __construct({
  6.         parent::__construct();
  7.         $this->addFile('tests/file_test.php');
  8.     }
  9. }
  10. ?>

Remote testing

SimpleTest ships with an XmlReporter class used for internal communication. When run the output looks like... <?xml version="1.0"?> <run> <group size="4"> <name>Remote tests</name> <group size="4"> <name>Visual test with 48 passes, 48 fails and 4 exceptions</name> <case> <name>testofunittestcaseoutput</name> <test> <name>testofresults</name> <pass>This assertion passed</pass> <fail>This assertion failed</fail> </test> <test> ... </test> </case> </group> </group> </run> To get your normal test cases to produce this format, on the command line add the --xml flag. php my_test.php --xml You can do teh same thing in the web browser by adding the URL parameter xml=1. Any true value will do.

You can consume this format with the parser supplied as part of SimpleTest itself. This is called SimpleTestXmlParser and resides in xml.php within the SimpleTest package...

  1. <?php
  2. require_once('simpletest/xml.php');
  3.     
  4. ...
  5. $parser new SimpleTestXmlParser(new HtmlReporter());
  6. $parser->parse($test_output);
  7. ?>
The $test_output should be the XML format from the XML reporter, and could come from say a command line run of a test case. The parser sends events to the reporter just like any other test run. There are some odd occasions where this is actually useful.

Most likely it's when you want to isolate a problematic crash prone test. You can collect the XML output using the backtick operator from another test. In that way it runs in it's own process...

  1. <?php
  2. require_once('simpletest/xml.php');
  3.  
  4. if (TextReporter::inCli()) {
  5.     $parser new SimpleTestXmlParser(new TextReporter());
  6. else {
  7.     $parser new SimpleTestXmlParser(new HtmlReporter());
  8. }
  9. $parser->parse(`php flakey_test.php --xml`);
  10. ?>

Another use is breaking up large test suites.

A problem with large test suites is thet they can exhaust the default 16Mb memory limit on a PHP process. By having the test groups output in XML and run in separate processes, the output can be reparsed to aggregate the results into a much smaller footprint top level test.

Because the XML output can come from anywhere, this opens up the possibility of aggregating test runs from remote servers. A test case already exists to do this within the SimpleTest framework, but it is currently experimental...

  1. <?php
  2. require_once('../remote.php');
  3. require_once('simpletest/autorun.php');
  4.     
  5. $test_url = ...;
  6. $dry_url = ...;
  7.  
  8. class MyTestOnAnotherServer extends RemoteTestCase {
  9.     function __construct({
  10.         $test_url = ...
  11.         parent::__construct($test_url$test_url ' --dry');
  12.     }
  13. }
  14. ?>
The RemoteTestCase takes the actual location of the test runner, basically a web page in XML format. It also takes the URL of a reporter set to do a dry run. This is so that progress can be reported upward correctly. The RemoteTestCase can be added to test suites just like any other test suite.

Previous Up Next
Partial mocks Simple Test PHP Unit Test Framework Expectations

Documentation generated on Sun, 31 Oct 2010 16:30:47 -0500 by phpDocumentor 1.4.3