【问题标题】:Phpunit test with disabled error handling is totally slow禁用错误处理的 Phpunit 测试非常慢
【发布时间】:2014-06-22 22:30:29
【问题描述】:

我重构了一些代码,并使用trigger_error()E_USER_DEPRECATED 常量引入了弃用警告。

因此我需要修改涉及该功能的测试

public function testMethod()
{
    $expected = 'value';
    $actual   = $this->subject->callDeprecatedMethod();
    $this->assertEquals($expected, $actual)
}

这样方法调用不会引发异常(例如,如herehere 所述):

public function testMethod()
{
    $expected = 'value';

    $save = PHPUnit_Framework_Error_Deprecated::$enabled;
    PHPUnit_Framework_Error_Deprecated::$enabled = false;
    $actual = $this->subject->callDeprecatedMethod();
    PHPUnit_Framework_Error_Deprecated::$enabled = save;

    $this->assertEquals($expected, $actual)
}

这很好用,但是我意识到,在运行所有测试时,测试运行程序突然在一个点上花费了更长的时间,并且在 checking against JSON 和 PHPStorm 中,为此刚刚更改了 testMethod,时间从毫秒上升到0.71 秒。

如何防止这种情况发生?

我需要我的测试快速快速运行:)

【问题讨论】:

    标签: php error-handling phpunit


    【解决方案1】:

    看起来,如果发生错误,PHPUnit 错误处理程序确实保留了一些信息,尽管错误异常被禁用。由于这些回溯可能更大,我认为这是迭代那些加上处理数据。

    所以我的想法是将错误处理程序排除在外——感谢将$actual 结果存储到变量中——然后再次重新启用 PHPUnits 错误处理程序:

    public function testMethod()
    {
        $expected = 'value';
    
        $noop = function() {};
        $previous = set_error_handler($noop);
    
        $actual = $this->subject->callDeprecatedMethod();
    
        set_error_handler($previous);
    
        $this->assertEquals($expected, $actual)
    }
    

    这会恢复原来的时间安排。

    实际上,我后来发现,必须使用错误抑制运算符才能产生类似的效果。尽管它没有禁用 PHPUnit 的处理程序,但恢复了快速计时:

    public function testMethod()
    {
        $expected = 'value';
    
        $actual = @$this->subject->callDeprecatedMethod();
    
        $this->assertEquals($expected, $actual)
    }
    

    使用起来也更舒服。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-18
      • 2018-11-12
      • 2016-02-16
      • 1970-01-01
      • 2019-06-22
      相关资源
      最近更新 更多