我正在使用 PHPUnit 对我的函数进行单元测试,当代码中出现任何警告时,测试脚本将不会针对该函数执行,任何人都可以告诉我如何忽略警告并继续测试
最佳答案
正如 Juhana 所说,您应该首先修复出现警告的代码。这表明代码无法正常/严格运行。
By default, PHPUnit converts PHP errors, warnings, and notices that are triggered during the execution of a test to an exception.
请参阅 Testing PHP Errors,其中包含有关如何测试警告(以及如何忽略测试中调用的子例程中的警告)的更多信息。
要禁用默认行为,您可以在测试中告诉 PHPUnit 这样做,例如通过在全局命名空间中设置静态变量,在您的测试或测试本身的 setUp
中:
# Warning:
PHPUnit_Framework_Error_Warning::$enabled = FALSE;
# notice, strict:
PHPUnit_Framework_Error_Notice::$enabled = FALSE;
更改默认行为的另一个选项是 configure the testrunner with an XML file 使用以下设置:
<phpunit convertErrorsToExceptions="false"
convertNoticesToExceptions="false"
convertWarningsToExceptions="false">
</phpunit>
这三个选项不能用作命令行开关。
请参阅相关问题:test the return value of a method that triggers an error with PHPUnit。
关于php - 忽略 PHPUnit 中的 PHP 警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8412746/