In Cucumber, “@CucumberOptions“ is an annotation used to specify various options for running Cucumber tests. It allows you to configure different aspects of how your Cucumber tests are executed. This annotation is typically placed above the test runner class, which is responsible for triggering the execution of Cucumber tests.
1.Command-line arguments have the highest precedence. If you specify options through the CLI, they will override any settings provided by “@CucumberOptions“ or properties files. For example, running the tests with specific options from the terminal.
mvn clean test -Dcucumber.options="--tags @smoke --plugin pretty"
2. The “@CucumberOptions” annotation comes next in the precedence order. Settings configured here will override those provided in properties files but will be overridden by CLI arguments. For example, specifying configurations directly in the test runner class.
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 = "com.mycompany.stepdefinitions",
plugin = {"pretty", "html:target/cucumber-reports.html", "json:target/cucumber.json"},
tags = "@smoke",
monochrome = true,
dryRun = false
)
public class RunCucumberTests {
}
3. Configuration settings in properties files have the lowest precedence. These settings will be used if neither CLI arguments nor “@CucumberOptions“ provide a value for a particular option. For example, Creating a “cucumber.properties“ file to specify default configurations.
cucumber.features=src/test/resources/features
cucumber.glue=com.mycompany.stepdefinitions
cucumber.plugin=pretty, html:target/cucumber-reports.html, json:target/cucumber.json
cucumber.tags=@smoke
Configuration Options in CucumberOptions
Below is an example of CucumberOptions
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 = "com.mycompany.stepdefinitions",
plugin = {"pretty", "html:target/cucumber-reports.html", "json:target/cucumber.json"},
tags = "@smoke",
monochrome = true,
dryRun = false,
strict = true,
name = "Login functionality" )
public class RunCucumberTest {
}
In this example:
- The “features” attribute points to the location of the feature files.
- The “glue” attribute points to the package where the step definitions are located.
- The “plugin” attribute specifies three plugins: one for pretty output in the console, one for an HTML report, and one for a JSON report.
- The “tags” attribute specifies that only scenarios tagged with
@smokewill be run. - The “monochrome” attribute is set to true to make the console output more readable.
- The “dryRun” attribute is set to false to indicate that the tests should actually be executed.
- The “strict” attribute treats undefined and pending steps as errors, ensuring all steps are fully implemented.
- The “name” attribute allows running scenarios that match a specific name or pattern using regular expressions.