</>

Technology

Cucumber

Difficulty

Beginner

Interview Question

Should any code be written within the TestRunner class in Cucumber?

Answer

Code in TestRunner Class

Answer: No Code Inside the Class Body

The TestRunner class should be empty — no methods, no logic, no assertions inside its class body. It should only include:

  1. Package declaration
  2. Import statements
  3. @CucumberOptions annotation
  4. Class declaration (extending AbstractTestNGCucumberTests for TestNG)

Correct TestRunner (Empty Body)

Java
package runners;

import io.cucumber.testng.AbstractTestNGCucumberTests;
import io.cucumber.testng.CucumberOptions;

// All configuration is in the annotation — no code in the body
@CucumberOptions(
    features = "src/test/resources/features",
    glue = {"stepDefinitions", "hooks"}
)
public class TestRunner extends AbstractTestNGCucumberTests {
    // NOTHING HERE — this is intentional and correct
}

Exception: Parallel Execution DataProvider

The only acceptable code is overriding scenarios() for parallel execution:

Java
@CucumberOptions(...)
public class TestRunner extends AbstractTestNGCucumberTests {

    @Override
    @DataProvider(parallel = true)  // ← only allowed code
    public Object[][] scenarios() {
        return super.scenarios();
    }
}

Where Should Code Go?

Code TypeWhere It Lives
Browser setup / teardown@Before / @After in Hooks.java
Step actions (click, type)Step Definition classes
Page interactionsPage Object classes
Test dataProperties files / Excel / DataProvider
AssertionsStep definition @Then methods
Driver managementBaseTest / DriverFactory class

Why Keep TestRunner Empty?

  1. Separation of Concerns — TestRunner configures, step definitions act
  2. Readability — Configuration in one place, logic in another
  3. Cucumber convention — Framework is designed for empty runner
  4. Maintainability — Changing test behavior = change step defs, not runner

Follow AutomateQA

Related Topics