Maven无法识别Cucumber 4测试但Cucumber 6运行正常求助
解决Cucumber 4版本下Maven无法检测到测试的问题
我仔细看了你的pom.xml和测试执行日志,核心问题是依赖版本冲突+不一致,导致Cucumber 4没法正确识别测试类。下面一步步给你解决:
1. 清理冲突的Cucumber依赖
你pom里混了好几个不同版本甚至不同groupId的Cucumber依赖:
io.cucumber:cucumber-java:5.1.3(Cucumber 5版,和你要的4版不兼容)info.cukes:cucumber-java:1.2.5(这是非常老的Cucumber版本,早就废弃改用io.cucumber了)io.cucumber:cucumber-junit:4.7.4(你要的Cucumber 4版,但和上面两个版本冲突)
这些杂糅的版本会让Maven加载类时混乱,直接导致测试检测失败。
修复:统一用Cucumber 4.x的全套依赖
把冲突的依赖删掉,只保留统一版本的Cucumber 4依赖,修改后的依赖块如下:
<properties> <cucumber.version>4.7.4</cucumber.version> <junit.version>4.13</junit.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <extent.version>5.0.1</extent.version> </properties> <dependencies> <!-- 统一用Cucumber 4的java和junit依赖 --> <dependency> <groupId>io.cucumber</groupId> <artifactId>cucumber-java</artifactId> <version>${cucumber.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>io.cucumber</groupId> <artifactId>cucumber-junit</artifactId> <version>${cucumber.version}</version> <scope>test</scope> </dependency> <!-- 其他非Cucumber依赖保留,版本没问题可以不动 --> <dependency> <groupId>com.aventstack</groupId> <artifactId>extentreports</artifactId> <version>${extent.version}</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>io.rest-assured</groupId> <artifactId>rest-assured</artifactId> <version>4.3.0</version> <scope>test</scope> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-configuration2</artifactId> <version>2.0</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.6</version> </dependency> <dependency> <groupId>commons-beanutils</groupId> <artifactId>commons-beanutils</artifactId> <version>1.9.3</version> </dependency> </dependencies>
2. 优化Maven Surefire插件配置
你的Surefire插件版本是2.19.1,虽然能用,但升级到2.22.2会更兼容Cucumber 4,同时可以明确指定测试类的路径,确保Maven能找到你的RunCucumberTest:
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <encoding>UTF-8</encoding> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.2</version> <configuration> <!-- 明确指定要运行的Cucumber测试类 --> <includes> <include>**/RunCucumberTest.java</include> </includes> </configuration> </plugin> </plugins> </build>
3. 确认测试运行类的配置正确
检查你的RunCucumberTest.java是不是Cucumber 4的正确写法,示例如下(注意包路径和feature文件路径要和你的项目对应):
package PfgApiAutomation; import org.junit.runner.RunWith; import cucumber.api.CucumberOptions; import cucumber.api.junit.Cucumber; @RunWith(Cucumber.class) @CucumberOptions( features = "src/test/resources/features", // 你的feature文件所在路径 glue = "PfgApiAutomation.steps", // 步骤定义类所在的包 plugin = {"pretty", "html:target/cucumber-reports"} // 可选的报告插件 ) public class RunCucumberTest { }
4. 最后执行清理和测试命令
修改完所有配置后,执行下面的Maven命令,清理旧的构建文件并重新运行测试:
mvn clean test
这样应该就能让Cucumber 4正确检测到你的测试用例并执行了。
内容的提问来源于stack exchange,提问作者Sayen
相关产品推荐
相关产品推荐

