Interview Questions on Reusability in Cucumber
How do you make steps reusable in Cucumber?
Use parameterized steps with placeholders like
{string}or{int}.
Can you reuse step definitions across multiple scenarios or feature files?
Yes. As long as the step text matches (or accepts dynamic values), the same step definition will be called across scenarios and feature files.
What happens if two step definitions match the same step?
Cucumber will throw an AmbiguousStepDefinitionsException. Make sure your steps are uniquely identifiable or scoped correctly.
How do you handle similar steps with slight variations?
Use parameterization to abstract variations. If needed, use Regex or write separate methods only for major differences.
What are other ways to make code reusable in a Cucumber framework?
- Page Object Model for UI logic
- Utility/helper classes for browser actions, wait conditions
- Hooks (
@Before,@After) for setup/cleanup - Data-driven scenarios with Examples/Table
- Scenario Outline with Examples
- Data Tables
- Regular Expressions in Step Definitions
- Scenario is used for a single set of data.
- Scenario Outline is used to run the same steps with multiple sets of data provided in an Examples table.
Scenario Outline: Login test
When user logs in with "<username>" and "<password>"
Then homepage should be displayed
Examples:
| username | password |
| admin | 1234 |
| user | 5678 |
When user provides credentials
| username | admin |
| password | 1234 |
- Scenario Outline is best for running the same scenario multiple times with different data sets.
- Data Table is best for providing structured data in a single scenario.
@When("^user logs in with username \"([^\"]*)\" and password \"([^\"]*)\"$")
public void user_logs_in(String username, String password) {
// login logic
}
- May reduce readability if over-complicated
- Difficult to debug when many rows fail
- Maintenance overhead with complex data combinations
Scenario Outline instead of a DataTable?A: Use
Scenario Outline when you want to run the same scenario multiple times with different sets of data. Use DataTable when you want to pass structured input to a step inside a single scenario.Q1. What are tags in Cucumber?
Answer:
Tags in Cucumber are annotations (prefixed with @) used to group scenarios or features, allowing selective test execution. They help manage and organize large test suites.
🔹 Q2. Where can you apply tags in a feature file?
Answer:
You can apply tags:
-
Above the Feature to apply to all scenarios
-
Above individual Scenarios or Scenario Outlines
🔹 Q3. What is the syntax to tag a scenario in a feature file?
Answer:
🔹 Q4. How do you execute only tagged scenarios in Cucumber?
Answer:
By specifying the tag in your runner file:
✅ Intermediate-Level Questions
🔹 Q5. Can you use multiple tags on a scenario?
Answer:
Yes. A scenario can have multiple tags:
🔹 Q6. What is the difference between AND, OR, and NOT with tags?
Answer:
-
@Tag1 and @Tag2: Runs scenarios with both tags. -
@Tag1 or @Tag2: Runs scenarios with either tag. -
not @Tag3: Excludes scenarios with this tag.
🔹 Q7. How to exclude a specific tag while executing scenarios?
Answer:
🔹 Q8. How do tags help in CI/CD automation?
Answer:
Tags allow you to create logical test groups (like @Smoke, @Sanity, @Regression), which can be triggered selectively in different stages of your CI/CD pipeline, saving time and resources.
✅ Advanced/Behavioral Questions
🔹 Q9. Can you create custom tags?
Answer:
Yes, any string prefixed with @ can be used as a custom tag. Example: @Login, @AdminFlow, etc.
🔹 Q10. Have you used tags to organize a large test suite? How?
Answer:
“Yes, in my project we had over 200 scenarios. We used tags like @Smoke, @Regression, @API, @UI, and @Critical. Based on the tag, our CI pipeline would run quick smoke tests on every PR and full regression tests nightly.”
🔹 Q11. Can tags be used with Hooks in Cucumber?
Answer:
Yes. Hooks (@Before, @After) can be configured to run only for specific tags, like:
🔹 Q12. Can the same tag be reused across multiple feature files?
Answer:
Yes, the same tag can be applied across different scenarios and features. It helps execute all related tests together.
🔹 Q13. Can you tag steps in Cucumber?
Answer:
❌ No. Cucumber does not support tagging individual steps. Tags are only for Feature, Scenario, or Scenario Outline.
Q1. What is the Background keyword in Cucumber?
Answer:
The Background keyword is used to define common steps that need to be executed before each scenario in a feature file. It helps avoid repetition of the same Given steps across multiple scenarios.
🔹 Q2. Where do you place the Background section in a feature file?
Answer:
It is placed after the Feature keyword and before the first Scenario in the feature file.
🔹 Q3. How many Background blocks can you have in one feature file?
Answer:
Only one Background block is allowed per feature file.
🔹 Q4. Do Background steps execute before every scenario?
Answer:
Yes, Background steps are executed before each scenario and scenario outline example row in that feature file.
🔹 Q5. What is the difference between Background and Before hooks?
Answer:
Background | @Before Hook |
|---|---|
| Written in feature file | Written in step definition Java class |
| Readable to business users | Used for technical setup (e.g., DB, API, driver setup) |
| Runs before each scenario | Can be customized using tags to run before specific scenarios |
✅ Intermediate-Level Questions
🔹 Q6. Can Background contain steps like When or Then?
Answer:
Technically yes, but it's not recommended. Background should be used for preconditions (Given) only, to set the context.
🔹 Q7. Will the Background run for @Ignore or @WIP tagged scenarios?
Answer:
No, if the scenario is skipped due to filtering tags, the Background will not run for those scenarios.
🔹 Q8. Can I use Background inside a Scenario Outline?
Answer:
Yes. The Background will run once for every set of examples (data row) in the Scenario Outline.
✅ Advanced/Behavioral Questions
🔹 Q9. What’s a real-world example where you’ve used Background effectively?
Answer:
"In our e-commerce automation suite, every scenario required the user to be logged in. Instead of repeating the login steps in every scenario, we defined them once in the Background. This made our tests cleaner and reduced maintenance effort."
🔹 Q10. When would you choose NOT to use Background and use @Before hook instead?
Answer:
If the setup logic is technical or should only apply to scenarios with a specific tag (e.g., @API, @Smoke), I would use a @Before("@tag") hook in the step definition class instead of Background.
🔹 Q11. Can we parameterize steps in Background like we do in Scenario Outline?
Answer:
No. The Background section cannot use parameters or Examples like Scenario Outline. It's intended for static, common preconditions.
🔹 Q12. How do Background steps affect test reports?
Answer:
They appear in the report as pre-steps for each scenario and are marked as passed/failed independently of the scenario steps.
==================
Where are the reports stored when generated?
Answer:
They are stored in the target/ folder:
-
HTML:
target/cucumber-reports.html -
JSON:
target/cucumber.json -
JUnit XML:
target/results.xml
What does the pretty plugin do?
Answer:
It formats the test output in a human-readable form in the console. It does not create a physical report file.
How do you generate reports in Cucumber?
Answer:
Cucumber provides built-in plugins like:
-
"pretty"for console logs -
"html:target/cucumber-reports.html"for basic HTML reports -
"json:target/cucumber.json"for integration with advanced reporting tools -
"junit:target/results.xml"for CI/CD tools like Jenkins
Can you generate multiple report formats at once?
Answer:
Yes. You can specify multiple plugins in the @CucumberOptions annotation: