A Feature File in Cucumber is a structured document that describes application behavior in plain English using Gherkin syntax. It has a .feature extension and serves as the foundation for Behavior-Driven Development (BDD) tests.


1. Structure of a Feature File

A feature file typically includes:

  1. Feature: A high-level description of the functionality being tested.
  2. Scenario: A specific test case with predefined steps.
  3. Given-When-Then: The steps defining the test flow.
  4. Scenario Outline: A parameterized test case for data-driven testing.
  5. Examples: A table providing multiple input values for a Scenario Outline.
  6. Tags (optional): Used to categorize and filter test cases.

2. Creating a Feature File in Cucumber

Example Feature File – Login Functionality

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 redirected to the homepage

  Scenario: Invalid Login
    Given the user is on the login page
    When the user enters incorrect credentials
    Then an error message should be displayed

3. Components of a Feature File

1️⃣ Feature

The Feature keyword describes the functionality being tested.

Feature: Search Functionality
  Users should be able to search for products on the website.

2️⃣ Scenario

Each scenario represents a single test case with a clear objective.

Scenario: Searching for a product

3️⃣ Given (Precondition)

Defines the initial state before performing an action.

Given the user is on the homepage

4️⃣ When (Action/Trigger)

Specifies user interactions or events that change the system state.

When the user searches for "laptop"

5️⃣ Then (Expected Outcome)

Describes the expected result of the test case.

Then search results for "laptop" should be displayed

6️⃣ And / But (Additional Conditions)

  • And is used for additional steps in Given/When/Then statements.
  • But is used for negative conditions.
Given the user is logged in
And the user is on the dashboard
When the user clicks on "Logout"
Then the user should be redirected to the login page
But the session should be cleared

4. Scenario Outline (Data-Driven Testing)

A Scenario Outline allows the same scenario to be tested with multiple sets of data.

Scenario Outline: Login with multiple users
  Given the user is on the login page
  When the user enters "<username>" and "<password>"
  Then the login should be "<status>"

Examples:
  | username  | password  | status  |
  | user1     | pass123   | success |
  | user2     | wrongpass | failure |

πŸ“Œ How it Works:

  • <username> and <password> are placeholders that get replaced by values from the Examples table.
  • The test will run twice, once for each row of data.

5. Using Tags in Feature Files

Tags allow test scenarios to be categorized and filtered during execution.

@SmokeTest @Regression
Feature: Login Functionality

  @PositiveTest
  Scenario: Successful Login
    Given the user is on the login page
    When the user enters valid credentials
    Then the user should be redirected to the homepage

  @NegativeTest
  Scenario: Invalid Login
    Given the user is on the login page
    When the user enters incorrect credentials
    Then an error message should be displayed

πŸ“Œ Tags Usage:

  • Run specific scenarios using tags like @SmokeTest or @Regression.
  • Execute tagged tests with:
    mvn test -Dcucumber.filter.tags="@SmokeTest"
    

6. Best Practices for Writing Feature Files

βœ” Use simple and clear language.
βœ” Write independent scenarios (avoid dependencies).
βœ” Keep feature files short and well-structured.
βœ” Use Scenario Outline for data-driven tests.
βœ” Use comments (#) for explanations where necessary.


7. Conclusion

Feature files in Cucumber provide a structured way to define test scenarios using Gherkin syntax. They make test cases readable, reusable, and maintainable, bridging the gap between business requirements and automation testing. πŸš€