如何使PHP警告在PHPUnit测试用例中失败


How to make PHP warnings fail PHPUnit test cases?

我正在为一个PHP项目运行与Jenkins、Ant和PHPUnit 4.5.0的持续集成。Jenkins的xUnit插件将处理PHPUnit生成的XML日志。

一些重大错误(例如,引用未推送到VCS的文件)只会在PHPUnit中引发PHP警告,并且警告不包括在日志中。因此,即使需要修复,该构建也被标记为成功。

如何使PHP警告在构建中失败,例如,通过为生成警告的测试引发异常?

我们必须添加failOnWarning="true"才能将此类警告视为错误:

There was 1 warning:
1) The data provider specified for Tests'CreateSomethingTest::testCreateSomething is invalid.
FAILURES!
Tests: 841, Assertions: 2493, Failures: 1, Errors: 0.

所以我们的配置看起来像:

<phpunit 
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="true"
         convertWarningsToExceptions="true"
         stopOnFailure="false"
         failOnWarning="true">

确保选项convert...ToExceptions设置为true。不幸的是,命令行中没有可用的选项,因此您必须创建phpunit.xml:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit
    convertErrorsToExceptions   = "true"
    convertNoticesToExceptions  = "true"
    convertWarningsToExceptions = "true">
</phpunit>