Cucumber: behaviour-driven awesomeness
The bumpy carpet problem
Have you ever asked your web developer to implement something, only to realise that something else on your website went wrong while they were doing it? Cucumber helps us to keep these situations to a minimum.
You may have heard of automated testing. It is common practice among programmers to write code to test their application code before the application code is written. The advantages of doing so are profound:
- the programmer is forced to consider the need for a feature before writing code for it
- the programmer must consider how a particular feature behaves from the point of view of a user
- the programmer has an easy way to check whether a feature is still working
Cucumber lets the product owner (you) in on this activity, by allowing tests to be written in plain English. These tests take the form of stories.
Stories have a secondary purpose of documenting the features of a website, and can be useful for non-programmers as a future reference for how the website works.
The story
A user story describes:
- A person (or role)
- The action that the person wishes to take
- The desired outcome of the action
For example, the following user story describes a new feature for a website that allows content writers to add news articles to the homepage.
Feature: Article writing
- As a content writer
- I want to add news articles to the homepage
- So that I can keep visitors up to date with the business
Scenario: Write article
- Given I am a content writer
- And I am on the admin page
- When I follow "Write News Article"
- And I fill in "Article Title" with "Hello, World!"
- And I fill in "Article Body" with "Some news article"
- And I press "Create"
- And I go to the homepage
- Then I should see "Hello, World!"
Translation
Suppose we have come to an initial agreement (nothing is final) about the needs of the user of the website. Camel Punch can now begin to write code that translates the description of the feature written in English into an automated test that ensures the website does as the description says.
An automated test in this sense is a computer program that uses the website as a visitor would, and complains when a particular feature does not work.
So, the translation into code of When I follow "Write news article" would tell the computer running the test to visit the site and click on a link called "Write news article". If such a link did not exist, the test would result in failure, and the programmer would know that they had work to do.
Failure
Running the test in the example for the first time results in a horrible mess of failures. This is because the code that implements the feature has not been written. Ensuring that a test fails in an appropriate way when we know the feature is not there protects us against false positives. That is, the test resulting in true and the feature not being in place.
Implementation
We are satisfied that the test is failing appropriately, so we type away and implement the feature (that's the easy bit). We then re-run the test and all other tests that have been written. This step ensures not only that the new feature works, but that the existing features of your site are not adversely affected by the changes introduced by the new feature.
It is this last step that helps us to catch bugs before they go live and avoid the 'bumpy carpet problem'.
You may also be interested in:
- the Cucumber homepage
- a description of our web development process
- a description of what web development is