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

如何让PHPUnit 11在测试运行时实时输出测试结果

让PHPUnit 11实时输出测试结果的方法

问题说明

我当前使用PHPUnit 11.4.3版本,之前在8、9版本中运行测试时会实时输出每一项测试的结果,但现在运行过程中仅显示以下内容,要等所有测试完成后才会展示全部结果:

Runtime:       PHP 8.2.12
Configuration: tests\phpunit.xml

.................................................

当前执行命令

vendor\bin\phpunit -c tests\phpunit.xml

执行后显示:

PHPUnit 11.4.3 by Sebastian Bergmann and contributors.

Runtime:       PHP 8.2.12
Configuration: tests\phpunit.xml

当前phpunit.xml配置

<?xml version="1.0"?>
<phpunit
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.2/phpunit.xsd"
    backupGlobals="true"
    backupStaticProperties="false"
    cacheDirectory=".phpunit.cache"
    cacheResult="false"
    colors="true"
    displayDetailsOnTestsThatTriggerWarnings="true"
    processIsolation="false"
    requireCoverageMetadata="false"
    stopOnError="false"
    stopOnFailure="false"
    stopOnIncomplete="false"
    stopOnSkipped="false"
    stopOnRisky="false"
    testdox="true"
    timeoutForSmallTests="1"
    timeoutForMediumTests="10"
    timeoutForLargeTests="60"
    >
    <coverage/>
    <php>
        <ini
            name="display_errors"
            value="true"/>
    </php>
    <source/>
    <testsuites>
        <testsuite name="all">
            <file>PageTests.php</file>
            <file>UnitTests.php</file>
        </testsuite>
    </testsuites>
</phpunit>

解决办法

方法1:命令行添加禁用输出缓冲参数

执行测试时加上--no-output-buffering参数,命令改为:

vendor\bin\phpunit -c tests\phpunit.xml --no-output-buffering

这个参数会关闭PHPUnit的输出缓冲,让TestDox格式的结果实时输出。

方法2:修改配置文件禁用输出缓冲

在phpunit根标签中添加outputBuffering="false"属性,修改后的配置片段如下:

<phpunit
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.2/phpunit.xsd"
    backupGlobals="true"
    backupStaticProperties="false"
    cacheDirectory=".phpunit.cache"
    cacheResult="false"
    colors="true"
    displayDetailsOnTestsThatTriggerWarnings="true"
    processIsolation="false"
    requireCoverageMetadata="false"
    stopOnError="false"
    stopOnFailure="false"
    stopOnIncomplete="false"
    stopOnSkipped="false"
    stopOnRisky="false"
    testdox="true"
    timeoutForSmallTests="1"
    timeoutForMediumTests="10"
    timeoutForLargeTests="60"
    outputBuffering="false"
>

保存配置后,直接执行原命令即可实时输出测试结果。

方法3:使用 verbose 模式输出详细信息

如果需要实时查看每个测试的详细执行情况,可以添加--verbose(或简写-v)参数:

vendor\bin\phpunit -c tests\phpunit.xml -v

该模式会实时输出每个测试方法的名称及执行结果。

内容的提问来源于stack exchange,提问作者James John McGuire 'Jahmic'

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.16 01:33:24