JUnit5+Cucumber项目执行mvn test时测试用例被跳过的问题排查
我在IntelliJ IDE中开发一个基于JUnit5的简单Cucumber测试程序,已按照cucumber-jvm官方文档配置依赖。但执行mvn clean test时,Cucumber测试用例被跳过,控制台输出如下:
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ xxx ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:3.0.0-M5:test (default-test) @ xxx ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.052 s
[INFO] Finished at: 2022-10-28T18:58:48-04:00
[INFO] ------------------------------------------------------------------------
但直接点击右上角运行按钮执行Runner类时,测试可正常执行。我的Runner类代码如下:
package pirate; import org.junit.platform.suite.api.ConfigurationParameter; import org.junit.platform.suite.api.IncludeEngines; import org.junit.platform.suite.api.SelectClasspathResource; import org.junit.platform.suite.api.Suite; import static io.cucumber.junit.platform.engine.Constants.GLUE_PROPERTY_NAME; import static io.cucumber.junit.platform.engine.Constants.PLUGIN_PROPERTY_NAME; @Suite @IncludeEngines("cucumber") @SelectClasspathResource("/pirate") @ConfigurationParameter(key = GLUE_PROPERTY_NAME, value = "pirate") @ConfigurationParameter(key = PLUGIN_PROPERTY_NAME, value = "pretty, html:target/cucumber-report/cucumber.html") public class Runner { }
pom.xml配置如下:
<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.example</groupId> <artifactId>xxx</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <name>xxx</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <build> <testSourceDirectory>src/test/java</testSourceDirectory> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <source>11</source> <target>11</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.0.0-M5</version> <configuration> <properties> <configurationParameters> cucumber.junit-platform.naming-strategy=long </configurationParameters> </properties> </configuration> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter</artifactId> <version>5.9.0</version> </dependency> <dependency> <groupId>io.cucumber</groupId> <artifactId>cucumber-java</artifactId> <version>7.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>io.cucumber</groupId> <artifactId>cucumber-junit-platform-engine</artifactId> <version>7.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.platform</groupId> <artifactId>junit-platform-suite</artifactId> <version>1.9.0</version> <scope>test</scope> </dependency> </dependencies> </project>
项目测试结构如下:
我希望通过mvn test或类似命令执行所有Cucumber测试,请问我遗漏了什么配置?
问题核心是Maven Surefire插件未识别到JUnit Platform Suite的Runner类,默认情况下Surefire只会扫描名称以*Test、*Tests或TestCase结尾的测试类,你的Runner类不符合这个命名规则,因此被跳过。
可以通过以下两种方式解决:
方式一:修改Runner类名称
将Runner改为符合Surefire命名规则的名称,比如CucumberTestRunner或PirateTestSuite,插件会自动识别并执行该类。
方式二:配置Surefire插件指定测试类
在maven-surefire-plugin的配置中添加includes标签,明确指定要执行的Runner类:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.0.0-M5</version> <configuration> <includes> <include>**/Runner.java</include> </includes> <properties> <configurationParameters> cucumber.junit-platform.naming-strategy=long </configurationParameters> </properties> </configuration> </plugin>
另外,建议给junit-jupiter依赖添加test scope,避免潜在的依赖冲突:
<dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter</artifactId> <version>5.9.0</version> <scope>test</scope> </dependency>
完成修改后重新执行mvn clean test,Cucumber测试即可正常执行。
内容的提问来源于stack exchange,提问作者zxcisnoias

