You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让PHPUnit输出错误信息?Windows命令行测试遇问题

PHPUnit 10.x 无法输出具体错误信息的解决办法

在Windows命令行执行PHPUnit命令:

php phpunit.phar --bootstrap .\commonTests\bootstrap_local.php .\commonTests\DIContainerTest.php --testdox --stderr -v

得到的输出仅显示错误总数,无具体信息:

PHPUnit 10.1.2 by Sebastian Bergmann and contributors.
Runtime: PHP 8.2.0
E
Time: 00:00.004, Memory: 24.00 MB
ERRORS! Tests: 1, Assertions: 0, Errors: 1.

已尝试以下方法但无效:

  • 使用--verbose、--debug参数,提示不支持
  • 创建phpunit.xml配置文件开启错误输出:
<phpunit>
    <php>
        <ini name="error_reporting" value="E_ALL" />
        <ini name="display_errors" value="1" />
        <ini name="display_startup_errors" value="1" />
    </php>
</phpunit>
  • 在测试类开头添加PHP代码开启错误报告:
ini_set('error_reporting', E_ALL);
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');

有效解决方法

1. 移除--testdox参数

--testdox会强制生成简化的文档式输出,自动隐藏详细错误堆栈。移除后执行命令:

php phpunit.phar --bootstrap .\commonTests\bootstrap_local.php .\commonTests\DIContainerTest.php --stderr -v

2. 使用PHPUnit 10.x专属输出参数

PHPUnit 10.x调整了参数命名,用以下参数强制显示错误详情:

php phpunit.phar --bootstrap .\commonTests\bootstrap_local.php .\commonTests\DIContainerTest.php --stderr -v --display-errors --display-deprecations --display-notices

3. 检查bootstrap文件的错误抑制

如果bootstrap_local.php中存在error_reporting(0)、@错误抑制符号,会覆盖全局错误配置,需修改该文件确保:

error_reporting(E_ALL);
ini_set('display_errors', 1);

4. 生成XML日志查看详情

如果命令行仍无法输出,可生成日志文件获取完整错误堆栈:

php phpunit.phar --bootstrap .\commonTests\bootstrap_local.php .\commonTests\DIContainerTest.php --stderr -v --log-junit test-errors.xml

打开生成的test-errors.xml文件,查看<failure>或<error>节点内的详细信息。

内容的提问来源于stack exchange,提问作者Jayy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 10:13:17