我想单个testNG /黄瓜跑步者& single testng @Test允许我将单个功能文件的名称作为testng参数传入(使用@Parameter)并运行它。我想要一个运行时解决方案。TestNG&Cucumber:传入单个功能文件名作为参数
我有一堆非Cucumber测试使用已经写入使用TestNG框架,我想在那里有黄瓜代码。
有人想出了一些聪明的东西?
我想单个testNG /黄瓜跑步者& single testng @Test允许我将单个功能文件的名称作为testng参数传入(使用@Parameter)并运行它。我想要一个运行时解决方案。TestNG&Cucumber:传入单个功能文件名作为参数
我有一堆非Cucumber测试使用已经写入使用TestNG框架,我想在那里有黄瓜代码。
有人想出了一些聪明的东西?
另一种方法是使用反射实例化的亚军之前修改实时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
想出了如何将包含特征文件名的自定义cucumberoptions发送到跑步者类。这将允许从testng.xml运行黄瓜和非黄瓜测试。如果任何选项会覆盖已经提供了@CucumberOptions注释
下面的文字是基于细节“黄瓜的Java”的书......
黄瓜检查。从顶部进行检查,以底部之后,停止任何一个被发现后:
覆盖中的值将替换除插件参数以外的任何值。插件参数将被追加。未被覆盖的参数不会受到影响。
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的黄瓜类...
这个“设置”代码的窍门。它给了我感兴趣的黄瓜功能。我也会看看另一个提案。
@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;
}
}
}
你使用Maven? – Grasshopper
Maven是我们正在寻找的候选人,但我更喜欢不依赖于特定构建系统的解决方案。我一直在寻找Cuccumber Java源代码,可能会遇到一种方法来实现这一点。使用TestNGCucumberRunner的v1.2.4我可以访问一个CucumberFeature对象列表。我可以扫描列表并查找与使用testng参数提供的字符串具有相同“路径”的功能。然后,我可以使用runCucumber方法将匹配功能用于跑步者。至少这是理论:-)。我会尝试一下。 –
这是在TestNGCucumberRunner中更改getFeatures()的实现的一个选项。有一个简单的方法。在调用runner类时,可以覆盖cucumberoptions标记。 https://github.com/cucumber/cucumber-java-skeleton请参阅“覆盖选项”一节。 Maven提供了一个传递这个参数的简单方法。还应该有一种方法可以在testng.xml中传递它。 – Grasshopper