如何使 PHP 警告无法通过 PHPUnit 测试用例?

bor*_*ini 7 php phpunit integration-testing automated-tests jenkins

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

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

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

Ole*_*kin 6

我们必须添加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.
Run Code Online (Sandbox Code Playgroud)

所以我们的配置如下:

<phpunit 
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="true"
         convertWarningsToExceptions="true"
         stopOnFailure="false"
         failOnWarning="true">
Run Code Online (Sandbox Code Playgroud)


小智 3

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

<?xml version="1.0" encoding="UTF-8"?>
<phpunit
    convertErrorsToExceptions   = "true"
    convertNoticesToExceptions  = "true"
    convertWarningsToExceptions = "true">
</phpunit>
Run Code Online (Sandbox Code Playgroud)