目前我们正在从testng.xml触发我们的冒烟测试,我们有两个不同的sceanrios需要验证。
我们的要求是,如果一个场景失败(@ Test1),其他场景不应该执行(@ Test2)。如何在QAF,Testng - Cucumber设置中实现这一目标?
<groups>
<run>
<include name="@Test1" />
<include name="@Test2" />
</run>
</groups>
<classes>
<class
name="com.qmetry.qaf.automation.step.client.gherkin.GherkinScenarioFactory" />
</classes>
</test>
其中一种方法是实现方法调用监听器。在after方法中,您可以设置一个标志,在方法之前,您可以根据标志的值检查标志和跳过测试。例如:
package com.qmetry.qaf.example.test;
...
public class StopRunListener implements IInvokedMethodListener {
private static boolean hasFailure=false;
@Override
public void beforeInvocation(IInvokedMethod method, ITestResult testResult) {
String[] groups = testResult.getMethod().getGroups();
if(hasFailure && Arrays.asList(groups).contains("Test2")) {
throw new SkipException("Stop execution due to failure");
}
}
@Override
public void afterInvocation(IInvokedMethod method, ITestResult testResult) {
String[] groups = testResult.getMethod().getGroups();
if(!testResult.isSuccess() && Arrays.asList(groups).contains("Test1")) {
hasFailure=true;
}
}
}
在XML配置文件中添加侦听器
<listeners>
<listener class-name="com.qmetry.qaf.example.test.StopRunListener" />
</listeners>
<groups>
<run>
<include name="@Test1" />
<include name="@Test2" />
</run>
</groups>
<classes>
<class
name="com.qmetry.qaf.automation.step.client.gherkin.GherkinScenarioFactory" />
</classes>
使用Gherkin语法,您无法指定依赖关系或其他元数据。您可以使用qaf-2.1.14提供的BDD2 syntax,并在test1
上设置组test2
的依赖关系。它将确保组test2
的测试在组test1
之后执行。但如果依赖组中的一个测试失败,它将不会跳过测试。您可以使用上面示例中提供的侦听器来实现。
例如:
#meta-data on feature will be assigned to all scenario in feature file
@Test1
Feature: A feature is a collection of scenarios
@Test2
@dependsOnGroups:Test1
Feature: A feature is a collection of scenarios
XML config将是:
<listeners>
<listener class-name="com.qmetry.qaf.example.test.StopRunListener" />
</listeners>
<groups>
<run>
<include name="Test1" /> <!-- don't add @ in group for BDD or BDD2 -->
<include name="Test2" />
</run>
</groups>
<classes>
<class
name="com.qmetry.qaf.automation.step.client.text.BDDTestFactory2" />
</classes>