Can you run PHPUnit tests from a script?

Simplest way to do this is by instantiating object of class PHPUnit_TextUI_Command.

So here is an example:

require '/usr/share/php/PHPUnit/Autoload.php';

function dummy($input)
{
   return '';
}

//Prevent PHPUnit from outputing anything
ob_start('dummy');

//Run PHPUnit and log results to results.xml in junit format
$command = new PHPUnit_TextUI_Command;
$command->run(array('phpunit', '--log-junit', 'results.xml', 'PHPUnitTest.php'),
              true);

ob_end_clean();

This way the results will be logged in results.xml file in junit format that can be parsed. If you need a different format you can check the documentation. Also you can add more options by changing the array passed to run method.


Figured it out:

$phpunit = new PHPUnit_TextUI_TestRunner;

try {
    $test_results = $phpunit->dorun($phpunit->getTest(__DIR__, '', 'Test.php'));
} catch (PHPUnit_Framework_Exception $e) {
    print $e->getMessage() . "\n";
    die ("Unit tests failed.");
}

Tags:

Php

Phpunit