如何使用testng实现和运行黄瓜测试文件

时间:2016-09-26 11:00:33

标签: selenium cucumber

尝试实现selenium + Cucumber + Testng而不是Junit。

我的疑问是

  1. testng
  2. 中@Runwith(Cucumber.class)的替代方法是什么?
  3. 如何运行包含要素文件路径的类文件
  4. package runner;
    
    import cucumber.api.CucumberOptions;
    import cucumber.api.testng.AbstractTestNGCucumberTests;
    
    
    @CucumberOptions(features="src/main/java/testCases/Cucumber/Login_Cucumber.Feature",glue="")
    public class TestRunner extends AbstractTestNGCucumberTests {
    
    }
    

    run with testng/cucumber are not appearing

4 个答案:

答案 0 :(得分:1)

TestNg使用@CucumberOptions标记来声明参数

@CucumberOptions(plugin = "json:target/cucumber-report.json")
public class RunCukesTest extends AbstractTestNGCucumberTests {
}

@CucumberOptions(features = "src/test/resources/features/Download.feature",
        glue = "uk.co.automatictester.jwebfwk.glue",
        format = {"pretty"})

检查出来:https://github.com/cucumber/cucumber-jvm/tree/master/examples/java-calculator-testng

也可能是:How to integrate the cucumber in testNG?

答案 1 :(得分:0)

安装 TestNG Eclipse插件。之后您应该能够运行 TestNG Test

答案 2 :(得分:0)

首先,Cucumber有.feature文件,而不是测试文件。

回答你的第一个问题:1。在testng中@Runwith(Cucumber.class)的替代品是什么? "在使用TestNG&#34时,你不需要@RunWith;

我没有理解你的第二个问题,但是你需要了解Cucumber默认运行结束执行Runner类,你已经在@CucumberOptions部分定义了功能文件。

为了更清楚,您可以使用TestNG轻松实现和运行Cucumber项目。整个游戏都在你的pom.xml文件和Runner类中。

以下详细说明还解释了您可以使用TestNG在黄瓜中运行每个方案作为测试。

如何?它的解释如下:

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

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

@CucumberOptions(features = "src/test/resources", plugin = "json:target/cucumber-report-feature-composite.json")
public class TestRunner {
private TestNGCucumberRunner testNGCucumberRunner;

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

@Test(groups = "cucumber scenarios", description = "Runs Cucumber 
Scenarios", dataProvider = "scenarios")
public void scenario(PickleEventWrapper pickleEvent, CucumberFeatureWrapper 
cucumberFeature) throws Throwable{
testNGCucumberRunner.runScenario(pickleEvent.getPickleEvent());
}
@DataProvider
public Object[][] scenarios() {
    return testNGCucumberRunner.provideScenarios();
}

@AfterClass(alwaysRun = true)
public void tearDownClass() throws Exception {
    testNGCucumberRunner.finish();
}
 }

使用mvn clean test命令运行并查看魔法:)

我很高兴看到你的问题得到解决。如果此问题仍未解决,请与我们联系。

参考: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

答案 3 :(得分:0)

import cucumber.api.CucumberOptions;
import cucumber.api.testng.AbstractTestNGCucumberTests;
@CucumberOptions(features="src/test/resources/features",glue="stepDefinitions",tags="@Test01",plugin= {"pretty", "html:target/cucumber-reports" },monochrome=true)
public class RunnerTest extends AbstractTestNGCucumberTests{
}

肯定会起作用。