使用Playwright+Java时Runner类无法识别测试的问题排查
解决JUnit5 Cucumber Runner无法找到测试的问题
核心问题排查与修复步骤
1. 移除冲突依赖并统一JUnit版本
你的依赖中同时包含了cucumber-junit(JUnit 4适配)和cucumber-junit-platform-engine(JUnit 5适配),两者会产生兼容性冲突。另外JUnit Platform组件版本不一致(junit-platform-commons为1.5.2,其他JUnit组件为1.10.0),会导致测试引擎无法正常加载。
修改后的依赖配置:
<dependencies> <!-- Playwright Dependency --> <dependency> <groupId>com.microsoft.playwright</groupId> <artifactId>playwright</artifactId> <version>1.49.0</version> </dependency> <dependency> <groupId>io.cucumber</groupId> <artifactId>cucumber-java</artifactId> <version>7.20.1</version> </dependency> <dependency> <groupId>io.cucumber</groupId> <artifactId>cucumber-junit-platform-engine</artifactId> <version>7.20.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>3.0.0-beta3</version> </dependency> <!-- 统一JUnit Platform版本,与JUnit Jupiter 5.10.0配套 --> <dependency> <groupId>org.junit.platform</groupId> <artifactId>junit-platform-commons</artifactId> <version>1.10.0</version> <scope>test</scope> </dependency> <!-- JUnit 5 Platform Suite --> <dependency> <groupId>org.junit.platform</groupId> <artifactId>junit-platform-suite-api</artifactId> <version>1.10.0</version> <scope>test</scope> </dependency> <!-- JUnit 5 Jupiter API --> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>5.10.0</version> <scope>test</scope> </dependency> <!-- JUnit 5 Engine --> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>5.10.0</version> <scope>test</scope> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.15.0</version> </dependency> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <version>42.7.4</version> </dependency> <!-- 移除cucumber-junit依赖 --> </dependencies>
2. 添加Maven Surefire插件配置
确保Maven的Surefire插件支持JUnit 5,在pom.xml的<build>节点下添加:
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.2.5</version> <configuration> <includes> <include>**/JUnit5CucumberTest.java</include> </includes> </configuration> </plugin> </plugins> </build>
3. 验证Runner配置细节
你的Runner类配置基本正确,可做以下确认:
@SelectClasspathResource("features"):对应src/test/resources/features目录,路径无误。@ConfigurationParameter(key = GLUE_PROPERTY_NAME, value = "com.steps"):步骤定义类GoogleSearchTest位于com.steps包下,配置正确。@ConfigurationParameter(key = FILTER_TAGS_PROPERTY_NAME, value = "@smoke"):Feature文件中存在@smoke标签,匹配正常。
4. 清理并重新构建项目
执行以下Maven命令清理缓存并重新构建:
mvn clean install -DskipTests
完成以上步骤后,再次运行JUnit5CucumberTest类,即可正常找到并执行测试。
内容的提问来源于stack exchange,提问作者Jade Rock
相关产品推荐
相关产品推荐

