PHPUnit - 为什么PHPUnit似乎在严格模式下运行?

  发布于 2022-12-07 15:04

问题:为什么PHPUnit似乎在严格模式下运行?

问题:

Sebastian Bergmann的PHPUnit 4.3.1.

配置从/full/path/to/configuration.xml读取

[R

时间:2.65秒,内存:11.50Mb

好的,但不完整,跳过或有风险的测试!测试:1,断言:1,风险:1.完成.

也:

危险测试:测试代码或测试代码没有(仅)关闭自己的输出缓冲区

我的PHP版本是5.4.

如文档(https://phpunit.de/manual/current/en/strict-mode.html)中所述,这似乎仅适用于PHPUnits的严格模式设置.

PHPUnit可以在执行测试时执行其他检查.除了对各种严格模式检查(见下文)的细粒度控制之外,您可以在PHPUnit的XML配置文件中使用--strict命令行选项或set strict ="true"来启用所有这些选项.

-

测试执行期间的输出

PHPUnit在测试期间可以严格控制输出.可以通过在命令行上使用--disallow-test-output选项或在PHPUnit的XML配置文件中设置beStrictAboutOutputDuringTests ="true"来启用此检查.

发出输出的测试(例如通过在测试代码或测试代码中调用print)将在启用此检查时标记为有风险.

我相信,我没有激活严格模式.我的命令行是"/ usr/bin/php/usr/bin/phpunit --colors --bootstrap /full/path/to/bootstrap.php --configuration/full/path/to/configuration.xml/full/path /to/Test.php".我还使用了" https://phpunit.de/manual/current/en/appendixes.configuration.html "中提供的配置.



我之前使用过这种配置的较短版本,提供了相同的结果.



xmojmr.. 6

看看GitHub 它上面的代码似乎无论文档说什么,都会检查并报告输出缓冲问题.

因此,您观察到的症状并不意味着测试以strict模式运行.

github.com/sebastianbergmann/phpunit/blob/4.3/src/Framework/TestCase.php#L818

// ...

try {
    $this->stopOutputBuffering();
} catch (PHPUnit_Framework_RiskyTestError $_e) {
    if (!isset($e)) {
        $e = $_e;
    }
}

github.com/sebastianbergmann/phpunit/blob/4.3/src/Framework/TestCase.php#L1938-L1946

private function stopOutputBuffering()
{
    if (ob_get_level() != $this->outputBufferingLevel) {
        while (ob_get_level() > 0) {
            ob_end_clean();
        }
        throw new PHPUnit_Framework_RiskyTestError(
            'Test code or tested code did not (only) close its own output buffers'
        );
    }

    // ...

    $this->outputBufferingActive = false;
    $this->outputBufferingLevel  = ob_get_level();
}

在您最喜欢的PHPUnit测试调试器的上面的行放置断点可能会揭示一些其他依赖项(如disallowTestOutput标志......?)

1 个回答
  • 看看GitHub 它上面的代码似乎无论文档说什么,都会检查并报告输出缓冲问题.

    因此,您观察到的症状并不意味着测试以strict模式运行.

    github.com/sebastianbergmann/phpunit/blob/4.3/src/Framework/TestCase.php#L818

    // ...
    
    try {
        $this->stopOutputBuffering();
    } catch (PHPUnit_Framework_RiskyTestError $_e) {
        if (!isset($e)) {
            $e = $_e;
        }
    }
    

    github.com/sebastianbergmann/phpunit/blob/4.3/src/Framework/TestCase.php#L1938-L1946

    private function stopOutputBuffering()
    {
        if (ob_get_level() != $this->outputBufferingLevel) {
            while (ob_get_level() > 0) {
                ob_end_clean();
            }
            throw new PHPUnit_Framework_RiskyTestError(
                'Test code or tested code did not (only) close its own output buffers'
            );
        }
    
        // ...
    
        $this->outputBufferingActive = false;
        $this->outputBufferingLevel  = ob_get_level();
    }
    

    在您最喜欢的PHPUnit测试调试器的上面的行放置断点可能会揭示一些其他依赖项(如disallowTestOutput标志......?)

    2022-12-11 02:04 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有