Running PHPUnit tests in a webpage

Given that there isn’t currently built in support for PHPUnit testing in browser although it was being put into the roadmap according to an article by the author (http://sebastian-bergmann.de/archives/639-AJAX-Based-Test-Runner-for-PHPUnit.html#content) I took a script someone else had put together and modified it slightly. I can’t find the original script now so I apologies for not crediting the author. The script goes a little something like this

require_once('PHPUnit/Util/Log/JUnit.php');

function bfglob($path, $pattern = '*', $flags = 0, $depth = 0) {
  $matches = array();
  $folders = array(rtrim($path, DIRECTORY_SEPARATOR));

  while($folder = array_shift($folders)) {
    $matches = array_merge($matches, glob($folder.DIRECTORY_SEPARATOR.$pattern, $flags));
    if($depth != 0) {
      $moreFolders = glob($folder.DIRECTORY_SEPARATOR.'*', GLOB_ONLYDIR);
      $depth   = ($depth < -1) ? -1: $depth + count($moreFolders) - 2;
      $folders = array_merge($folders, $moreFolders);
    }
  }
  return $matches;
}

class SPTestRunner{
  public static function run(){
    // Create the test suite instance
    $suite = new PHPUnit_Framework_TestSuite();
    $suite->setName('SPTestRunner');

    $filenames = bfglob(__DIR__,"*Test.php",0,-1);
    // Add files to the TestSuite
    $suite->addTestFiles($filenames);

    // Create a xml listener object
    $listener = new PHPUnit_Util_Log_JUnit;

    // Create TestResult object and pass the xml listener to it
    $testResult = new PHPUnit_Framework_TestResult();
    $testResult->addListener($listener);

    // Run the TestSuite
    $result = $suite->run($testResult);

    // Get the results from the listener
    $xml_result = $listener->getXML();
    return $xml_result;
  }
}

$test_xml = SPTestRunner::run();
$simple = new SimpleXMLElement($test_xml);

$test_results = array();
$assertions = 0;
$time = 0.0;
$tests = 0;
foreach($simple->{'testsuite'}->testsuite as $testsuite){
  foreach($testsuite->testcase as $testcase){
    $result = array();
    $tests += 1;
    // Don't froget to cast SimpleXMLElement to string!
    $result['name'] = (string)$testcase['name'];
    $result['suite'] = (string)$testcase['class'];
    $assertions += $result['assertions'] = (string)$testcase['assertions'];
    $time += $result['time'] = (string)$testcase['time'];
    if(isset($testcase->{'failure'})){
      $result['result'] = 'Fail';
      $result['message'] = (string)$testcase->{'failure'};
    }else{
      $result['result'] = 'Pass';
      $result['message'] = '';
    }
    $test_results[] = $result;
  }
}
?>

Suite Test Name Result Message Assertions Time
Fail Pass
TESTS ASSERTIONS SEC

It’s by no means pretty, but it does the job, just drop it in a folder at the root of your tests and it will look for all files including subdirectories that look like *Test.php and run them. Unfortunately because the script first has to wait for the XML to come back form PHPUnit you will not see the result of your tests until they have all finished running.

AJAX PHPUnit Runner

I’m hoping to find some time to able to realise an AJAX driven interface that allow you to select certain groups / tests / suites and run them in browser with realtime results. If anyway wants to lend me a hand I’m open to offers.

Share and Enjoy:
  • Print
  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay
  • StumbleUpon
  • Twitter

About Danny

Primarily a Ruby On Rails developer, but I can do other cool stuff too.
This entry was posted in Development, PHP. Bookmark the permalink.

One Response to Running PHPUnit tests in a webpage

  1. Johnny says:

    Thank you so much for your example! This helps me a lot to understand how XML works in PHPUnit.
    I am using PHPUnit 3.5.13 and tried to follow your example.
    By running my script with command line using –log-xml, I can generate the XML string from the output. However, when I included it in my script like your example, it returns no output except my debug statements. I tried to switch to JSON in the script and it works.
    For PHPUnit_Util_Log_JUnit class, do I need to include any other paths in my scripts or configuration setup for XML?

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>