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

TestNG+Cucumber框架下Allure报告出现Cucumber测试重复记录问题

Cucumber测试Allure报告重复问题解决

问题现象

执行本地Allure报告命令allure serve allure-report时,Cucumber测试的报告出现重复:

  • 一份能正确解析allure-cucumber标签并展示对应可视化内容;
  • 另一份基于surefire路径生成,无法正确识别标签与Allure注解信息(如@Step)。

项目场景

项目采用Java+Maven+TestNG+Cucumber技术栈,使用Allure作为报告工具:

  • 通过Maven命令运行Cucumber测试:clean test -Ddriver=chrome -Dmaximize=false -DthreadCount=3 -Dexecutor=jenkins -Dtimeout=10 -PCucumber
  • 项目同时包含TestNG测试用例,其Allure报告记录正常,仅Cucumber测试出现重复报告。

排查思路

推测TestNG/Surefire在索引Cucumber测试用例时,仅捕获了参数、setUp/tearDown及结果,但未识别Allure注解,因此排除类结构问题,锁定pom.xml配置为问题根源。

补充信息

  • PageObject模型仅包含@Step注解;
  • DriverManager类通过ThreadLocal实现驱动的初始化、获取与销毁,无Cucumber或TestNG执行层级注解,相关方法由BaseTest和CucumberSupport类分别调用。

现有pom.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.seleniumTest</groupId>
    <artifactId>SeleniumTest</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>SeleniumTest</name>
    <description>POC for selenium testing project implementation.</description>

    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <driver>chrome</driver>
        <maximize>false</maximize>
        <executor>jenkins</executor>
        <timeout>10</timeout>
        <threadCount>3</threadCount>

        <webdrivermanager-version>5.3.3</webdrivermanager-version>
        <testng-version>7.7.1</testng-version>
        <log4j-core-version>2.20.0</log4j-core-version>
        <selenium-java-version>4.9.1</selenium-java-version>
        <allure-testng-version>2.22.0</allure-testng-version>
        <jul-to-slf4j-version>2.0.7</jul-to-slf4j-version>
        <cucumber-version>7.12.0</cucumber-version>
        <gherkin-version>26.0.1</gherkin-version>
        <allure-cucumber7-jvm-version>2.21.0</allure-cucumber7-jvm-version>
        <maven-compiler-plugin-version>3.11.0</maven-compiler-plugin-version>
        <maven-surefire-plugin-version>3.0.0-M7</maven-compiler-plugin-version>
        <aspectjweaver-version>1.9.8.RC2</aspectjweaver-version>
        <allure-maven-version>2.22.0</allure-maven-version>


    </properties>

    <dependencies>
        <!-- WebDriver automated manager -->
        <dependency>
            <groupId>io.github.bonigarcia</groupId>
            <artifactId>webdrivermanager</artifactId>
            <version>${webdrivermanager-version}</version>
        </dependency>

        <!-- Testing framework -->
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>${testng-version}</version>
        </dependency>

        <!-- Logger dependency -->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>${log4j-core-version}</version>
        </dependency>

        <!-- Selenium framework for web testing -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>${selenium-java-version}</version>
        </dependency>

        <!-- Reporting framework -->
        <dependency>
            <groupId>io.qameta.allure</groupId>
            <artifactId>allure-testng</artifactId>
            <version>${allure-testng-version}</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jul-to-slf4j</artifactId>
            <version>${jul-to-slf4j-version}</version>
        </dependency>

        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-java</artifactId>
            <version>${cucumber-version}</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-testng</artifactId>
            <version>${cucumber-version}</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>gherkin</artifactId>
            <version>${gherkin-version}</version>
            <scope>test</scope>
        </dependency>
        
        <dependency>
            <groupId>io.qameta.allure</groupId>
            <artifactId>allure-cucumber7-jvm</artifactId>
            <version>${allure-cucumber7-jvm-version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <profiles>
        <profile>
            <id>TestNG</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <version>${maven-surefire-plugin-version}</version>
                        <configuration>
                            <suiteXmlFiles>
                                <suiteXmlFile>config/testng.xml</suiteXmlFile>
                            </suiteXmlFiles>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
        <profile>
            <id>Cucumber</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <version>${maven-surefire-plugin-version}</version>
                        <configuration>
                            <includes>
                                <include>**/*CucumberRunnerTests.java</include>
                            </includes>
                            <properties>
                                <property>
                                    <name>dataproviderthreadcount</name>
                                    <value>${threadCount}</value>
                                </property>
                            </properties>
                            <argLine>
                                -javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/${aspectjweaver-version}/aspectjweaver-${aspectjweaver-version}.jar"
                            </argLine>
                        </configuration>
                        <dependencies>
                            <dependency>
                                <groupId>org.aspectj</groupId>
                                <artifactId>aspectjweaver</artifactId>
                                <version>${aspectjweaver-version}</version>
                            </dependency>
                        </dependencies>
                    </plugin>
                </plugins>
            </build>
        </profile>
        <profile>
            <id>TestNG+Cucumber</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <version>${maven-surefire-plugin-version}</version>
                        <configuration>
                            <properties>
                                <property>
                                    <name>dataproviderthreadcount</name>
                                    <value>${threadCount}</value>
                                </property>
                            </properties>
                            <suiteXmlFiles>
                                <suiteXmlFile>config/cucumber.xml</suiteXmlFile>
                                <suiteXmlFile>config/testng.xml</suiteXmlFile>
                            </suiteXmlFiles>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>

    <reporting>
        <plugins>
            <plugin>
                <groupId>io.qameta.allure</groupId>
                <artifactId>allure-maven</artifactId>
                <version>${allure-maven-version}</version>
                <configuration>
                    <propertiesFilePath>test/resources/allure.properties</propertiesFilePath>
                </configuration>
            </plugin>
        </plugins>
    </reporting>

</project>

问题根源

项目同时引入了allure-testng和allure-cucumber7-jvm依赖,当运行Cucumber测试时,Surefire插件会同时触发TestNG的Allure监听器和Cucumber的Allure监听器:

  • Cucumber的Allure监听器生成正确解析标签和注解的报告;
  • TestNG的Allure监听器仅捕获Cucumber测试的执行框架层面信息(如参数、生命周期方法),无法识别Cucumber专属的Allure注解,从而生成一份无效的重复报告。

解决方案

方案1:在Cucumber Runner类中指定专属Allure插件

在*CucumberRunnerTests.java中,通过@CucumberOptions明确指定Cucumber的Allure插件,确保仅由Cucumber监听器处理报告生成:

import io.cucumber.testng.AbstractTestNGCucumberTests;
import io.cucumber.testng.CucumberOptions;

@CucumberOptions(
    features = "src/test/resources/features",
    glue = "com.yourpackage.stepdefinitions",
    plugin = {"io.qameta.allure.cucumber7jvm.AllureCucumber7Jvm"}
)
public class CucumberRunnerTests extends AbstractTestNGCucumberTests {
}

方案2:调整Maven Profile配置,隔离TestNG与Cucumber的Allure依赖

修改pom.xml的Cucumber profile,排除allure-testng依赖,避免TestNG监听器干扰:

<profile>
    <id>Cucumber</id>
    <dependencies>
        <dependency>
            <groupId>io.qameta.allure</groupId>
            <artifactId>allure-testng</artifactId>
            <version>${allure-testng-version}</version>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>*</groupId>
                    <artifactId>*</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>
    <build>
        <!-- 原Surefire配置保持不变 -->
    </build>
</profile>

方案3:在Surefire配置中禁用TestNG监听器

在Cucumber profile的Surefire插件配置中,添加参数清空TestNG监听器列表,仅保留Cucumber相关逻辑:

<profile>
    <id>Cucumber</id>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>${maven-surefire-plugin-version}</version>
                <configuration>
                    <includes>
                        <include>**/*CucumberRunnerTests.java</include>
                    </includes>
                    <properties>
                        <property>
                            <name>dataproviderthreadcount</name>
                            <value>${threadCount}</value>
                        </property>
                        <!-- 禁用TestNG的Allure监听器 -->
                        <property>
                            <name>listener</name>
                            <value></value>
                        </property>
                    </properties>
                    <argLine>
                        -javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/${aspectjweaver-version}/aspectjweaver-${aspectjweaver-version}.jar"
                    </argLine>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.aspectj</groupId>
                        <artifactId>aspectjweaver</artifactId>
                        <version>${aspectjweaver-version}</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>
</profile>

验证方法

  1. 执行Maven命令重新运行测试:clean test -PCucumber
  2. 生成Allure报告:allure serve allure-report
  3. 检查报告中是否仅存在一份正确解析的Cucumber测试报告。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.20 14:44:59