如何使用Cucumber&amp ;;在不同的@Test或xml <test>中运行Cucumber场景TestNG的

时间:2017-02-01 15:13:05

标签: cucumber testng cucumber-jvm testng-dataprovider cucumber-java

我正在考虑使用TestNG运行Cucumber测试。但是我遇到的问题是我的所有方案都作为一个TestNG @Test会话运行。有没有办法将每个场景作为单独的 <suite name="cucumber Suites"> <test name="cucumber-testing"> <classes> <class name="runners.Run2" /> </classes> </test> </suite> 会话运行?

这是我的TestNG xml:

import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import cucumber.api.CucumberOptions;
import cucumber.api.testng.AbstractTestNGCucumberTests;
import cucumber.api.testng.CucumberFeatureWrapper;
import cucumber.api.testng.TestNGCucumberRunner;

@CucumberOptions(   features="cucumber/features/example.feature",
                    glue="steps",
                    format={"pretty"}
                )
public class Run2 extends AbstractTestNGCucumberTests{
    private TestNGCucumberRunner tcr;

    @BeforeClass(alwaysRun=true)
    public void beforeClass() throws Exception{
        tcr = new TestNGCucumberRunner(this.getClass());
    }

    @Test(groups="cucumber", description="Runs CucumberFeature", dataProvider="features")
    public void feature(CucumberFeatureWrapper cucumberFeature){
        tcr.runCucumber(cucumberFeature.getCucumberFeature());
    }

    @DataProvider
    public Object[][] features(){
        return tcr.provideFeatures();
    }

    @AfterClass (alwaysRun=true)
    public void afterClass(){
        tcr.finish();
    }
}

这将调用运行以下Test类:

@DataProviderto

我想知道是否有办法让@Test提供方案和select 'Привет', '你好' 来运行Scenario而不是功能?

这样做的原因是我有其他TestNG测试用于监听器,并且我想使用相同的监听器,报告黄瓜测试。

由于

3 个答案:

答案 0 :(得分:0)

你有没有尝试过:

@CucumberOptions(   features="cucumber/features/example.feature",
                    glue="steps",
                    format={"pretty"}
                )
public class Run2 extends AbstractTestNGCucumberTests{
    private TestNGCucumberRunner tcr;

    @BeforeClass(alwaysRun=true)
    public void beforeClass() throws Exception{
        tcr = new TestNGCucumberRunner(this.getClass());
    }

    @Test(groups="cucumber", description="Runs CucumberFeature")
    public void scenario(){
        for (CucumberFeatureWrapper cucumberFeature : tcr.provideFeatures()) {
            tcr.runCucumber(cucumberFeature.getCucumberFeature());
        }
    }

    @AfterClass (alwaysRun=true)
    public void afterClass(){
        tcr.finish();
    }
}

答案 1 :(得分:0)

您可以尝试为每个功能提供特定的测试名称:

@CucumberOptions(   features="cucumber/features/example.feature",
                    glue="steps",
                    format={"pretty"}
                )
public class Run2 extends AbstractTestNGCucumberTests implements ITest {
    private TestNGCucumberRunner tcr;
    private String featureName;

    @BeforeClass(alwaysRun = true)
    public void beforeClass() throws Exception {
        tcr = new TestNGCucumberRunner(this.getClass());
    }

    @BeforeMethod
    public void beforeMethod(Object[] params) {
        CucumberFeatureWrapper cucumberFeature = (CucumberFeatureWrapper) params[0];
        featureName = cucumberFeature.getCucumberFeature().getGherkinFeature().getName();
    }

    @Test(groups = "cucumber", description = "Runs CucumberFeature", dataProvider = "features")
    public void feature(CucumberFeatureWrapper cucumberFeature) {
        tcr.runCucumber(cucumberFeature.getCucumberFeature());
    }

    @Override
    public String getTestName() {
        return featureName;
    }

    @DataProvider
    public Object[][] features() {
        return tcr.provideFeatures();
    }

    @AfterClass(alwaysRun = true)
    public void afterClass() {
        tcr.finish();
    }
}

让我知道你是否喜欢它。

答案 2 :(得分:0)

答案是&#34;是&#34;,您可以使用TestNG在黄瓜中运行每个场景作为测试。

如何?它的解释如下:

  1. 首先将您的Cucumber Maven依赖项从info.cukes更新为io.cucumber依赖项

  2. 然后代替使用方法&#34; provideFeatures()&#34; &#34; TestNGCucumberRunner&#34;在&#39;你的&#39;以下代码:

    *ngIf
  3. 使用&#34; provideScenarios()&#34; @DataProvider中的方法。

    Cucumber Runner Class中的以下Java代码非常适合我在功能文件中运行每个场景作为TestNG测试:

    @DataProvider
    public Object[][] features(){
        return tcr.provideFeatures();
    }
    

    我很高兴看到您的问题得到解决。

    参考:https://github.com/cucumber/cucumber-jvm/blob/master/testng/README.md

    我遵循了这种方法:https://github.com/cucumber/cucumber-jvm/blob/master/examples/java-calculator-testng/src/test/java/cucumber/examples/java/calculator/RunCukesByCompositionTest.java