Maven执行BUILD SUCCESS但Selenium-Cucumber-JUnit测试未运行求助
Maven执行Selenium-Cucumber-JUnit测试无执行问题排查
问题背景
测试套件在Eclipse中可正常执行,但命令行执行mvn test时,控制台返回BUILD SUCCESS却无任何测试用例被执行。以下是相关配置与执行日志:
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>scmb</groupId> <artifactId>SeleniumCucumberMBDD</artifactId> <version>0.0.1-SNAPSHOT</version> <name>SeleniumCucumberMBDD</name> <url>http://www.example.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> </properties> <dependencies> <!-- 省略其他依赖 --> <dependency> <groupId>io.cucumber</groupId> <artifactId>cucumber-picocontainer</artifactId> <version>4.2.6</version> <scope>test</scope> </dependency> </dependencies> <build> <pluginManagement> <plugins> <!-- 省略其他插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.0.0-M5</version> <configuration> <includes> <include>**/*RunnerTest.java</include> </includes> </configuration> </plugin> </plugins> </pluginManagement> </build> </project>
SuiteRunnerTest.java代码
import org.junit.runner.RunWith; import io.cucumber.junit.Cucumber; import io.cucumber.junit.CucumberOptions; @RunWith(Cucumber.class) @CucumberOptions(features = {"E:/Programming/workspace/SeleniumCucumberMBDD/src/test/resources/com/qa/features/HandlingDemo.feature"}, glue = {"/com/qa/stepDefinitions", "/com/qa/hooks"}, tags = "@ReRun", stepNotifications = true, plugin = {"com.aventstack.extentreports.cucumber.adapter.ExtentCucumberAdapter:"}, monochrome = true, dryRun = false) public class SuiteRunnerTest { }
mvn test执行日志
[INFO] [INFO] ---------------------< scmb:SeleniumCucumberMBDD >---------------------- [INFO] Building SeleniumCucumberMBDD 0.0.1-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- maven-resources-plugin:3.0.2:resources (default-resources) @ SeleniumCucumberMBDD --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] skip non existing resourceDirectory E:\Programming\workspace\SeleniumCucumberMBDD\src\main\resources [INFO] [INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ SeleniumCucumberMBDD --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-resources-plugin:3.0.2:testResources (default-testResources) @ SeleniumCucumberMBDD --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 9 resources [INFO] [INFO] --- maven-compiler-plugin:3.8.0:testCompile (default-testCompile) @ SeleniumCucumberMBDD --- [INFO] No sources to compile [INFO] [INFO] --- maven-surefire-plugin:3.0.0-M5:test (default-test) @ SeleniumCucumberMBDD --- [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 3.660 s [INFO] Finished at: 2023-01-27T21:28:38+05:30 [INFO] ------------------------------------------------------------------------
问题分析
从日志和配置中可定位几个核心问题:
- 测试源码未被编译:日志中
[INFO] No sources to compile说明Maven未找到测试类文件,大概率是测试类未放在Maven默认的src/test/java目录下,或目录结构不符合规范。 - Cucumber配置错误:
glue路径带开头斜杠,会导致Cucumber无法正确扫描步骤定义类;features使用绝对路径,在Maven环境下会依赖本地路径,导致跨环境执行失败。
- 依赖版本不兼容:cucumber-picocontainer版本为4.2.6,与Cucumber主版本7.10.1不匹配,会导致步骤定义无法绑定。
- Surefire插件未生效:插件配置放在
pluginManagement节点下,仅用于版本管理,不会实际执行插件逻辑。
修复方案
1. 规范Maven项目结构
确保测试类(包括SuiteRunnerTest.java)放在src/test/java目录下,严格遵循Maven的源码目录规范:
src/ ├── main/ │ └── java/ └── test/ ├── java/ # 测试类存放目录 └── resources/ # 测试资源(feature文件等)存放目录
2. 修正SuiteRunnerTest配置
import org.junit.runner.RunWith; import io.cucumber.junit.Cucumber; import io.cucumber.junit.CucumberOptions; @RunWith(Cucumber.class) @CucumberOptions( features = {"src/test/resources/com/qa/features/HandlingDemo.feature"}, // 改用相对路径 glue = {"com.qa.stepDefinitions", "com.qa.hooks"}, // 去掉开头斜杠 tags = "@ReRun", stepNotifications = true, plugin = {"com.aventstack.extentreports.cucumber.adapter.ExtentCucumberAdapter:"}, monochrome = true, dryRun = false ) public class SuiteRunnerTest { }
3. 统一Cucumber依赖版本
将cucumber-picocontainer版本升级至与Cucumber主版本一致:
<dependency> <groupId>io.cucumber</groupId> <artifactId>cucumber-picocontainer</artifactId> <version>7.10.1</version> <!-- 与cucumber-java/junit版本一致 --> <scope>test</scope> </dependency>
4. 确保Surefire插件生效
将maven-surefire-plugin从pluginManagement移至build/plugins节点下:
<build> <pluginManagement> <!-- 保留其他插件版本管理配置 --> </pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.0.0-M5</version> <configuration> <includes> <include>**/*RunnerTest.java</include> </includes> </configuration> </plugin> </plugins> </build>
5. 执行清理与测试
命令行执行以下命令,确保清理旧编译文件后重新执行测试:
mvn clean test
内容的提问来源于stack exchange,提问作者Bobby Brahmam
相关产品推荐
相关产品推荐

