2017-04-18 38 views
0

我想單個testNG /黃瓜跑步者& single testng @Test允許我將單個功能文件的名稱作爲testng參數傳入(使用@Parameter)並運行它。我想要一個運行時解決方案。TestNG&Cucumber:傳入單個功能文件名作爲參數

我有一堆非Cucumber測試使用已經寫入使用TestNG框架,我想在那裏有黃瓜代碼。

有人想出了一些聰明的東西?

+0

你使用Maven? – Grasshopper

+0

Maven是我們正在尋找的候選人,但我更喜歡不依賴於特定構建系統的解決方案。我一直在尋找Cuccumber Java源代碼,可能會遇到一種方法來實現這一點。使用TestNGCucumberRunner的v1.2.4我可以訪問一個CucumberFeature對象列表。我可以掃描列表並查找與使用testng參數提供的字符串具有相同「路徑」的功能。然後,我可以使用runCucumber方法將匹配功能用於跑步者。至少這是理論:-)。我會嘗試一下。 –

+0

這是在TestNGCucumberRunner中更改getFeatures()的實現的一個選項。有一個簡單的方法。在調用runner類時,可以覆蓋cucumberoptions標記。 https://github.com/cucumber/cucumber-java-skeleton請參閱「覆蓋選項」一節。 Maven提供了一個傳遞這個參數的簡單方法。還應該有一種方法可以在testng.xml中傳遞它。 – Grasshopper

回答

0

另一種方法是使用反射實例化的亞軍之前修改實時CucumberOptions註釋(感謝爲靈感幾個舊帖子):

@Parameters({"featurePath"}) 
@BeforeTest(alwaysRun = true) 
public void setUpTest(@Optional("src/main/java/cucumberFeatureFiles/Testcase.feature") String featurePath) throws Exception { 
    Class<?> testClass = this.getClass(); 
    changeCucumberAnnotation(testClass, "features", new String [] {featurePath});  
    testNGCucumberRunner = new WaltersTestngCucumberRunner(testClass);   
} 

private static void changeCucumberAnnotation(Class<?> clazz, String key, Object newValue) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException{ 
    Annotation options = clazz.getAnnotation(CucumberOptions.class);     //get the CucumberOptions annotation 
    InvocationHandler proxyHandler = Proxy.getInvocationHandler(options);    //setup handler so we can update Annotation using reflection. Basically creates a proxy for the Cucumber Options class 
    Field f = proxyHandler.getClass().getDeclaredField("memberValues");    //the annotaton key/values are stored in the memberValues field 
    f.setAccessible(true);                //suppress any access issues when looking at f 
    Map<String, Object> memberValues = (Map<String, Object>) f.get(proxyHandler);  //get the key-value map for the proxy 
    memberValues.remove(key);               //renove the key entry...don't worry, we'll add it back 
    memberValues.put(key,newValue);             //add the new key-value pair. The annotation is now updated. 
}//end method 
1

想出瞭如何將包含特徵文件名的自定義cucumberoptions發送到跑步者類。這將允許從testng.xml運行黃瓜和非黃瓜測試。如果任何選項會覆蓋已經提供了@CucumberOptions註釋

下面的文字是基於細節「黃瓜的Java」的書......


黃瓜檢查。從頂部進行檢查,以底部之後,停止任何一個被發現後:

  1. 操作系統環境變量CUCUMBER_OPTIONS
  2. Java系統屬性cucumber.options
  3. Java資源包cucumber.properties用黃瓜。 options property

覆蓋中的值將替換除插件參數以外的任何值。插件參數將被追加。未被覆蓋的參數不會受到影響。


testng.xml 

<suite name="Default suite">  
    <test name="Cucumber Mix"> 
     <classes> 
      <class name="cucumtestng.test.RunAbstractSampleTest"></class> 
      <class name="cucumtestng.test.NormalTest"></class> 
     </classes> 
    </test> 
</suite> 


@CucumberOptions(features="",glue="cucumtestng.test.stepdefs",snippets=SnippetType.CAMELCASE, 
plugin={"pretty", "html:report", "json:reports.json"}) 
public class RunAbstractSampleTest extends AbstractTestNGCucumberTests { 

} 


public class NormalTest { 
    @Test 
    public void f() { 
     System.out.println("NORMAL TESTNG CLASS"); 
    } 
} 

您也可以使用不延長AbstractTestNgCucumberTests但可用的組合物TestNG的黃瓜類...

設置在下面和運行Eclipse運行方式配置... enter image description here enter image description here

0

這個「設置」代碼的竅門。它給了我感興趣的黃瓜功能。我也會看看另一個提案。

@Parameters({"featurename"}) 
@BeforeTest(alwaysRun = true) 
public void setUpTest(String featureName) throws Exception { 
    testNGCucumberRunner = new TestNGCucumberRunner(this.getClass()); 
    List<CucumberFeature> featureList = testNGCucumberRunner.getFeatures(); 

    for (CucumberFeature feature : featureList){ 
     if (featureName.equalsIgnoreCase(feature.getPath())){ 
      runFeature = feature; 
      break; 
     } 
    } 
}