Cucumber Options in Cucumber

  

In Cucumber@CucumberOptions is an important annotation used in the Test Runner class to define and customize the behavior of the Cucumber framework while executing tests. This annotation comes from the io.cucumber.junit or cucumber.api package (depending on the version you're using).


What is @CucumberOptions?

@CucumberOptions is used to configure various execution parameters for running Cucumber tests. It helps define where feature files and step definitions are located, how the results should be reported, and what tags or filters to apply during test execution.


Syntax Example

@RunWith(Cucumber.class)
@CucumberOptions(
    features = "src/test/resources/features",
    glue = "stepdefinitions",
    plugin = {"pretty", "html:target/cucumber-report.html"},
    monochrome = true,
    tags = "@SmokeTest"
)
public class TestRunner {
}




Parameters of @CucumberOptions


ParameterDescription
featuresPath to the .feature files. It can be a directory or specific file path.
gluePackage name(s) where step definitions and hooks are defined.
pluginUsed for report generation. Common plugins include "pretty", "html:target/report.html", "json:target/report.json" etc.
monochromeMakes the console output more readable by removing unnecessary characters. Set to true or false.
tagsFilters which scenarios or features to run based on assigned tags. Useful for running subsets like @SmokeTest, @Regression.
dryRunIf true, it checks if every Step in the feature file has a corresponding Step Definition without executing the tests.
strictIf true, fails execution if there are undefined or pending steps.
nameRuns scenarios whose names match the given regular expression.
snippetsControls the style of generated snippets (CAMELCASE or UNDERSCORE).



Example with Explanation

@CucumberOptions(
    features = "src/test/resources/features/Login.feature", // specific feature file
    glue = "stepdefinitions.login",                          // package with step defs
    plugin = {
        "pretty",
        "json:target/jsonReports/report.json",
        "html:target/htmlReports"
    },
    tags = "@Regression and not @WIP",                       // include/exclude based on tags
    monochrome = true,                                       // clean console output
    dryRun = false,                                          // actually runs the tests
    strict = true                                            // fails on undefined steps
)


When and Where Is It Used?

  • Placed above the Test Runner class that runs with @RunWith(Cucumber.class).

  • Helps control the execution and reporting of Cucumber tests.

  • Allows filtering which scenarios to run in large test suites.

  • Provides integration with tools like Jenkins and Allure via plugins.



Important Points:

@CucumberOptions gives you flexibility and control over how your Cucumber test suite runs by specifying:

  • where your tests are,

  • how they should be reported,

  • which tests to run,

  • and how results should be formatted.