0

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.

2

3 Answers 3

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

Extend the Runner class with AbstractTestNGCucumberTests and add the TestNG dependencies

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Neha, It is helpful for me and I have used to run the TestNG xml file for Test Suite run in Cucumber framework :)
0

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" />

Comments

0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.