Hack hack hack...

An open journal-- some of it written for you, but most of it is for me.

Cucumber

Cucumber

Cucumber is a BDD tool that supports Outside-in development by running plain text features or user stories as acceptance tests.

Features

Let’s create our first feature. The anatomy of a feature is the following:

1
2
3
4
Feature: [feature]
  In order to [business value]
  As a [role]
  I want to [some action]

Notice how a feature includes who, what and most importantly: why.

For example:

1
2
3
4
Feature: Manage Articles
  In order to make a blog
  As an author
  I want to create and manage articles

Scenarios

There will be many scenarios accompanying a feature. Scenarios will follow the four stages of testing: setup, exercise, verify and teardown. A simple scenario looks like this:

1
2
3
Given [context]
When I do [action]
Then I should see [outcome]
  • The Given step is where you set up the context of your scenario. Every scenario starts with a blank slate, so it is important to create a state in your application for example by creating data in the database, or by navigating to a specific page.
  • The When step is where you exercise the application in order to accomplish what needs testing. This is usually where you might fill in forms, press buttons, click links, or otherwise interact with the system in some way.
  • The Then step is where you verify the result, and it’s where we check that the correct pages are rendered, that we see a success or error message, or anything that could help us verify that the prior action was successful.
  • The teardown, is taken care of by cucumber automatically.

For example:

1
2
3
4
5
Scenario: Articles List
  Given I have articles titled Pizza, Breadsticks
  When I go to the list of articles
  Then I should see "Pizza"
  And I should see "Breadsticks"
1
2
3
4
5
6
7
8
9
Scenario: User signs up with valid data
  When I go to the sign up page
  And I fill in "Email" with "email@person.com"
  And I fill in "Username" with "user"
  And I fill in "Password" with "password"
  And I fill in "Confirm password" with "password"
  And I press "Sign up"
  Then I should see "instructions for confirming"
  And a confirmation message should be sent to "email@person.com"

We can also test a user attempting to sign up without filling in all the fields:

1
2
3
4
5
6
7
Scenario: User tries to sign up without a twiddr name
  When I go to the sign up page
  And I fill in "Email" with "email@person.com"
  And I fill in "Password" with "password"
  And I fill in "Confirm password" with "password"
  And I press "Sign up"
  Then the "Username" field should have the "can't be blank" error

Tags

cucumber --tags @billing --tags @important -> Runs the first scenario (Scenarios with @important AND @billing) cucumber --tags @billing,@important -> Runs both scenarios (Scenarios with @important OR @billing)

Comments