Thursday, 10 October 2013

Cucumber and Ruby, Introduction


I was using RSpec, as my testing framework , as part of my daily Rails development since couple of years.  Recently I happened to try Cucumber. In that process I decided to use Cucumber Ruby than Rails.

Cucumber ?
cucumber image
 Lets us look at the exiting business practice.

Who owns the business ?
Some one who have an idea.

Then why do we need software developers or “engineers” ?

Why can not business people do development as well ? They can express their thoughts in some language, English, Hindi, Spanish etc ...

Instruction to computer:
“Let there be a Robot that can move in one direction”

magic cap

Nothing happens .. Glad that  our computers does not produce software based on instructions in spoken languages, yet. (fortunately !).

As long as this is the case, we the software engineers are safe :)


What to expect from Cucumber

  1. Cucumber lets software development teams describe how software should behave in plain text, spoken languages. 
  2. Reduces the gap between business requirement and developed product( Agile to the rescue). 
  3. Develop only what is required
  4. Documentation
  5. Acceptance test. Some defined way of proving that developer met the expectation.


TDD - In general
TDD-digram
Cucumber and TDD

Cucumber and TDD diagram
I am not sure if business people would get involved as much I expected here. 

Cucumber steps

cucumber steps - diagram


How does  a feature file look like ?

Feature: Serve coffee
  In order to earn money
  Customers should be able to
  buy coffee at all times

  Scenario: Buy last coffee
    Given there are 1 coffees left in the machine
    And I have deposited 1$
    When I press the coffee button
    Then I should be served a coffee
  Scenario: Buy first coffee
    Given there are 10 cofee left
    When I press the coffee button
    Then I should be served a coffee

    And number of coffee became 9


  • Notice indentation.
  • A line starting with the keyword Feature followed by free indented text starts a feature. A feature usually contains a list of scenarios. You can write whatever you want up until the first scenario, which starts with Scenario.
  • Every scenario consists of a list of steps, which must start with one of the keywords Given, When, Then, But or And.



Cucumber supports over 40 spoken languages and the number is steadily growing.


A # language: header on the first line of a feature file tells Cucumber what spoken language to use – for example # language: fr for French. If you omit this header, Cucumber will default to English (en).

feature file snapshot


Gherkin


Gherkin


Cucumber lets software development teams describe how software should behave in plain text.
The text is written in a business-readable domain-specific language -
The language that Cucumber understands is called Gherkin -
Gherkin is the language that Cucumber understands.
It is a Business Readable, Domain Specific Language that lets you describe software’s behavior
 without detailing how that behavior is implemented.()

Gherkin’s grammar is defined in the Treetop grammar
    Single Gherkin source file contains a description of a single feature.
Source files have .feature extension.
Like Python and YAML, Gherkin is a line-oriented language that uses indentation to define structure.
Line endings terminate statements (eg, steps).

Either spaces or tabs may be used for indentation (but spaces are more portable). Most lines start with a keyword.


Example
Robot


Moving robot, but only in one direction.

Accept 2 params, step and direction

Ex: 'F', 10 OR 'B', 20


Moves either forward or back ward, Initially at the center (0).


Installing cucumber
1.
$ gem install cucumber
Building native extensions.  This could take a while...
Successfully installed builder-3.2.2
Successfully installed diff-lcs-1.2.4
Successfully installed multi_json-1.8.0
Successfully installed gherkin-2.12.1
Successfully installed multi_test-0.0.2
Successfully installed cucumber-1.3.8
6 gems installed

2.
$cucumber
No such file or directory - features. Please create a features directory to get started. (Errno::ENOENT)

3.
Create directory and then create 'features/robot.feature'
Feature: Robot in Motion
  In order to move my robot
  As an owner
  I want the ability to create and move based on instructions
  Scenario: Create a robot
    Given Robot created
    When I check the position
    Then Robot should be at 0
  Scenario: Move forward
    Given Robot is at 0
    When I enter 'F', 10
    And I check the position
    Then Robot should be at 10

4.
Run again and see it fails.

5.
Now create 'features/step_definitions/robot_steps.rb' as

require 'robot'

Given(/^Robot created$/) do
  @robot = Robot.new
  # pending # express the regexp above with the code you wish you had
end

When(/^I check the position$/) do
  @position = @robot.position
  # pending # express the regexp above with the code you wish you had
end

Then(/^Robot should be at (\d+)$/) do |arg1|
  @position.should == arg1.to_i
  # pending # express the regexp above with the code you wish you had
end

Given(/^Robot is at (\d+)$/) do |arg1|
  @robot = Robot.new
  # pending # express the regexp above with the code you wish you had
end

When(/^I enter 'F', (\d+)$/) do |arg1|
        @robot.move('F', arg1.to_i)
  # pending # express the regexp above with the code you wish you had
end

6. This will again fail, create the actual robot class now - $ robot.rb

class Robot
        attr_accessor :location

        def initialize
                @location = 0
        end

        def move(dir, step)
                if dir=='F'
                        @location = @location + step
                elsif dir=='B'
                        @location = @location - step
                end
        end

        def position
                @location
        end
end

 7. $cucumber -f html > report.html 

This gives you some level of documentation, something like ...

cucumber report


Ref
http://cukes.info/
https://github.com/cucumber/cucumber/wiki
https://github.com/cucumber/cucumber/wiki/Given-When-Then


No comments:

Post a Comment