情况
我正在实施一个PHPUnit监听器,它会将测试结果记录到数据库中供以后检查。当遇到故障,错误,跳过或不完整的测试时,我喜欢输出到控制台的默认信息phpunit
。是否可以使用Listener来模拟PHPUnit的输出到控制台结果?
例子:
1) sandbox_ExtensionSampleTest::testError
printf(): Too few arguments
E:\ecom_testing\tests_v2\TestSuites\sandbox\Base.php:28
E:\ecom_testing\tests_v2\TestSuites\sandbox\ExtensionSampleTest.php:37
C:\Program Files (x86)\EasyPHP-5.3.8.1\php\pear\pear\PHPUnit\Framework\TestCase.php:939
C:\Program Files (x86)\EasyPHP-5.3.8.1\php\pear\pear\PHPUnit\Framework\TestCase.php:801
C:\Program Files (x86)\EasyPHP-5.3.8.1\php\pear\pear\PHPUnit\Framework\TestResult.php:649
C:\Program Files (x86)\EasyPHP-5.3.8.1\php\pear\pear\PHPUnit\Framework\TestCase.php:748
C:\Program Files (x86)\EasyPHP-5.3.8.1\php\pear\pear\PHPUnit\Framework\TestSuite.php:772
C:\Program Files (x86)\EasyPHP-5.3.8.1\php\pear\pear\PHPUnit\Framework\TestSuite.php:745
C:\Program Files (x86)\EasyPHP-5.3.8.1\php\pear\pear\PHPUnit\TextUI\TestRunner.php:325
C:\Program Files (x86)\EasyPHP-5.3.8.1\php\pear\pear\PHPUnit\TextUI\Command.php:187
C:\Program Files (x86)\EasyPHP-5.3.8.1\php\pear\pear\PHPUnit\TextUI\Command.php:125
C:\Program Files (x86)\EasyPHP-5.3.8.1\php\pear\phpunit:44
FAILURES!
Tests: 1, Assertions: 0, Errors: 1.
注:我确实发现在输出列出所有PHPUnit的依赖关系是a bug,但上面的例子还显示了两个相关的测试文件。
问题一项所述的用于实现侦听方法的参数之一是Exception $e
。我可以通过使用$e
得到消息以及第一行,但我似乎无法得到其余的行。
PHP手册阅读异常之后,我想用$e->getPrevious()
会工作......试图做到以下几点:
// Storing in a array to put into a DB later
// First line with the message
$trace[] = sprintf(
"%s\n\n%s:%s\n",
$e->getMessage(),
$e->getFile(),
$e->getLine()
);
// Go through the rest of the exceptions of files and lines
while ($e = $e->getPrevious()){
$trace[] = sprintf(
"%s:%s\n",
$e->getFile(),
$e->getLine()
);
}
这没有奏效。好像$e->getPrevious()
是空的,当我倾倒物体时,我确实证实了它。我认为像$e->getTraceAsString()
会产生相同的结果,但它不是我所期望的(我没有使用过多的例外)
太棒了,这正是我想要的。谢谢! – NkM 2012-02-10 02:31:27