SimpleTest pretty much follows the MVC 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.
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...
For web page based displays there is the
HtmlReporter class with the following
signature...
HtmlReporter(string $encoding)void paintHeader(string $test_name)$test_name
comes from.
It paints the page titles, CSS, body tag, etc.
It returns nothing (void).
void paintFooter(string $test_name)void paintMethodStart(string $test_name)void paintMethodEnd(string $test_name)void paintFail(string $message)void paintPass(string $message)string _getCss()array getTestList()integer getPassCount()integer getFailCount()integer getExceptionCount()integer getTestCaseCount()integer getTestCaseProgress()
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.
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...
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 file_test.phpYou 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: 0A 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...
SimpleTest ships with an XmlReporter class
used for internal communication.
When run the output looks like...
You can make use of this format with the parser supplied as part of SimpleTest itself. This is called]]> Remote tests Visual test with 48 passes, 48 fails and 4 exceptions testofunittestcaseoutput testofresults This assertion passed This assertion failed ...
SimpleTestXmlParser and
resides in xml.php within the SimpleTest package...
$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.
A problem with large test suites is thet they can exhaust the default 8Mb 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...
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 group test.