TestNG&黄瓜:将单个要素文件名作为参数

时间:2017-04-18 19:04:57

标签: java cucumber testng

我想要一个单一的测试NG / Cucumber跑步者&单个testng @Test允许我将单个特征文件的名称作为testng参数传递(使用@Parameter)并运行它。我想要一个运行时解决方案。

我已经使用TestNG框架编写了一堆非Cucumber测试,我也想在那里使用Cucumber代码。

有没有人想出一些聪明的东西?

3 个答案:

答案 0 :(得分:1)

了解如何将包含特征文件名的自定义cucumberoptions发送到runner类。这将允许从 testng.xml 运行黄瓜和非黄瓜测试。

下面的文字基于" Cucumber for Java"书...

Cucumber检查是否为@CucumberOptions注释提供了任何选项覆盖。从上到下检查以下内容,在找到任何一个后停止:

  1. 操作系统环境变量CUCUMBER_OPTIONS
  2. Java系统属性cucumber.options
  3. 带有cucumber.options属性的Java资源包cucumber.properties
  4. 在override中找到的值将替换除插件参数之外的任何设置值。插件参数将被追加。未被覆盖的参数不会受到影响。

    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中设置Run as Configuration,如下所示并运行... enter image description here enter image description here

答案 1 :(得分:1)

这个“设置”代码可以解决问题。它给了我运行感兴趣的黄瓜功能。我也会看另一个提案。

@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;
        }
    }
}

答案 2 :(得分: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