How to Use the Cucumber Runner Class in Java?



cucumber test runner class,cucumber runner,cucumber test runner,cucumber tutorials,@cucumberoptions,cucumber automation,runner,test runner,cucumber automation tutorial,junit runner,selenium automation,@runwith



The Cucumber Runner class is the main class for executing Cucumber tests. It is responsible for discovering feature files, reading them, and executing the associated step definitions. In this section, we will explain the Runner class in detail with an example.



Here's a step-by-step guide on how to run tests with Cucumber:



1. Create a new class and annotate it with @RunWith(Cucumber.class). This tells JUnit to use the Cucumber runner when executing the test.


import org.junit.runner.RunWith;
 
import cucumber.api.junit.Cucumber;
 
 
 
@RunWith(Cucumber.class)
 
public class CucumberRunnerTest {
 
}





2. Specify the path to the feature files and step definitions in the @CucumberOptions annotation.


import org.junit.runner.RunWith;
 
import cucumber.api.junit.Cucumber;
 
import cucumber.api.CucumberOptions;
 
 
 
@RunWith(Cucumber.class)
 
@CucumberOptions(features = "src/test/resources/features", 
 
                 glue = "com.example.stepdefinitions")
 
public class CucumberRunnerTest {
 
}


Here, we have specified the path to our feature files using the features parameter and the path to our step definitions using the glue parameter.




3. Run the test. Once you have created the Runner class, you can run it like any other JUnit test.

When you run the test, Cucumber will look for feature files in the specified location and execute the associated step definitions. The results of the test will be output to the console.


In addition to the features and glue parameters, there are many other parameters you can specify in the @CucumberOptions annotation. 

For example, you can specify the output format, the tags to include or exclude, and the location of the report files.



import org.junit.runner.RunWith;
 
import cucumber.api.junit.Cucumber;
 
import cucumber.api.CucumberOptions;
 
 
 
@RunWith(Cucumber.class)
 
@CucumberOptions(
 
    features = "src/test/resources/features",
 
    glue = "com.example.stepdefinitions",
 
    plugin = {"pretty", "html:target/cucumber", "json:target/cucumber.json"},
 
    tags = "@smoke and not @ignore",
 
    monochrome = true,
 
    dryRun = false
 
)
 
public class CucumberRunnerTest {
 
}


  • In this example, we have specified that Cucumber should output the results in a pretty format, as well as HTML and JSON formats. 
  • We have also included a tag expression that will only run scenarios tagged with "@smoke" and exclude any scenarios tagged with "@ignore". 
  • We have set monochrome to true to make the console output more readable, and dryRun to false to ensure that all the steps are executed.




4. Analyze the test results: Once the tests have finished running, you can analyze the test results to see which scenarios passed or failed. 

Cucumber provides several built-in report formats, including HTML, JSON, and XML, which can be generated automatically by adding the appropriate options to the @CucumberOptions annotation.



In conclusion, the Cucumber Runner class is essential to any Cucumber project. It provides a way to discover and execute feature files and step definitions, and it allows you to customize the behavior of your tests using various parameters.

Post a Comment

Post a Comment (0)

Previous Post Next Post