douqiu9529 2013-01-03 19:29
浏览 37
已采纳

使用phpunit的自定义侦听器,测试状态为null

I am working on a custom test listener for phpunit and I am unable to get the status of a test after it finishes. When I call getStatus() on the test after it is finished I am getting null instead of the status. I have tried this out on some tests where one passes, one is incomplete and one fails, but getStatus() returns null for all of them.

I am using PHPUnit version 3.7.10

Here is my test listener code:

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
";

        }
    }
}

Anyone have any ideas of what I am doing wrong?

  • 写回答

1条回答 默认 最新

  • dounianxie2058 2013-01-03 22:35
    关注

    The interface PHPUnit_Framework_Test doesn't even have a getStatus() method, so you're likely to run into more problems. Better capture the results in the add*() methods.

    Have a look at PHPUnit/TextUI/ResultPrinter.php, it does it this way.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据