'How to implement and run cucumber test files using testng

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 {

}

run with testng/cucumber are not appearing



Solution 1:[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?

Solution 2:[2]

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

Solution 3:[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

Solution 4:[4]

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{
}

It will work for sure.

Solution 5:[5]

This perfectly worked for me package com.shyom.cucumberOptions;

import io.cucumber.testng.AbstractTestNGCucumberTests;
import io.cucumber.testng.CucumberOptions;

@CucumberOptions(
    plugin = "json:target/cucumber-report.json",
    features = "/shyom/src/test/java/feature",
    glue="stepDefinations"
    )
public class TestRunner extends AbstractTestNGCucumberTests{

}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Community
Solution 2 MarcelT
Solution 3 Khan Here
Solution 4 David Buck
Solution 5 shyam yadav