Maven构建未生成Cucumber报告:TestRunner未被识别问题求助
Maven构建时未生成Cucumber报告,手动运行TestRunner正常
手动运行TestRunner.java时可正常生成Cucumber报告,但执行mvn clean -U install verify命令进行Maven构建时,target目录下未生成Cucumber报告。相关配置及代码如下:
Pom.xml
<plugin> <groupId>net.masterthought</groupId> <artifactId>maven-cucumber-reporting</artifactId> <version>5.5.0</version> <executions> <execution> <id>execution</id> <phase>verify</phase> <goals> <goal>generate</goal> </goals> <configuration> <projectName>CustomeProjectNAme</projectName> <outputDirectory>${project.build.directory}/cucumber-report-html</outputDirectory> <inputDirectory>${project.build.directory}</inputDirectory> <!-- this is where the cucumber.json files is saved --> <jsonFiles> <param>cucumber.json</param> <!-- the name/regex of the cucumber report json file --> </jsonFiles> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-failsafe-plugin</artifactId> <version>3.0.0-M8</version> <executions> <execution> <goals> <goal>integration-test</goal> <goal>verify</goal> </goals> <configuration> <includes> <include>**/*TestRunner.java</include> </includes> </configuration> </execution> </executions> </plugin> </plugins> </build>
TestRunner类
import io.cucumber.junit.Cucumber; import io.cucumber.junit.CucumberOptions; import org.junit.runner.RunWith; @RunWith(Cucumber.class) @CucumberOptions( features = "src/integration-test/resources/features", glue = {"test.test.test"}, plugin = {"pretty", "json:target/cucumber-reports/cucumber.json"}, monochrome = true ) public class TestRunner { static { System.out.println("Executing TestRunner++++++++++****************"); } }
问题原因分析
- JSON文件路径不匹配:TestRunner中配置的JSON报告输出路径是
target/cucumber-reports/cucumber.json,但maven-cucumber-reporting插件的inputDirectory指向target目录,并且指定查找cucumber.json文件,插件无法在target根目录找到该文件,因此无法生成HTML报告。 - Failsafe插件可能未执行TestRunner:如果TestRunner不在
src/integration-test/java目录下,或者includes规则未匹配到TestRunner,会导致测试未执行,没有JSON文件生成,自然无法生成报告。 - 构建阶段冗余:
install阶段会执行打包和安装到本地仓库,但verify阶段已经包含integration-test和报告生成步骤,多余的阶段可能不会影响,但需确保流程顺序正确。
解决思路
1. 修正JSON文件路径匹配
两种方案二选一:
- 修改TestRunner配置:将JSON输出路径改为
target/cucumber.json,对应@CucumberOptions的plugin参数:plugin = {"pretty", "json:target/cucumber.json"} - 修改Maven插件配置:将maven-cucumber-reporting的
inputDirectory改为${project.build.directory}/cucumber-reports:<inputDirectory>${project.build.directory}/cucumber-reports</inputDirectory>
2. 确认Failsafe插件执行状态
- 查看Maven构建日志,检查是否有
Executing TestRunner++++++++++****************的输出,如果没有,说明TestRunner未被执行:- 若TestRunner位于
src/test/java目录,改用maven-surefire-plugin执行单元测试; - 若TestRunner位于
src/integration-test/java,确保Maven识别该目录(可通过添加build-helper-maven-plugin配置源目录),或调整includes规则为src/integration-test/java/**/*TestRunner.java。
- 若TestRunner位于
3. 简化构建命令
执行mvn clean verify即可,无需添加install和-U(除非需要强制更新依赖),减少不必要的步骤干扰。
内容的提问来源于stack exchange,提问作者Shubha Rao
相关产品推荐
相关产品推荐

