How to implement and run cucumber test files using testng

点点圈 提交于 2019-12-31 03:12:10

问题


Trying to implement selenium + Cucumber + Testng instead of Junit.

My queries are

  1. What is the alternate for @Runwith(Cucumber.class) in testng
  2. How to run the class file which contains the path to feature file

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 {

}


回答1:


TestNg uses @CucumberOptions tag to declare parameters

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

or

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

Check this out: https://github.com/cucumber/cucumber-jvm/tree/master/examples/java-calculator-testng

Also a possible dup of :How to integrate the cucumber in testNG?




回答2:


Install TestNG Eclipse Plugin. Afterwards you should be able to run TestNG Test.




回答3:


First of all, Cucumber have .feature files and not test files.

Answer to your first question: 1. What is the alternate for @Runwith(Cucumber.class) in testng? "You don't need @RunWith while running with TestNG"

I didn't understand your second question but you need to understand that Cucumber runs end execute the Runner class by default and you have already defined feature files in @CucumberOptions section.

To make it more clear you can easily implement and Run Cucumber project using TestNG. The whole game is in your pom.xml file and Runner class.

Following detail also explains that you can run each scenario in cucumber as a test using TestNG.

How? It's explained below:

First of all, update your Cucumber Maven dependencies from info.cukes to io.cucumber dependencies

Following Java code in Cucumber Runner Class worked perfectly for me to run each scenario as TestNG test in feature files:

@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();
}
 }

Run with mvn clean test command and see the magic :)

I would be happy to see your problem resolved. Please let me know if this issue is still not resolved.

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

I followed this approach: https://github.com/cucumber/cucumber-jvm/blob/master/examples/java-calculator-testng/src/test/java/cucumber/examples/java/calculator/RunCukesByCompositionTest.java



来源:https://stackoverflow.com/questions/39701094/how-to-implement-and-run-cucumber-test-files-using-testng

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!