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
:
Feature: Adding a taskBackground:Given a projectScenario: I can add and change the priority of a new taskWhen I visit the project pageAnd I complete the new task formThen I am back on the project pageAnd I see the new task is last in the listWhen I click to move the new task upThen I am back on the project pageAnd 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
, ...