PHPUnit报错仍返回exit code 0导致CI误判成功该如何配置解决?
你需要在phpunit.xml的<phpunit>根节点新增两个配置项即可解决问题:
failOnEmptyTestSuite="true":当因类加载错误、语法错误等情况导致没有任何测试用例被正常执行时,强制返回非0退出码failOnError="true":遇到PHP层面的致命错误(包括类未找到、语法错误等)时直接标记为执行失败,返回非0退出码
修改后的完整配置参考:
<phpunit backupGlobals="false" backupStaticAttributes="false" bootstrap="bootstrap/app.php" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" beStrictAboutTestsThatDoNotTestAnything="true" failOnEmptyTestSuite="true" failOnError="true">
如果你的PHPUnit版本低于8.4,上述两个配置项不支持,可选择两种兼容方案:
- 升级PHPUnit到8.4及以上稳定版本
- 在CI执行脚本中增加输出校验逻辑,匹配到错误关键词时手动返回非0退出码,示例脚本片段:
phpunit 2>&1 | tee phpunit_output.txt if grep -q -E "Fatal error|Class .* not found" phpunit_output.txt; then exit 1 fi exit ${PIPESTATUS[0]}
内容的提问来源于stack exchange,提问作者Marcelo de Andrade
相关产品推荐
相关产品推荐

