php - 忽略 PHPUnit 中的 PHP 警告

标签 php unit-testing phpunit

我正在使用 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/

相关文章:

visual-studio - 如何在 Visual Studio 的特定文件夹中运行单个测试或所有测试?

java - 单元测试等于和哈希码 - 一个复杂的故事

selenium - 对刚刚执行了 type() 的元素进行 verifyText() 有意义吗?

php - yii 调用身份验证访问规则部分中未定义的方法

unit-testing - 如何加速 Spark SQL 单元测试?

php - 无法在 XAMPP 中安装 PHPUnit pear 包

Laravel 和 PHPUnit : Getting 500 when unit testing "Passport" restricted route

php - 使用 PHP 数组显示下拉菜单

php - preg_replace file_get_contents 中不包含单词的所有链接

php - 将 curl stdout 捕获到 PHP 中的变量中