如果第一个方案在QAF中失败,则停止执行-黄瓜

时间:2019-03-21 14:35:37

标签: java cucumber testng qaf

当前,我们从testng.xml触发我们的烟雾测试,其中有两个要验证的不同sceanrios。

我们的要求是,如果一个方案失败(@ Test1),则其他方案不应执行(@ Test2)。如何在QAF,Testng-黄瓜设置中实现此目标?

    <groups>
        <run>
            <include name="@Test1" />
            <include name="@Test2" />
        </run>
    </groups>
    <classes>
        <class
            name="com.qmetry.qaf.automation.step.client.gherkin.GherkinScenarioFactory" />
    </classes>
</test>

1 个答案:

答案 0 :(得分:1)

方法之一是通过实现方法调用侦听器。在after方法中可以设置标志,而在before方法中可以根据标志的值检查标志并跳过测试。例如:

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>