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 { }
@CucumberOptions
| Parameter | Description |
|---|---|
| features | Path to the .feature files. It can be a directory or specific file path. |
| glue | Package name(s) where step definitions and hooks are defined. |
| plugin | Used for report generation. Common plugins include "pretty", "html:target/report.html", "json:target/report.json" etc. |
| monochrome | Makes the console output more readable by removing unnecessary characters. Set to true or false. |
| tags | Filters which scenarios or features to run based on assigned tags. Useful for running subsets like @SmokeTest, @Regression. |
| dryRun | If true, it checks if every Step in the feature file has a corresponding Step Definition without executing the tests. |
| strict | If true, fails execution if there are undefined or pending steps. |
| name | Runs scenarios whose names match the given regular expression. |
| snippets | Controls the style of generated snippets (CAMELCASE or UNDERSCORE). |
@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.
@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.