2017-07-14 79 views
3

是否有可能从java .properties文件中获取黄瓜选项值?爪哇黄瓜:从外部来源@CucumberOptions就像属性文件

this SO后,它显示它是从CLI传递的。

这里是我的示例类:

@RunWith(Cucumber.class) 
@CucumberOptions(
     features = {"resources/features/"}, 
     glue = {"classpath:com/"}, 
     tags = {"@foo, @bar"} 
) 
public class UITestRunner { 

} 

而是这里硬编码的标签,我想采取从属性文件。 任何帮助表示赞赏!

+1

此功能不黄瓜实现。 – fg78nc

+0

谢谢!如果您能够链接到支持此信息的文档,请多加谅解。 – iamkenos

+0

我想我错了,请检查我的答案。我误解了你的问题,认为你问的是否可以提供属性文件而不是功能文件。对不起,凌晨2点:)。请看我的答案,它不适合评论。 – fg78nc

回答

3

黄瓜最初将寻求通过cucumber.api.cli.Main@CucumberOptions

提供的参数但是你可以重写它们提供(在这个特定的顺序):

  1. 操作系统环境变量CUCUMBER_OPTIONS
  2. Java系统属性cucumber.options
  3. Java资源包cucumber.propertiescucumber.options属性

一旦找到上述选项之一,就会使用它。在名为cucumber.optionsCUCUMBER_OPTIONS的变量(或属性)中提供覆盖。所有的值,除了插件参数将覆盖由cucumber.api.cli.Main@CucumberOptions提供的值。插件选项将添加到由cucumber.api.cli.Main@CucumberOptions指定的插件中。

+0

哪里有这个记录:)你有链接吗? –

+1

你可以在Cucumber Java Book中找到。我相信官方文件做得不好(不幸的是)。 – fg78nc

+0

哇。经过很长一段时间的努力,我设法做到了我需要的东西。这些信息确实帮了很大忙。接受这个答案:) – iamkenos

1

希望你知道,如果在命令行中运行,您可以使用系统属性

mvn test -Dcucumber.options="--features resources/features/ --tags [email protected]" -Dtest=AnimalsTest 

,这意味着你可以通过编程设置这些属性:

@RunWith(Cucumber.class) 
public class CatsRunner { 

    @BeforeClass 
    public static void before() { 
     System.setProperty("cucumber.options", "--features resources/features/ --tags [email protected]"); 
    } 

} 

希望给你一些想法。例如,您可以手动读取文件中的属性,然后实现您想要的。

编辑:显然上述不起作用。所以这是我的下一个想法,通过扩展Cucumber类来实现你自己的JUnit黄瓜跑步者。一个例子参考this。所以在构造函数中,你应该完全控制。

+1

我会放弃这一点。在此之前,请给我一些时间来接受这是一个正确的答案。谢谢您的帮助! PS - 我在src/main /中有我的测试,并且我将它构建到一个可执行的jar中,所以mvn测试不会真的适合我。无论如何,这种见解是有帮助的! – iamkenos

+1

@iamkenos不用担心:)我很肯定这是可行的。在我的开放源代码项目[https://github.com/intuit/karate]中,我已经能够将Cucumber转化为它不适合做的各种事情。在黄瓜运行时被初始化之前设置一个系统属性是个诀窍。如果您遇到困难,请告诉我。 –

+0

对不起,这个解决方案不起作用。不能接受这个答案。当我尝试运行我的跑步者类时,它甚至没有进入@BeforeClass方法。因此,我已经在'classpath:....'中找到了没有找到的功能。 – iamkenos

0

我做这样的: -

cucmberOption.properties

#cucumber.options=--plugin html:output/cucumber-html-report 
#src/test/resources 
cucumber.options.feature =src/test/resources 
cucumber.options.report.html=--plugin html:output/cucumber-html-report 
  • 的Java Class:CreateCucumberOptions。java的

    方法加载属性文件: -

    private static void loadPropertiesFile(){ 
        InputStream input = null; 
        try{ 
         String filename = "cucumberOptions.properties"; 
         input = CreateCucumberOptions.class.getClassLoader().getResourceAsStream(filename); 
         if(input==null){ 
          LOGGER.error("Sorry, unable to find " + filename); 
          return; 
         } 
         prop.load(input); 
        }catch(IOException e){ 
         e.printStackTrace(); 
        }finally{ 
         if(input!=null) { 
          try { 
           input.close(); 
          } catch (IOException e) { 
           e.printStackTrace(); 
          } 
         } 
        } 
    } 
    
  • 方法来获取和设置CucumberOptions

    private String createAndGetCucumberOption(){  
        StringBuilder sb = new StringBuilder(); 
        String featureFilesPath = 
        prop.getProperty("cucumber.options.feature"); 
        LOGGER.info(" featureFilesPath: " +featureFilesPath); 
        String htmlOutputReport = 
         prop.getProperty("cucumber.options.report.html"); 
        LOGGER.info(" htmlOutputReport: " +htmlOutputReport); 
        sb.append(htmlOutputReport); 
        sb.append(" "); 
        sb.append(featureFilesPath); 
        return sb.toString(); 
        } 
    
    
    private void setOptions(){ 
        String value = createAndGetCucumberOption(); 
        LOGGER.info(" Value: " +value); 
        System.setProperty(KEY, value); 
        } 
    
  • 并运行此主要方法: -

    public static void main(String[] args) { 
         CreateCucumberOptions cucumberOptions = new CreateCucumberOptions(); 
         JUnitCore junitRunner = new JUnitCore(); 
         loadPropertiesFile(); 
         cucumberOptions.setOptions(); 
         junitRunner.run(cucumberTest.runners.RunGwMLCompareTests.class); 
        } 
    

    和RunGwMLCompare Tests.class是我的黄瓜类

    @RunWith(Cucumber.class) 
    @CucumberOptions(
         monochrome = true, 
         tags = {"@passed"}, 
         glue = "cucumberTest.steps") 
    public class RunGwMLCompareTests { 
    
        public RunGwMLCompareTests(){ 
    
        } 
    } 
    

    所以基本上nopw你打通属性文件和其他选项,如胶水definations Java类设置输出报告和文件夹功能。运行测试用例只需运行你的主类。

    问候, 维克拉姆Pathania