Custom Template for cucumber-jvm-parallel-plugin

《Cucumber Parallel Run》文章中介绍了cucumber-jvm-parallel-plugin配置,为每个feature file生成一个Cucumer Runner Class,以此来实现并行跑cases。

问题

按照默认的配置,会有个小问题,那就是log的路径比较长,后续要parse log信息时,加上绝对路径往往超过256个字符,导致没法取到log文件。下面截图中为jenkins上备份的log:
在这里插入图片描述
绝对路径就是:
-opt-jenkins-workspace-dev-la-app-apac-la-apac-infra-cert7-smoke/target/rest-logs/-opt-jenkins-workspace-dev-la-app-apac-la-apac-infra-cert7-smoke-search-generic-search-verify-all-hlcts/validate-whether-search-service-returns-list-of-all-valid-hlcts-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:/ForKelly/Automation/infra-shared-services-tests/src/test/resources/features/search/generic/Search-Verify-All-HLCTs.feature"},
        plugin = {
    
    "json:C:/ForKelly/Automation/infra-shared-services-tests/target/cucumber-parallel/json/1.json", "html:C:/ForKelly/Automation/infra-shared-services-tests/target/cucumber-parallel/html/1.html", "rerun:C:/ForKelly/Automation/infra-shared-services-tests/target/rerun/1.txt"},
        monochrome = false,
        tags = {
    
    "@kelly"},
        glue = {
    
    "com.lexisnexis.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-All-HLCTs.feature"},
       plugin = {
    
    "json:C:/ForKelly/Automation/infra-shared-services-tests/target/cucumber-parallel/json/1.json", "html:C:/ForKelly/Automation/infra-shared-services-tests/target/cucumber-parallel/html/1.html", "rerun:C:/ForKelly/Automation/infra-shared-services-tests/target/rerun/1.txt"},
       tags = {
    
    "@kelly"},
       glue = {
    
    "com.lexisnexis.infra.services.stepdefs"})
       public class SearchVerifyAllHlcts01IT {
    
    
}

最终生成的log的路径也就变短了:
在这里插入图片描述
绝对路径,文件路径就不会超256了:
-opt-jenkins-workspace-dev-la-app-apac-la-apac-infra-cert7-smoke/target/rest-logs/search-generic-search-verify-all-hlcts/validate-whether-search-service-returns-list-of-all-valid-hlcts-14

猜你喜欢

转载自blog.csdn.net/wumingxiaoyao/article/details/111835406