php - 如何使 PHPunit 在警告时返回非零退出状态

标签 php phpunit

在某些因警告而失败的测试中调用 PHPunit 时,我得到:

$ phpunit -c phpunit.xml --group app
Warning - MongoCollection::insert(): expects parameter 1 to be an array or object, null given in ...
    <more output>

OK, but incomplete or skipped tests!
Tests: 17, Assertions: 81, Incomplete: 1.

其中一个测试应该会失败,但实际上并没有; PHPunit 将其标记为“不完整”。

让我们检查最后的退出状态:

$ echo $?
0

我使用的配置是:

<phpunit
    convertErrorsToExceptions="true"
    convertNoticesToExceptions="true"
    convertWarningsToExceptions="true"
    strict="true"
    stopOnError="true"
    stopOnFailure="true"
    stopOnIncomplete="true"
    stopOnSkipped="true"

    colors="true"
    bootstrap="bootstrap_phpunit.php"
    >

知道如何在“不完整”测试的情况下强制 PHPunit 发出非零退出状态吗?

最佳答案

感谢 gontrollez,我开始研究错误处理程序并最终找到了解决方案:

set_error_handler(function ($severity, $message, $filepath, $line)
{
    throw new Exception($severity." - ".$message." - ".$filepath." - ".$line);
});

此代码抛出异常,导致 PHPunit 将测试正确检测为 failed 而不是 incomplete。它应该放在bootstrap_phpunit.php 中的某处(即在文件中,指定为bootstrap 文件)。

关于php - 如何使 PHPunit 在警告时返回非零退出状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24305936/

相关文章:

php - 通过某些路由前缀捕获未找到错误 [Laravel]

php - URL 打错 Controller LARAVEL

installation - PHPUnit 中的 setup() 和 Codeception 中的 _before 有什么区别

php - 为什么 PHPUnit 在模拟不存在的方法时默默地不返回任何内容?

PHPunit:我可以模拟我测试的类吗?

php - Laravel phpunit 总是 404

nginx - 无法从 Nginx php - 基于 fpm 的 dedi 发送电子邮件?

php - Joomla 查询 - 从发布状态的内容中选择

php - 在 PHP 中使用内联查询与存储过程

php - 在 PHPUnit 中使用 namespace 时“找不到类”