2017-07-14 34 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