我遇到的情况是我直接调用PHPUnit_TextUI_TestRunner
try {
error_log('phpunit');
$phpunit = new PHPUnit_TextUI_TestRunner();
$test_results = $phpunit->dorun($phpunit->getTest(__DIR__.'/../tests', '', 'Test.php'));
error_log($test_results->count().' '.$test_results->failureCount());
} catch (PHPUnit_Framework_Exception $e) {
print $e->getMessage() . "\n";
error_log("Unit tests failed.");
}
根据此帖子中的答案Can you run PHPUnit tests from a script?
在我的情况下,我想查看测试运行时的数量和详细信息,并将其显示给用户。我已更新代码以设置打印机对象
try {
error_log('phpunit');
$phpunit = new PHPUnit_TextUI_TestRunner();
// set the printer
$resultPrinter = new PHPUnit_TextUI_ResultPrinter();
$phpunit->setPrinter($resultPrinter);
$test_results = $phpunit->dorun($phpunit->getTest(__DIR__.'/../tests', '', 'Test.php'));
error_log($test_results->count().' '.$test_results->failureCount());
// print results returns nothing?
$resultPrinter->printResult($test_results);
$resultPrinter->flush();
} catch (PHPUnit_Framework_Exception $e) {
print $e->getMessage() . "\n";
error_log("Unit tests failed.");
}
但我的日志中没有任何内容。有人可以建议我如何捕获和显示PHPUnit_TextUI_ResultPrinter的输出吗?