Browser based PHPUnit testing

Why Browser based PHPUnit testing

When testing websites the first thing most people do is to point their browser at the site and look. So why can the same not be done to view the results of PHPUnit tests on the site or its components and libraries.

PHPUnit forces the web developer to run it in at the command prompt in terminal window. Admittedly it does offer some ANSI color support to make it a bit prettier.

Want to see it in action! A dummy demo the output of its own test run can be seen at http://www.nickturner.co.uk/libs/PHPUnit_Html/demo/.

So what is PHPUnit_Html ?

PHPUnit_Html is a front end to PHPUnit that presents the test results in nice HTML format in your browser. It supports the display of most of the features reported by PHPUnit and allows control of PHPUnit's options via the request URL instead of command line options.

Easy to use! Simply install the PHPUnit_Html library to where you like and take a copy of its example tests/index.php file and drop it into your own tests directory. If you have installed PHPUnit_Html and PHPUnit into directories in your PHP include path then their is nothing more to do - just point your browser at the index.php file and view the test results.

If the PHPUnit_Html or PHPUnit directories are not in the PHP include path and you don't want to or can't add them then you can edit the index.php file to specify their locations - just follow the instructions in the file.

Self Discovery

PHPUnit_Html will by default just discover all the *Test.php files in the folder tree containing the index.php file, however like PHPUnit it accepts the test and testFile parameters.

PHPUnit command line parameter support

The web script will accept a large proportion of PHPUnit's command line parameters via normal URL request parameters.

For example, the equivalent of running phpunit --bootstrap <bootstrap.php> --stop-on-error UnitTest UnitTest.php would be to browse to the url index.php?stopOnError=true&bootstrap=bootstrap.php&test=UnitTest&testFile=UnitTest.php.

The full list of supported URL parameters is as follows;

test                    The test to run (or directory of tests to run)
testFile                The file containing the test

filter={pattern}        Filter which tests to run.
groups={groups}         Only runs tests from the specified group(s).
excludeGroups={groups}  Exclude tests from the specified group(s).

stopOnError             Stop execution upon first error.
stopOnFailure           Stop execution upon first error or failure.
stopOnSkipped           Stop execution upon first skipped test.
stopOnIncomplete        Stop execution upon first incomplete test.
strict                  Mark a test as incomplete if no assertions are made.

processIsolation        Run each test in a separate PHP process.
noGlobalsBackup         Backup and restore $GLOBALS for each test.
staticBackup            Backup and restore static attributes for each test.
syntaxCheck             Try to check source files for syntax errors.

bootstrap={file}        A "bootstrap" PHP file that is run before the tests.

coverageHtml={dir}      Generate code coverage report in HTML format.
coverageClover={file}   Write code coverage data in Clover XML format.

tpldir={dir}            Template directory
template={name}         Name of template if in default template directory

For the boolean parameters their associated value must be one of 'true,1,false,0', a missing value means true. For example; 'stopOnError', 'stopOnError=true', 'stopOnError=1' all enable the options, while 'stopOnError=false', 'stopOnError=0' will disable the option.

If the 'test' parameter is omitted then all the tests in the current directory are run (getcwd()).

URL parameters are case sensitive!!

Suite Results Display

PHPUnit_Html will display the number of tests in each test suite along with how many passed, failed, were marked skipped or were marked incomplete. It will also display the number of assertions made by the tests in the suites, the number of problems (errors or deprecated warnings) and the total execution time.

Example:

Example Suite Results Display

Test Results Display

PHPUnit_Html will display any the number of assertions and problems generated by each test, along with the total execution time.

The details of any failed assertion that halted the test will be displayed along with it's appropriate stack trace. The stack trace is filter in a similar why to the normal PHPUnit output to remove stack frames generated by the testing framework itself.

Example:

Example Test Results Display

Test Errors Display

PHPUnit_Html will display any exceptions and errors (trigger_error()) generated by the test and display them along with their appropriate stack trace. The stack trace is filter in a similar why to the normal PHPUnit output to remove stack frames generated by the testing framework itself.

The default template will allow the displaying of the stack frames to be toggled.

Also any deprecated feature messages generated by PHPUnit during executing the test will be displayed.

Example:

Example Test Errors Display

Test Output Display

PHPUnit_Html will capture any output generated by the test and display it in the final results escaping an HTML reserved characters appropriately.

Example:

function test2() {
    global $fred;
echo '$fred=';
    print_r($fred);
    $this->assertEquals($fred, array(2, 4, 6));
}

Example Test Output Display

Source Code Display

PHPUnit_Html will display the source code of each test so that you can easily cross reference points of failure to the appropriate source line. As the source code could be quite large the default template allows this to be optionally displayed.

Example:

Example Test Source Display

Template Based Output

PHPUnit_Html using a simple PHP template based output which neatly separates the testing logic from the display logic. At the moment it comes with a single example template which in most case will be all that is needed. However designers can clone this template and modify it to suite.

The default template supports simple expansion and contraction of test results to control how much information is displayed. By default failing tests are shown initially expanded.

Getting PHPUnit_Html

PHPUnit_Html can be downloaded from GitHub.

The sample index.php file to copy to your test directory can be found in the PHPUnit_Html test directory '\test\'.

Want to see it in action! A dummy demo the output of its own test run can be seen at http://www.nickturner.co.uk/libs/PHPUnit_Html/demo/.

© Nick Turner 2011 - Based on initial ideas by Matt Mueller