首页 > 解决方案 > QAF 和 Spring 引导环境中 Cucumber 标记为 TestNG 组

问题描述

我在一个功能文件中有 4 个测试,有 2 个不同的标签 @first 和 @then。我希望 @first 测试首先以并行性运行,@then 测试在所有 @first 测试完成后运行,同时也具有并行性。

项目在这里: https ://github.com/marcesso/qafTesting

@CucumberOptions(plugin = {"com.qmetry.qaf.automation.cucumber.QAFCucumberPlugin", "pretty", "html:target"},
        /*tags = {"@Ignore"},*/
        features = {"src/test/resources/my/custom/packagename/testing"})
public class RunnerTest extends AbstractTestNGCucumberTests {

    @Autowired
    private ObjectMapper objectMapper;


    @Test(description = "Runs Cucumber Scenarios", dataProvider = "scenarios", groups = {"first"})
    public void runScenarioFirst(PickleWrapper pickleWrapper, FeatureWrapper featureWrapper) throws Throwable {
        super.runScenario(pickleWrapper,featureWrapper);

    }
    @Test(description = "Runs Cucumber Scenarios", dataProvider = "scenarios", groups = {"then"}, dependsOnMethods =
            "runScenarioFirst")
    public void runScenarioThen(PickleWrapper pickleWrapper, FeatureWrapper featureWrapper) throws Throwable {
        super.runScenario(pickleWrapper,featureWrapper);
    }

    @Override
    @DataProvider(name = "scenarios", parallel = true)
    public Object[][] scenarios() {
        return super.scenarios();
    }

    @PostConstruct
    public void setUp() {
        objectMapper.registerModule(new JavaTimeModule());
    }

}

问题是所有测试都运行两次(每个测试方法一次),@Test 注释的“组”属性没有像我预期的那样过滤测试(在https://qmetry.github.io/qaf/latest/gherkin_client非常底部.html )

也根本没有并行性。

我试图在测试方法中过滤泡菜,但与条件不匹配的测试即使没有运行也会显示为通过

if(pickleWrapper.getPickle().getTags().contains("@first")) {
            super.runScenario(pickleWrapper,featureWrapper);
}

标签: testngspring-testcucumber-javaqaftestng-annotation-test

解决方案


在上面的示例中RunnerTest,来自 qaf 的 GherkinClient 没有出现,因为您使用的是黄瓜跑步者。GherkinScenarioFactoryBDDTestFactory2(使用 qaf 2.1.15+)是 QAF 的 GherkinClient 实现。当你使用它们中的任何一个时,你不需要上面的RunnerTest类。BDDTestFactory2是首选GherkinScenarioFactory,它支持标准 gherkin 语法之上的其他语法功能。

当您使用 cucumber runner (RunnerTest在您的情况下为 class )时,标签不被视为 TestNG 组。如果你想使用 cucumber runner 来运行特性文件,你需要使用cucumber options来处理它。AFAIK,使用黄瓜跑步者时,您正在寻找的东西无法通过单一课程来实现。

当您使用 qaf 时,您可以使用BDD2Factory代替黄瓜测试类。您可以提供考虑场景作为 TestNG 测试用例的 xml 配置。您可以混合和匹配 TestNG 支持的不同配置选项,就像执行用 java 编写的测试一样。在您的情况下,它可能如下所示:

<suite name="QAF Demo" verbose="0" parallel="false" data-provider-thread-count="10">

<test name="First"  parallel="methods"  thread-count="5">
   <groups>
      <run>
        <include name="first" />
      </run>
   </groups>
   <classes>
      <class name="com.qmetry.qaf.automation.step.client.text.BDDTestFactory2" />
   </classes>
</test>

<test name="second"  parallel="methods"  thread-count="5">
   <groups>
      <run>
        <include name="then" />
      </run>
   </groups>
   <classes>
      <class name="com.qmetry.qaf.automation.step.client.text.BDDTestFactory2" />
   </classes>
</test>
</suite>

您还可以使用元数据过滤器。组/标签也被 qaf视为测试用例的元数据。所以而不是:

<groups>
  <run>
    <include name="first" />
  </run>
</groups>

您可以简单地提供include如下参数:

   <parameter name="include" value="{'groups': ['first']}" />

我想在这里重申,上述功能仅在您 BDDTestFactory2使用 Cucumber runner 时可用,但在使用 Cucumber runner 时不可用。参考使用 qaf-bdd-runner


推荐阅读