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:
- Feature: A high-level description of the functionality being tested.
- Scenario: A specific test case with predefined steps.
- Given-When-Then: The steps defining the test flow.
- Scenario Outline: A parameterized test case for data-driven testing.
- Examples: A table providing multiple input values for a Scenario Outline.
- 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 theExamples
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. π