Components

Cucumber, as a testing tool, consists of several key components that work together to facilitate behavior-driven development (BDD) and test automation. These components include:

1. Feature Files

A feature file in Cucumber is a plain-text file that contains descriptions of one or more features of a software application written in the Gherkin language. It serves as a formalized document to describe the behaviors or functionalities of the software in a human-readable format.

In the previous chapter we have seen the Gherkin syntax. Feature file is written using the structured language called Gherkin. The file representing the features in Gherkin syntax is called as feature file. Features files are saved with .feature extension.

The feature file may contain one or more feature, definitions, scenarios, and steps outlining various test cases. Following is an example of feature file.

Login.feature
Copy
Feature: Login functionality

  Scenario: Successful login
    Given the user is on the login page
    When the user enters valid credentials
    Then the user should be logged in successfully

2. Step Definitions

Step Definitions in Cucumber are the pieces of code that link the Gherkin steps defined in feature files to the actual automation code that performs the actions or assertions in the software under test. Following are the important points to remember.

  • Code Implementation: Step definitions are written in a programming language supported by Cucumber (such as Java, JavaScript, Ruby, Python, etc.).
  • Mapping Gherkin Steps: Each Gherkin step (Given, When, Then, etc.) in the feature files needs a corresponding step definition that defines the actions to be taken or assertions to be made.
  • Connect Feature Files to Automation Code: They provide the bridge between human-readable Gherkin steps and the code that executes those steps in the software application.
  • Regular Expressions: Step definitions often use regular expressions to match the Gherkin steps in feature files with the appropriate code implementation.

Following is an example of step definition file written for above feature file.

LoginStepDefinition.java
Copy
@Given("the user is on the login page")
public void the_user_is_on_the_login_page() 
{
	// Code to navigate to the login page
}
@When("the user enters valid credentials")
public void the_user_enters_valid_credentials() 
{
	// Code to login with username and password
}
@Then("the user should be logged in successfully")
public void the_user_should_be_logged_in_successfully() 
{
	// Code to validate the successful login
}

3. Test Runner

Test Runner is a component responsible for orchestrating and executing the scenarios defined in feature files by connecting them with the associated step definitions, initializing the testing environment, and generating test reports. It acts as the engine that drives the execution of Cucumber tests.

Following are key features of test runner

  • Execution Coordination: Coordinates the execution of feature files and their associated step definitions.
  • Matches Steps to Definitions: Connects the steps written in Gherkin syntax in feature files to their corresponding step definitions in the automation code.
  • Test Environment Setup: Sets up the necessary environment or preconditions required for executing the tests, like initializing connections, setting up configurations, etc.
  • Reporting: Collects and generates test reports detailing the results of the test execution, including passed, failed, or skipped scenarios.
  • Integration with Test Frameworks: Works in conjunction with test frameworks (e.g., JUnit, TestNG for Java; Mocha, Jasmine for JavaScript; pytest for Python, etc.) to execute and manage the test runs.
  • Configuration Management: Manages the configuration settings related to test execution, such as specifying which feature files to run, selecting the output format of reports, etc.
TestRunner.java
Copy
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 = {"stepdefinitions"},
    plugin = {"pretty", "html:target/cucumber-reports"}
)
public class TestRunner 
{
    // This class acts as the test runner for Cucumber tests.
}

In the above example, @RunWith(Cucumber.class) tells JUnit to use Cucumber as the test runner. @CucumberOptions specifies various options like the location of feature files (features), the package where step definitions are located (glue), and the plugins used for generating reports (plugin).