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

升级PHPUnit相关依赖后Selenium2TestCase类所有方法被执行问题

PHPUnit升级后Selenium测试套件全量执行非test前缀方法故障

背景说明

我最近通过composer更新了测试依赖,从如下版本:

"phpunit/phpunit": "8.5.*",
"phpunit/phpunit-selenium": "8.0.0"

升级到以下版本,运行环境为PHP 7.4:

"phpunit/phpunit-selenium": "^9.0",
"se/selenium-server-standalone": "3.141.59",
"phpunit/phpunit": "9.5.*"

故障现象

升级后出现异常:每次调用该测试套件时,类的所有成员方法都会被执行。我的phpunit.xml配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="true"
         backupStaticAttributes="false"
         bootstrap="vendor/autoload.php"
         colors="true"
         convertErrorsToExceptions="false"
         convertNoticesToExceptions="false"
         convertWarningsToExceptions="false"
         stopOnFailure="false"
         processIsolation="true"
>
    <testsuites>
        <testsuite name="feature">
            <file>tests/feature/WordpressTest.php</file>
            <file>tests/feature/RestTest.php</file>
            <file>tests/feature/BracketTest.php</file>
            <file>tests/feature/PayPalTest.php</file>
            <file>tests/feature/TwitchApiTest.php</file>
            <file>tests/feature/DiscordApiTest.php</file>
        </testsuite>
        <testsuite name="browser">
            <file>tests/browser/NavigationTest.php</file>
        </testsuite>
    </testsuites>
    <php>
        <env name="TEST" value="TRUE"/>
        <env name="CACHE_DRIVER" value="array"/>
        <env name="SESSION_DRIVER" value="array"/>
        <env name="QUEUE_DRIVER" value="sync"/>
        <ini name="display_errors" value="true"/>
    </php>
</phpunit>

功能测试(feature)不存在该问题,只有继承Selenium2TestCase的浏览器测试(browser)有此故障。公共方法setUp、不带test前缀的startSeleniumServer()都被当作测试用例运行;哪怕仅指定运行一个成员方法,PHPUnit也会尝试执行Selenium2TestCase的所有成员。在IntelliJ编辑器中尝试通过cgi执行单个测试时也出现相同现象,最终所有测试都会被运行,需要执行的测试本身可正常通过,核心问题为非test前缀的方法也被当作测试执行。
故障参考截图:
错误执行方法的可视化参考

解决方案

该问题为phpunit/phpunit-selenium 9.x版本与PHPUnit 9.5的方法识别规则不兼容导致,按以下步骤修复即可:

  • 为非测试用例的公共方法添加#[NoTest]注解
    PHPUnit 9.x开始引入注解机制标记非测试方法,所有不需要作为测试执行的公共方法(包括setUp、setUpBeforeClass、tearDown、自定义辅助方法如startSeleniumServer等)都需要在方法前添加该注解:
    use PHPUnit\Framework\Attributes\NoTest;
    
    class NavigationTest extends \PHPUnit\Extensions\Selenium2TestCase
    {
        #[NoTest]
        public function setUp(): void
        {
            parent::setUp();
            // 初始化逻辑
        }
    
        #[NoTest]
        public function startSeleniumServer()
        {
            // 服务启动逻辑
        }
    
        // 测试方法正常保留test前缀即可,不需要加注解
        public function testNavigationHomePage()
        {
            // 测试逻辑
        }
    }
    
    如果需要兼容旧版本写法,可以用/** @noTest */注释代替注解。
  • 调整phpunit.xml配置,显式开启测试方法前缀校验
    在phpunit标签中增加testSuffix="Test.php"和forceTestAnnotation="false"属性,明确要求只识别带test前缀的方法为测试用例:
    <phpunit 
        <!-- 原有其他属性保持不变 -->
        testSuffix="Test.php"
        forceTestAnnotation="false"
    >
    <!-- 剩余配置保持不变 -->
    </phpunit>
    
  • 临时降级兼容方案(不推荐长期使用)
    如果不想修改现有代码,可以将phpunit/phpunit-selenium版本回退到8.x分支的最新兼容版本,保持PHPUnit 9.5的情况下指定安装"phpunit/phpunit-selenium": "8.0.x-dev"即可临时规避该问题。

内容的提问来源于stack exchange,提问作者Richard Tyler Miles

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.05 05:51:03