How to create Runner class in Cucumber using Java
Runner class in Cucumber helps to execute tests in a structured manner. You can say it is the control center for the Cucucmber BDD framework. Cucumber is a BDD-based tool that works well with selenium and many other tools.
Test Runner file is required to execute the Step definition methods written for the feature file. Check out a sample Test runner class in cucumber using TestNG first
Interestingly, you can use JUNIT as well as TestNG both to create a Runner file with Cucumber.
Honestly I never knew or understand, why the JUNIT is so popular for creating the runner class when JUNIT and TestNG both are equally easy to use and efficient.
I have written both of the ways you can create runner class in cucumber and selenium here. It’s upto you to decide which one to use.
Runner Class in cucumber using Java and JUnit
I am not writing any explanation as it is self explanatory.
/**
* This is a sample Test Runner class using junit
* Values an be provided as per the requirements
*/
package runner;
import org.junit.runner.RunWith;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
@RunWith(Cucumber.class)
@CucumberOptions(
features = {"src/test/resources/features"},
glue= {"stepsdefs","support"},
tags= "P2"
plugin= {"html:target/cucumber-html-report", "json:target/cucumber-json-report.json"} ,
monochrome= true,
strict= true,
dryRun= false
)
public class TestRunner {
}
Runner Class in cucumber using Java and TestNG
This differs from Runner class using JUNIT a little. TestNG does not use @RunWith. Instead, we’ll use the @CucumberOptions annotation with TestNG’s AbstractTestNGCucumberTests class
package runner;
import io.cucumber.testng.AbstractTestNGCucumberTests;
import io.cucumber.testng.CucumberOptions;
import org.testng.annotations.DataProvider;
@CucumberOptions(
features = {"src/test/resources/features"}, // Path to the feature files
glue = {"stepsdefs", "support"}, // Package containing step definitions
tags = "@P2", // Tag to filter test execution
plugin = {"html:target/cucumber-html-report", "json:target/cucumber-json-report.json"}, // Reporting plugins
monochrome = true, // Better console output formatting
strict = true, // Fail the execution if there are undefined steps
dryRun = false // Set to true to check mapping of steps without execution
)
public class TestRunner extends AbstractTestNGCucumberTests {
// This method allows parallel execution of tests if enabled in TestNG
@Override
@DataProvider(parallel = true)
public Object[][] scenarios() {
return super.scenarios();
}
}
What are the Changes:
1. TestNG Inheritance:
Instead of using @RunWith (specific to JUnit), TestNG uses inheritance from AbstractTestNGCucumberTests. This class enables Cucumber to run in the TestNG environment.
2. DataProvider for Parallel Execution:
@DataProvider(parallel = true) allows the tests to run in parallel. If you don’t want parallel execution, you can set parallel = false or remove the DataProvider method altogether.
3. CucumberOptions:
The @CucumberOptions annotation remains the same, controlling features, glue code, tags, and reporting formats.
4. JUnit Removal:
Removed @RunWith(Cucumber.class) since it is not required in TestNG. Instead, TestNG automatically picks up AbstractTestNGCucumberTests for execution.
Discover more from Automation Script
Subscribe to get the latest posts sent to your email.