【问题标题】:PHPUnit - Why would PHPUnit appear to be running in strict mode?PHPUnit - 为什么 PHPUnit 似乎在严格模式下运行?
【发布时间】:2015-02-04 10:52:28
【问题描述】:

问题:为什么 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 可以在执行测试时执行额外的检查。在 除了对各种严格模式的细粒度控制 检查(见下文)您可以使用 --strict 命令行选项或设置 PHPUnit 的 XML 配置文件中的 strict="true" 以启用所有 他们。

-

测试执行期间的输出

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

发出输出的测试,例如通过在任一 测试代码或被测试的代码,在这个检查时会被标记为有风险的 已启用。

我相信,我没有激活严格模式。我的命令行是“/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”提供的配置。

<phpunit
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.3/phpunit.xsd"
   backupGlobals="true"
   backupStaticAttributes="false"
   cacheTokens="false"
   colors="false"
   convertErrorsToExceptions="true"
   convertNoticesToExceptions="true"
   convertWarningsToExceptions="true"
   forceCoversAnnotation="false"
   mapTestClassNameToCoveredClassName="false"
   printerClass="PHPUnit_TextUI_ResultPrinter"
   processIsolation="false"
   stopOnError="false"
   stopOnFailure="false"
   stopOnIncomplete="false"
   stopOnSkipped="false"
   testSuiteLoaderClass="PHPUnit_Runner_StandardTestSuiteLoader"
   timeoutForSmallTests="1"
   timeoutForMediumTests="10"
   timeoutForLargeTests="60"
   strict="false"
   verbose="false">
</phpunit>

我之前使用过此配置的较短版本,它提供了相同的结果。

<phpunit
   beStrictAboutOutputDuringTests="false"
   strict="false"
   colors="false">
</phpunit>

【问题讨论】:

标签: php unit-testing phpunit


【解决方案1】:

从输出我相信我假设你只做一个测试是正确的,所以设置 --stop-on-risky 在这里并没有真正的帮助。

我建议您确保在代码本身中关闭输出缓冲。如果你曾经使用过 ob_start 之类的东西,请确保在脚本停止执行之前调用 ob_end_clean 或 ob_end_flush。

再想一想,也许尝试在运行时传递 -d 和 -v 标志,看看它是否为您提供更多信息。

【讨论】:

  • 你可以edit你所有的帖子和cmets。也许您应该将评论内容移动到您的答案中,然后删除评论。
【解决方案2】:

查看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 标志...?)

【讨论】:

  • 这确实解释了问题。我已经修复了我当时使用的库中的缓冲问题,此后没有遇到类似的情况。 help.slimframework.com/discussions/problems/…
  • @xmojmr,这应该被认为是一个错误吗?
  • @Pacerier 可能是。我对phpunit 的文档不太熟悉。如果此行为与文档所述相矛盾,则应将其视为错误,因此应在 issue tracker 中与其他 147 个打开的问题一起注册
【解决方案3】:

在您的代码库中搜索 error_reporting。您可能在代码中打开了严格模式。 error_reporting(E_NOTICE) 足以触发您收到的危险警告。

PHPUnit 不是一个特殊的二进制文件(我曾经有过这种误解),它只是 PHP 执行一些运行代码的引导机制。这意味着您的代码与 PHPUnit 共享相同的环境——因此您可能在代码中的某处将错误报告设置为严格。

【讨论】:

  • 我关闭了 e_notice。问题在于输出缓冲。
猜你喜欢
  • 2012-05-18
  • 2016-04-18
  • 2017-08-31
  • 2015-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-22
相关资源
最近更新 更多