Arun Salvi QA
Arun Salvi QA

Reputation: 11

How to use TestNG with Cucumber framework with JAVA?

I am using Junit to run the runner file but I want to use TestNG to run the feature files and extent reporting. Please help me to find out the TestNG with cucumber runner class to run the project.

Upvotes: 0

Views: 3694

Answers (3)

Ramdeep Singh
Ramdeep Singh

Reputation: 1

To replace Junit with TestNG, one can use below:

a. In TestNG xml add path and name of TestRunner file in class tag. It will help TestNG to locate cucumber's TestRunner file

b. In TestRunner file, make sure your class extends AbstractTestNGCucumberTests

c. In Maven add cucumber-testNG dependency to load required jars.

Then you are good to go, it will also facilitate you to control test execution from testNG in addition to what controls we already had (from TestRunner). Also, one can now user cucumber-reporting which seems to be the best reporting tool, especially for Test Automation Engineers. (but i think Developers can also make use of it in unit tests)

Upvotes: 0

Anoop Singh
Anoop Singh

Reputation: 29

Step1 - If you are using Maven, then in your pom.xml add this "cucumber-testng" dependency- I have used 4.8.0 version

<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-testng</artifactId>
    <version>4.8.0</version>
</dependency>

Step2 - you need to create a Runner Class and extend it to AbstractTestNGCucumberTests so it will look like as below-

public class <your runner class name here>  extends AbstractTestNGCucumberTests

Step3- Add the below "CucumberOptions" in your runner class

@CucumberOptions
(
features = "<your feature file path>", //the path of the feature files
glue={"<your step definition file path>"}, //the path of the step definition files
plugin= {"pretty:target/cucumber-pretty.txt",
        "html:target/cucumber-html-report",
        "json:target/cucumber.json",
        "rerun:target/rerun.txt"
        }, //to generate different types of reporting
monochrome = true, //display the console output in a proper readable format
strict = true, //it will check if any step is not defined in step definition file
dryRun = false //to check the mapping is proper between feature file and step definition file
)

Now, you can run your execution from your testng.xml file. Make sure to add the "Runner class" in your testng.xml file.

<class name="runner class path here" />

Upvotes: 0

Neha
Neha

Reputation: 316

@RunWith(ExtendedCucumber.class) -This is used for Junit
AbstractTestNGCucumberTests – is used for TestNG

Extend the Runner class with AbstractTestNGCucumberTests and add the TestNG dependencies

Upvotes: 1

Related Questions