Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
This is now an archive web site.
Some is still relevant as at February 2026 but some is purely of historic interest.


Lisa and I (Graham) are now fully retired from IT training.We have made many friends over 30 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Apache, Linux and Solaris/SunOS too. Our training notes are out of date, but with upward compatability some examples remain operational and relevant. You are welcome to make use of them "as seen", at your own risk. We now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We remain active, enjoying the times that we are retired but still healthy enough in mind and body to do things!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
Second step Cucumber and Gherkin - beyond Hello World (written 2015-01-03)

 
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]
(written 2015-01-03)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
R221 - Ruby - Introduction to Cucumber
  [4380] Behaviour Driven Development / Ruby and Cucumber - (2015-01-02)
  [4381] Installing Cucumber (Ruby) - (2015-01-03)
  [4383] Improved test in Cucumber with RSpec - (2015-01-03)
  [4384] Installing Cucumber on Ubuntu - cannot load such file -- mkmf (LoadError) message - (2015-01-04)
  [4386] Guide exercise to help you learn Gherkin, Cucumber and Rspec - (2015-01-06)
  [4551] Testing your new class - first steps with cucumber - (2015-10-23)
  [4552] Scenario outlines - tables of values to test - in Gherkin / Cucumber - (2015-10-23)


Back to
Installing Cucumber (Ruby)
Previous and next
or
Horse's mouth home
Forward to
Improved test in Cucumber with RSpec
Some other Articles
A booking that looks too good to be true? It probably is too good to be true!
Second step Cucumber and Gherkin - beyond Hello World
Well House Consultants / Well House Manor - Prices for 2015
What FGW passengers want to talk about / and PHP programming to find out
Designing a base class and subclasses, and their extension, in C++
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. So much so, and it's so long ago that we are retired

Link to Ezine home page (for reading).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2026: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: +44 (0)1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net

PAGE: https://www.wellho.info//mouthwork/4382_Second-step-Cucumber-and-Gherkin-beyond-Hello-World.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb