</>

Technology

Cucumber

Difficulty

Intermediate

Interview Question

What is the purpose of the @CucumberOptions annotation?

Answer

@CucumberOptions Annotation

@CucumberOptions annotation is used on the TestRunner class to configure how Cucumber executes tests. It provides a link between feature files and step definitions, and configures plugins, tags, and execution behavior.

Syntax

Java
@CucumberOptions(
    features = "src/test/resources/features",
    glue = {"stepDefinitions", "hooks"},
    plugin = {"pretty", "html:target/cucumber-reports/report.html", "json:target/cucumber.json"},
    tags = "@smoke",
    dryRun = false,
    monochrome = true
)

All Available Options

OptionPurposeExample
featuresPath to feature files"src/test/resources/features"
gluePackage(s) of step definitions{"stepDefs", "hooks"}
pluginReporting format and output"html:target/report.html"
tagsFilter which scenarios to run"@smoke and not @slow"
dryRunCheck for missing step defstrue / false
monochromeClean console outputtrue
nameFilter by scenario name regex"Login.*"
publishPublish report to Cucumber Cloudtrue

Complete TestRunner Example

Java
package runners;

import io.cucumber.testng.AbstractTestNGCucumberTests;
import io.cucumber.testng.CucumberOptions;
import org.testng.annotations.DataProvider;

@CucumberOptions(
    features = "src/test/resources/features",
    glue = {"stepDefinitions", "hooks"},
    plugin = {
        "pretty",
        "html:target/cucumber-reports/cucumber.html",
        "json:target/cucumber-reports/cucumber.json",
        "junit:target/cucumber-reports/cucumber.xml"
    },
    tags = "@smoke",
    dryRun = false,
    monochrome = true
)
public class TestRunner extends AbstractTestNGCucumberTests {

    @Override
    @DataProvider(parallel = true)
    public Object[][] scenarios() {
        return super.scenarios();
    }
}

dryRun = true

When dryRun = true, Cucumber checks if all steps in feature files have corresponding step definitions without actually executing the tests. Use this to detect missing step definitions.

Syntax of @CucumberOptions Tag

CODE
@CucumberOptions(features="Features", glue={"StepDefinition"})

Follow AutomateQA

Related Topics