2

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

Nagarjuna Reddy
  • 759
  • 9
  • 19
  • 39

5 Answers5

2

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?

Community
  • 1
  • 1
Sid
  • 408
  • 4
  • 7
  • i found this kind of tips. when i add @ test annotation in the same class, these are running. Dont know whether thats the right way – Nagarjuna Reddy Sep 27 '16 at 05:52
  • It is one of the ways. There was no way for Maven to realize what are tests in this case. The two annotations basically tell testng what constitutes a test. – Sid Sep 27 '16 at 15:57
  • To be clearer, "@test" is used to annotate a class/function as a test, without that a method will be treated as a normal function by TestNG. "@Test" is used in test suites when the framework doesnt have other tools like Cucumber. To make full use of your feature file and to specify which feature files to run, "@CucumberOptions" annotation has to be used. – Sid Sep 27 '16 at 16:25
0

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

Khan Here
  • 18
  • 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{
}

It will work for sure.

David Buck
  • 3,752
  • 35
  • 31
  • 35
0

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{

}
shyam yadav
  • 214
  • 1
  • 10
-1

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

MarcelT
  • 32
  • 3