Main Content

Second step Cucumber and Gherkin - beyond Hello World

Archive - Originally posted on "The Horse's Mouth" - 2015-01-03 12:00:35 - Graham Ellis

 
Articles in this "Introduction to Cucumber" series:
[link] - Installing Cucumber for Ruby
[link] - Hello Test Cucumber World
[link] - First real tests / second example
[link] - Improved tests with RSpec
 




Let's look at an example where I calculate a train's capacity in a class, based on the number of seats and the Department for Transport's specification of 40% overloading for local trains which stop at least every half hour.

I'll define my feature - second/capacity.feature:

  Feature: Check that a train's capacity is 1.4 x number of seats
  
  Scenario: Calculating people that can be carried
  Given I have a 3 coach train with 75 seats per carriage
  When I ask what the capacity is
  Then I should be told it is 315


When I run that, Gherkin / Cucumber gives me a template for the test code:



which I can paste directly into a ruby steps file:

  WomanWithCat:cuc grahamellis$ mkdir second/step_definitions
  WomanWithCat:cuc grahamellis$ cat > second/step_definitions/second_steps.rb
  Given(/^I have a (\d+) coach train with (\d+) seats per carriage$/) do |arg1, arg2|
    pending # express the regexp above with the code you wish you had
  end
  
  When(/^I ask what the capacity is$/) do
    pending # express the regexp above with the code you wish you had
  end
  
  Then(/^I should be told it is (\d+)$/) do |arg1|
    pending # express the regexp above with the code you wish you had
  end
  WomanWithCat:cuc grahamellis$


Running that, I'm told that the given clause is pending and (as a result of it being incomplete) the when and then clauses are skipped.



You'll note keywords Given, When, Then, And and But ... and it turns out they're all 'linking words' which you can replace with "* bullets if you prefer.

Note also that each scenario (if you have multiple scenarios) is going to be independent of the other scenarios - each must be built from scratch as you won't have data transferred across. Keeping the scenarios independent in this way is a big help later on - you may feel it's a bit much to setup every time (but there are ways of sharing code).

I can now implement my tests (replacing pending with what the code should be to run the tests, and then implement my class, and the result is my final scenario (in Gherkin) with the test patterns it's helped me generate and the class code that's been used to satisfy those test patterns:



The steps file has become:

  Given(/^I have a (\d+) coach train with (\d+) seats per carriage$/) do |arg1, arg2|
    @mytrain = Train.new(arg1.to_i,arg2.to_i)
  end
  
  When(/^I ask what the capacity is$/) do
    @capacity = @mytrain.getcapacity
  end
  
  Then(/^I should be told it is (\d+)$/) do |arg1|
    if @capacity == arg1.to_i
      pass
    else
      fail
    end
  end


Note that the incoming variables from the regular expression match are all strings, and need to be converted to integers or floats as the case my be before they're passed into the methods / class we're testing.

And the class that we're testing:

  class Train
    def initialize (coaches,seats)
      @coaches = coaches
      @seats = seats
    end
    def getcapacity
      return (@coaches * @seats * 1.4).to_i
    end
  end


For this example ... our code
1. Feature file [here]
2. Step implementation [here]
3. Code to implement the behaviour [here]