...

/

Writing Cucumber Features

Writing Cucumber Features

Learn about Cucumber features, integration testing with Cucumber, and ordering and writing Cucumber steps.

Cucumber features

In Cucumber, we write tests as a series of steps using a minimal language called Gherkin. An individual Cucumber test is called a Scenario, and a group of them is called a Feature.

Integration testing with Cucumber

Let’s take the Capybara integration test from the last section and convert it to Cucumber. Cucumber feature files go in the features directory and typically end in .feature. Here is features_add_task.feature:

Press + to interact
Feature: Adding a task
Background:
Given a project
Scenario: I can add and change the priority of a new task
When I visit the project page
And I complete the new task form
Then I am back on the project page
And I see the new task is last in the list
When I click to move the new task up
Then I am back on the project page
And the new task is in the middle of the list

This file has three parts. The Feature declaration is at the top. The Cucumber file needs to have one, but the description there is strictly for humans. We can put anything here we want. Gherkin is whitespace-sensitive, so anything that goes beyond that top line needs to be indented.

Ordering tasks

The next section is Background, which is optional. It might not be clear from the preceding code, but the Background line is indented subordinate to Feature. In Cucumber, Background is like Minitest’s setup and RSpec’s before, ...