What is Cucumber Feature File & Step Definition? (Example)

โšก Smart Summary

Cucumber feature files describe behaviour in plain-language Gherkin, while step definitions hold the code that runs each line. Every Cucumber project needs both, stored in a fixed folder layout beneath a features directory.

  • ๐Ÿ”˜ Two files: A feature file states what should happen; a step definition file states how the automation makes it happen.
  • โ˜‘๏ธ Gherkin keywords: Feature, Scenario, Given, When and Then give a scenario its structure and its readable shape.
  • โœ… No colons on steps: Feature and Scenario take a colon, but Given, When and Then must not, or the step is ignored.
  • ๐Ÿงช Data sets: Scenario Outline plus an Examples table replays one scenario against many rows of input.
  • ๐Ÿ› ๏ธ Shared setup: Background lifts repeated Given steps out of every scenario in a feature.
  • ๐Ÿ“Š Matching: Cucumber ignores the keyword when matching, so two steps may never share the same text.

Cucumber feature file and step definition explained with an example

For every cucumber project there is a single directory at the root of the project named โ€œfeaturesโ€œ. This is where all of your cucumber features will reside. In this directory you will find additional directories, which is step_definition and support directories.

That layout matters, because Cucumber discovers your files by convention rather than by configuration. A feature file that sits outside the features directory is simply never read.

What is โ€œFeature Fileโ€?

Features file contain high level description of the Test Scenario in simple language. It is known as Gherkin. Gherkin is a plain English text language.

Cucumber Feature File consist of following components โ€”

  • Feature: A feature would describe the current test script which has to be executed.
  • Scenario: Scenario describes the steps and expected outcome for a particular test case.
  • Scenario Outline: Same scenario can be executed for multiple sets of data using scenario outline. The data is provided by a tabular structure separated by (I I).
  • Given: It specifies the context of the text to be executed. By using datatables โ€œGivenโ€, step can also be parameterized.
  • When: โ€œWhenโ€ specifies the test action that has to performed.
  • Then: The expected outcome of the test can be represented by โ€œThenโ€.

One file holds one Feature. The scenarios below it are the concrete examples that illustrate the feature, and each scenario follows the same rhythm: describe a starting context, describe an event, describe the expected result.

Next, we will learn how to write Feature File in Cucumber with example.

Sample Feature File Example:

The original example published with this tutorial is reproduced below exactly as written.

Feature: Visit career guide page in career.guru99.com
Scenario: Visit career.guru99.com
Given: I am on career.guru99.com
When: I click on career guide menu
Then: I should see career guide page

โš ๏ธ Correction: the example above places a colon after Given, When and Then. Gherkin puts a colon after Feature and Scenario only. A step keyword followed by a colon does not parse as a step, so Cucumber silently ignores the line. The corrected file reads as follows.

Feature: Visit career guide page in career.guru99.com

  Scenario: Visit career.guru99.com
    Given I am on career.guru99.com
    When I click on career guide menu
    Then I should see career guide page

Indentation is not required by the parser, but two spaces per level is the conventional style and makes a long feature far easier to scan. The animation below shows a feature file being run and each step turning green as it passes.

Animation of a Cucumber feature file running with each Gherkin step reported in turn

What is โ€œStep Definitionโ€?

Step definition maps the Test Case Steps in the feature files(introduced by Given/When/Then) to code. It which executes the steps on Application Under Test and checks the outcomes against expected results. For a step definition to be executed, it must match the given component in a feature. Step definition is defined in ruby files under โ€œfeatures/step_definitions/*_steps.rbโ€.

The match is made on the text of the step alone. Cucumber does not take the keyword into account when it looks for a definition, which is why two steps in the same suite may never carry identical text even if one is a Given and the other a Then.

Example for Step Definition: Here we will use above Cucumber Feature File example of browsing career.guru99.com. We will use features like โ€œWhen, Then, Given โ€ as shown in the below Cucumber Feature File examples.

Step 1:

Given (/^ I am on career.guru99.com$/) do

Browser.goto "http://career.guru99.com" -This will visit career.guru99 on browser

end

Step 2:
When (/^ click on career guide menu$/) do

Browser.text (:name, " career guide" ).click โ€“ This will click "career guide menu"

end

Step 3:

Then (/^ I should see career guide page$/) do

Browser.goto "http://career.guru99.com/category/career-guide/" - It will visit "career guide page"

end

โš ๏ธ A note on languages: the definitions above are Ruby, which is where Cucumber started and which is why the folder is named step_definitions and the files end in _steps.rb. The same feature file drives step definitions written in Java, JavaScript, Python or C# without a single change to the Gherkin. In Cucumber-JVM the regular expression moves into an annotation on a method:

@Given("I am on career.guru99.com")
public void i_am_on_the_career_page() {
    driver.get("http://career.guru99.com");
}

Whichever language you choose, the step definition is the only place implementation detail belongs. Keeping selectors and HTTP calls out of the feature file is what lets a non-programmer read the scenario. The first Cucumber script walkthrough builds this pairing end to end.

Scenario Outline and Examples in a Feature File

Copying a scenario three times to change one value is the most common way a feature file becomes unreadable. Scenario Outline replaces the copies with a template, and an Examples table supplies the values.

Parameters are written inside angle brackets and must match a column heading in the table below them. Cucumber substitutes the values before it looks for a matching step definition, so the definitions themselves need no special handling.

Scenario Outline: Search the career guide
  Given I am on career.guru99.com
  When I search for "<keyword>"
  Then I should see the <section> section

  Examples:
    | keyword   | section       |
    | interview | Interview Tips|
    | resume    | Resume Guide  |

A few points are worth remembering when using outlines:

  • The outline runs once per data row. The first row is the header and is never executed.
  • A Scenario Outline must be followed by at least one Examples section, otherwise nothing runs.
  • The table pipes do not need to line up, although aligning them keeps the file readable.
  • Keep the table small. A table with thirty rows is usually a sign the data belongs in a data-driven test instead.

Background, And, But and the Other Gherkin Keywords

Beyond the six components listed earlier, Gherkin has a handful of keywords that keep longer feature files readable.

  • Background โ€” a block of Given steps that runs before every scenario in the feature. It removes the repetition of writing the same precondition at the top of each scenario.
  • And and But โ€” replacements for a repeated Given, When or Then. They read better and behave identically; Cucumber matches on the step text either way.
  • Rule โ€” an optional grouping, added in Gherkin 6, that collects the scenarios illustrating one business rule.
  • Doc Strings โ€” a block of text fenced by three double quotes, passed to the step definition as a single argument when one line is not enough.
  • Data Tables โ€” pipe-delimited rows attached to a single step, used to hand a list of values to that step.
  • Tags โ€” labels beginning with an at sign, placed above a Feature or Scenario so that a subset can be selected at run time.
  • Comments โ€” a line beginning with a hash. Comments are only allowed at the start of a line, and Gherkin has no block comment.

Put together, a feature file with shared setup looks like this:

Feature: Career guide navigation

  Background:
    Given I am on career.guru99.com

  Scenario: Open the career guide menu
    Given I am signed in
    And my profile is complete
    When I click on career guide menu
    Then I should see career guide page
    But I should not see the admin panel

Common Feature File Mistakes and How to Fix Them

Most Cucumber problems reported by beginners are not framework faults. They are small violations of the Gherkin rules that fail quietly.

Mistake What happens Fix
Colon after Given, When or Then The line is not recognised as a step and is skipped Use a colon after Feature and Scenario only
Two steps with identical text Cucumber treats them as duplicates because the keyword is ignored Reword one step so the two read differently
Feature file outside the features directory The file is never discovered and no test runs Keep every .feature file under the features folder
Fifteen steps in one scenario The scenario stops working as documentation Aim for three to five steps and push detail into the step definition
Then asserts on a database row The test passes while the user-visible behaviour is broken Assert on observable output such as the page or the response
Scenario Outline with no Examples table The template never executes Add an Examples section directly beneath the outline

Fixing these before the suite grows is far cheaper than fixing them afterwards, because every scenario written in the meantime copies the same shape. Once the feature files behave, the next step is wiring the project up โ€” see the Cucumber installation walkthrough and the wider automation testing series.

FAQs

No. A .feature file may declare exactly one Feature, although it can hold as many scenarios as you like. If you need a second feature, create a second file โ€” that constraint is what keeps the folder navigable as a suite grows.

It does not. Gherkin is localised into more than seventy spoken languages. Adding a language header on the first line of the file switches the keywords, so a team can write scenarios in the same language its domain experts actually speak.

It holds everything that is not a step definition: environment setup, browser configuration, hooks that run before or after a scenario, and shared helper code. Keeping it separate stops step definition files from turning into a dumping ground.

Cucumber reports the step as undefined rather than failed, and prints a suggested snippet you can paste into a step definition file. The scenario is marked as not passing, so an undefined step never quietly counts as a success.

A tag placed above a Feature or Scenario lets the runner select a subset at execution time, independent of the folder structure. Teams commonly tag a smoke set, a slow set and a work-in-progress set, then run only what a given pipeline stage needs.

Large language models draft Gherkin from an acceptance criterion well, and generate the matching definition skeletons faster still. Review the output for duplicated step text and for scenarios that leak implementation detail, which is where generated Gherkin usually goes wrong.

Very. Given an open feature file, Copilot infers the regular expression and the method signature for an undefined step and fills in the body from surrounding code. Treat it as a first draft; the assertions it proposes still need a human check.

Yes, and that is the point of the design. Definitions live in a shared folder and are matched by text, not by file, so a login step written once is reused by every feature that needs it. Reuse is why duplicate step text is forbidden.

Summarize this post with: