在《Cucumber Parallel Run》文章中介绍了 cucumber-jvm-parallel-plugin 配置,为每个 feature file 生成一个 Cucumer Runner Class,以此来实现并行跑 cases。
问题
按照默认的配置,会有个小问题,那就是 log 的路径比较长,后续要 parse log 信息时,加上绝对路径往往超过 256 个字符,导致没法取到 log 文件。下面截图中为 jenkins 上备份的 log:
绝对路径就是:
-opt-jenkins-workspace-dev-…/target/rest-logs/-opt-jenkins-workspace-dev-…-search-generic-search-verify-…/validate-whether-search-…-14
导致原因
就是在生成的 cucumber runner class 中,features 的配置是绝对路径。本地 run 的情况如下:
import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
@RunWith(Cucumber.class)
@CucumberOptions(
strict = true,
features = {"C:/xxx/Automation/infra-shared-services-tests/src/test/resources/features/search/generic/Search-Verify-xxxx.feature"},
plugin = {"json:C:/xxx/Automation/infra-shared-services-tests/target/cucumber-parallel/json/1.json", "html:C:/xxx/Automation/infra-shared-services-tests/target/cucumber-parallel/html/1.html", "rerun:C:/xxx/Automation/infra-shared-services-tests/target/rerun/1.txt"},
monochrome = false,
tags = {"@xxx"},
glue = {"com.xxx.infra.services.stepdefs"})
public class SearchVerifyAllHlcts01IT {
}
解决办法:
将 feature 的配置设置成相对路径。cucumber-jvm-parallel-plugin 支持自定义模板。
Specify a custom template for the generated sources (this is a path relative to the project base directory) <customVmTemplate>src/test/resources/cucumber-custom-runner.vm</customVmTemplate>
经过几番摸索,生成一个 template 文件,可以参考 Custom Templates 中的实例:
有关 vm,可以参考 velocity(vm)模板引擎相关资料。
#parse("/array.java.vm")
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
import org.junit.runner.RunWith;
@RunWith(Cucumber.class)
@CucumberOptions(
features = {"$featureFile.substring($featureFile.indexOf("src"))"},
plugin = #stringArray($plugins),
#if(!$featureFile.contains(".feature:") && $tags)
tags = #stringArray($tags),
#end
glue = #stringArray($glue))
public class $className {
}
在 POM 中配置 cucumber-jvm-parallel-plugin:
<customVmTemplate>src/test/java/parallel/CucumberJVMParallelTemplate.vm</customVmTemplate>
这样再看一下生成cucumber runner class:features的配置就成相对路径了。
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
import org.junit.runner.RunWith;
@RunWith(Cucumber.class)
@CucumberOptions(
features = {"src/test/resources/features/search/generic/Search-Verify-xxx.feature"},
plugin = {"json:C:/xxx/Automation/infra-shared-services-tests/target/cucumber-parallel/json/1.json", "html:C:/xxx/Automation/infra-shared-services-tests/target/cucumber-parallel/html/1.html", "rerun:C:/xxx/Automation/infra-shared-services-tests/target/rerun/1.txt"},
tags = {"@xxx"},
glue = {"com.xxx.infra.services.stepdefs"})
public class SearchVerifyAllHlcts01IT {
}
最终生成的log的路径也就变短了:
绝对路径,文件路径就不会超 256 了:
-opt-jenkins-workspace-dev-xxx/target/rest-logs/search-generic-search-verify-xxx/validate-whether-search-xxx-14