我正在为 phpunit 开发一个自定义测试侦听器,完成后我无法获得测试的状态。当我在测试完成后调用 getStatus() 时,我得到的是 null 而不是状态。我在一些测试中尝试过这一点,其中一个通过,一个不完整,一个失败,但 getStatus() 为所有测试返回 null。
我正在使用 PHPUnit 3.7.10 版
这是我的测试监听器代码:
class MyTestListener implements PHPUnit_Framework_TestListener
{
protected $tests = array();
public function addError(PHPUnit_Framework_Test $test, Exception $e, $time) {}
public function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time) {}
public function addIncompleteTest(PHPUnit_Framework_Test $test, Exception $e, $time) {}
public function addSkippedTest(PHPUnit_Framework_Test $test, Exception $e, $time) {}
public function startTest(PHPUnit_Framework_Test $test) {}
public function endTest(PHPUnit_Framework_Test $test, $time) {
$this->tests[] = array('test'=>$test, 'time'=>$time);
$status = $test->getStatus();
//NOTE: $status is null
}
public function startTestSuite(PHPUnit_Framework_TestSuite $suite) {}
public function endTestSuite(PHPUnit_Framework_TestSuite $suite) {}
public function __destruct() {
foreach($this->tests as $d) {
$test = $d['test'];
$time = $d['time'];
$testClass = get_class($test);
$status = $test->getStatus();
//NOTE: $status is null here
if($status == PHPUnit_Runner_BaseTestRunner::STATUS_FAILURE) {
$status = "fail - ";
} else if($status == PHPUnit_Runner_BaseTestRunner::STATUS_SKIPPED) {
$status = "skip - ";
} else if($status == PHPUnit_Runner_BaseTestRunner::STATUS_INCOMPLETE) {
$status = "inc - ";
} else if($status == PHPUnit_Runner_BaseTestRunner::STATUS_ERROR) {
$status = "err - ";
} else {
$status = $status."ok - ";
}
print $status.$testClass."::".$test->getName()." ".round($time,1)." seconds\n";
}
}
}
有人对我做错了什么有任何想法吗?