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

同一项目中执行Cucumber标签用例时如何避免触发独立TestNG@Test用例

解决方案

问题根因

默认Maven Surefire插件会扫描src/test/java路径下所有带TestNG @Test注解的类和方法执行,你的Cucumber运行器RunCucumberTest本身就是继承自TestNG的测试类,因此执行mvn test时会同时触发Cucumber用例和所有独立的@Test用例。

可行实现方案

以下方案均符合你的要求,无需使用你提到的四种禁用方案:

方案1:直接修改Jenkins执行命令(零代码改动,最便捷)

执行mvn命令时指定仅运行Cucumber测试运行器类即可,Surefire会跳过其他所有测试类:

mvn clean test -Dtest=RunCucumberTest -Dcucumber.filter.tags="tag_from_feture_file"

方案2:配置Maven Profile适配多执行场景

如果需要同时支持「仅跑Cucumber」「仅跑独立TestNG用例」「跑全部用例」三种场景,可在pom.xml中增加Profile配置:

<profiles>
    <!-- 仅执行Cucumber用例的Profile -->
    <profile>
        <id>cucumber</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>3.2.2</version>
                    <configuration>
                        <includes>
                            <include>RunCucumberTest.java</include>
                        </includes>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
    <!-- 仅执行独立TestNG用例的Profile -->
    <profile>
        <id>standalone-testng</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>3.2.2</version>
                    <configuration>
                        <excludes>
                            <exclude>RunCucumberTest.java</exclude>
                        </excludes>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

配置后Jenkins执行Cucumber用例时添加Profile参数即可:

mvn clean test -P cucumber -Dcucumber.filter.tags="tag_from_feture_file"

需要执行独立TestNG用例时,使用-P standalone-testng参数即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.27 10:15:04