如何使TestNG在“Running Suite”中显示黄瓜情景?

时间:2017-02-20 12:26:48

标签: cucumber testng cucumber-junit

我创建了一个maven项目并尝试使用TestNG插件运行Cucumber + Selenium + Java测试。虽然一些使用JUnit + Cucumber的教程显示了功能的每个场景的结果,但我的TestNG结果选项卡仅显示功能名称。我错过了什么?

我有一个功能身份验证 在里面我有两个方案登录和注销。

我期待在TestNG的“运行套件的结果”标签中出现类似的情况:   From a JUnit tutorial

My current testng result is like this. 'Authentication' is the feature, but the 2 scenarios inside it are not appearing

2 个答案:

答案 0 :(得分:1)

之前的回答是对的,但是 而不是

scenarioList.add(new Object[]{scenario, scenario.getGherkinModel().getName()});

使用

scenarioList.add(new Object[]{scenario});

否则会出现这样的错误: Selenium Webdriver, TestNG - data provider is trying to pass 2 parameter but the method take 3 and TestNG is unable in inject a suitable object

答案 1 :(得分:-1)

如果您按照cucumber-jvm源(https://github.com/cucumber/cucumber-jvm/blob/master/examples/java-calculator-testng/src/test/java/cucumber/examples/java/calculator/RunCukesByFeatureAndCompositionTest.java)中给出的示例,那么由于DataProvider,这将为每个功能创建一个测试:

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

如果您修改DataProvider以返回场景,那么您将在每个场景中获得一次testNG测试。

public List<CucumberFeature> getFeatures() {
        return runtimeOptions.cucumberFeatures(resourceLoader);
    }

public Object[][] provideScenarios() {
        try {
            List<CucumberFeature> features = getFeatures();
            List<Object[]> scenarioList = new ArrayList<Object[]>(features.size());

            for (CucumberFeature feature : features) {
                List<CucumberTagStatement> scenarios = feature.getFeatureElements();

                for (CucumberTagStatement scenario : scenarios) {
                    // If this is a Scenario Outline, split it up so each one is a test.
                    if (scenario instanceof CucumberScenarioOutline) {
                        List<CucumberExamples> cucumberExamplesList = ((CucumberScenarioOutline) scenario).getCucumberExamplesList();

                        for (CucumberExamples cucumberExamples : cucumberExamplesList) {
                            List<CucumberScenario> exampleScenarios = cucumberExamples.createExampleScenarios();
                            for (CucumberScenario exampleScenario : exampleScenarios) {
                                scenarioList.add(new Object[]{exampleScenario, exampleScenario.getGherkinModel().getName()});
                            }
                        }
                    } else
                        scenarioList.add(new Object[]{scenario, scenario.getGherkinModel().getName()});
                }
            }
            return scenarioList.toArray(new Object[][]{});
        } catch (CucumberException e) {
            return new Object[][]{new Object[]{new CucumberExceptionWrapper(e)}};
        }
    }