Cucumber Java项目build.gradle从javaexec改project.test遇测试未执行问题
问题:Cucumber Java测试框架重试功能实现受阻
我想给Cucumber Java测试框架添加重试功能,于是把build.gradle中原有的javaexec测试执行结构改成了project.test方式,但运行Gradle任务时完全没有执行测试步骤——构建显示成功,却找不到任何测试。排查日志、添加调试语句也没定位到问题,另外JSON报告输出暂时也无法配置(这个优先级较低)。以下是原有代码和尝试实现的代码,求相关建议或示例:
原有测试执行代码
task smoke() { dependsOn assemble, compileTestJava doLast { runTests('smoke') } } task regression() { dependsOn assemble, compileTestJava doLast { runTests('regression') } } task newWork() { dependsOn assemble, compileTestJava doLast { runTests('newWork') } } private ExecResult runTests(String testType) { javaexec { mainClass = "io.cucumber.core.cli.Main" classpath = configurations.cucumberRuntime + sourceSets.main.output + sourceSets.test.output args = ['--plugin', 'pretty', '--plugin', 'json:output/reports/report.json', '--plugin', 'html:output/reports/html', '--tags', ('@' + testType), '--glue', 'stepDefinitions', 'src/test/resources/features', '--tags', 'not @disabled'] } }
尝试实现的代码
task smoke() { dependsOn assemble, compileTestJava doLast { runTests('smoke') } } task regression() { dependsOn assemble, compileTestJava doLast { runTests('regression') } } task newWork() { dependsOn assemble, compileTestJava doLast { runTests('newWork') } } private void runTests(String testType) { project.test { workingDir = file('src/test/resources') systemProperty 'cucumber.options', "--tags @${testType} --glue stepDefinitions:src/test/java/stepDefinitions" retry { maxRetries = 3 maxFailures = 20 failOnPassedAfterRetry = false } reports { reports.html.required = true reports.html.destination = file('alice/JavelinTAF/output/reports/html') } } }
解决方案建议
核心问题分析
你当前写法的根本问题是:在doLast(执行阶段)里配置project.test任务,而Gradle的测试任务需要在配置阶段完成配置。当执行阶段到来时,Gradle已经完成了任务图的构建,此时再修改test任务的配置不会生效,而且你也没有触发这个test任务的执行,所以自然不会有测试被运行。
正确实现方式示例
直接基于Gradle的Test任务类型创建自定义测试任务,复用其内置的重试、报告功能,同时适配Cucumber的执行逻辑:
// 自定义Cucumber测试任务类型,继承Gradle内置Test任务 abstract class CucumberTest extends Test { // 定义测试标签属性,用于区分不同测试套件 String testTag @Override void executeTests() { // 设置Cucumber执行参数 systemProperty 'cucumber.options', """ --tags @${testTag} --tags not @disabled --glue stepDefinitions src/test/resources/features --plugin json:output/reports/${testTag}.json --plugin html:output/reports/html/${testTag} """.trim().replace('\n', ' ') // 执行父类的测试逻辑 super.executeTests() } } // 创建smoke测试任务 task smoke(type: CucumberTest) { dependsOn assemble, compileTestJava testTag = 'smoke' // 配置重试规则 retry { maxRetries = 3 maxFailures = 20 failOnPassedAfterRetry = false } // 配置报告 reports { html.required = true html.destination = file("output/reports/html/${testTag}") } } // 创建regression测试任务 task regression(type: CucumberTest) { dependsOn assemble, compileTestJava testTag = 'regression' retry { maxRetries = 3 maxFailures = 20 failOnPassedAfterRetry = false } reports { html.required = true html.destination = file("output/reports/html/${testTag}") } } // 创建newWork测试任务 task newWork(type: CucumberTest) { dependsOn assemble, compileTestJava testTag = 'newWork' retry { maxRetries = 3 maxFailures = 20 failOnPassedAfterRetry = false } reports { html.required = true html.destination = file("output/reports/html/${testTag}") } } // 配置Cucumber依赖(确保版本和你的项目匹配) dependencies { testImplementation 'io.cucumber:cucumber-java:7.14.0' testImplementation 'io.cucumber:cucumber-junit-platform-engine:7.14.0' testImplementation 'org.junit.platform:junit-platform-launcher:1.10.0' } // 全局测试任务配置,启用JUnit平台(Cucumber需要依赖它) test { useJUnitPlatform() }
关键注意点
- 依赖配置:必须引入
cucumber-junit-platform-engine,因为Gradle的Test任务默认使用JUnit平台执行测试,这个依赖是Cucumber和JUnit平台的桥梁。 - Glue路径写法:
--glue后面应该是步骤定义的包名(比如stepDefinitions),而不是文件路径,Cucumber会自动扫描该包下的所有步骤定义类。 - 报告配置:可以通过
cucumber.options的--plugin参数同时配置JSON和HTML报告,每个测试套件生成独立的报告文件,避免覆盖。 - 重试功能:Gradle内置的
retry配置只对失败的测试用例生效,符合你的需求。
内容的提问来源于stack exchange,提问作者Chris Iserman
相关产品推荐
相关产品推荐

