Behavior Driven Development (BDD) is an agile software Development technique that encourages collaboration between developers, QA team, non-technical persons in software development. RSpec and cucumber are the BDD tools for Ruby developers.
1. Cucumber
Cucumber works with Ruby, Java, .Net, Flex or web applications written in any language. It has been translated to over 30 spoken languages. Aslak Hellesoy is the founder of cucumber, it is the rewrite of RSpec’s story runner. Cucumber is written in ruby to test codes written in ruby, java, .Net, etc. Gherkin is the language that Cucumber understands. It executes the functional description written in plain text.
Cucumber is available in gem. So you install it by executing,
gem install cucumber
Main part of cucumber are
- Features
- Step definitions
We write features in Gherkin.
Gherkin.
Like ruby executable interprets ruby code in .rb file, cucumber executable interprets Gherkin in .feature file.Gherkin keyword are translated into 35 different languages.So you can write feature in in your native language. To enable this you can put a header like this ( # language: pt) in .feature file. cucumber –i18n help will show the available language lists.It have some keywords:- Feature,Background,Scenario,Scenario outline,Scenarios,Given,When,Then,And etc
Run rails generate cucumber:install, will create following files.
create features/step_definitions
create features/step_definitions/web_steps.rb
create features/support
create features/support/paths.rb
create features/support/selectors.rb
create features/support/env.rb
Run cucumber
Using the default
profile…
0 scenarios
0 steps
0m0.000s
1.1 Features
Like user stories, Cucumber features have a title and a brief narrative. For this create a file in folder ‘features’ with file-extension .feature. Title of cucumber is few words that represents what we do in system.
eg:- feature/add_two_no.feature
Feature: Add two numbers
As a user
I type two number and click sum button
So that goto add page
Here “Add two numbers” is title and all remaining thing is story.
Next part in feature is Scenarios. we write it in the same .feature file. Each feature may have multiple scenarios, scenario is composed of Given-When-Then.
Given indicates that a particular condition is true.
When in scenario indicates events.
Then indicates the outcome of the event.
Scenario: Add two number
Given a user on home page
When I fill no1 with 3 and no2 with 5
Then go to add page
1.2 Step Definitions
Step definitions are Cucumber’s equivalent of method definitions or function declarations in a conventional programming language. When we execute the command “cucumber”, you will get
1 scenario (1 undefined)
3 steps (3 undefined)
0m2.020s
You can implement step definitions for undefined steps with these snippets:
Given /^a user on home page$/ do
pending # express the regexp above with the code you wish you had
end
When /^I fill (.+) and (.+) and click (.+) button$/ do |no1, no2,add|
pending # express the regexp above with the code you wish you had
end
Then /^shows sum of numbers$/ do
pending # express the regexp above with the code you wish you had
end
Let us move to the implementation of step definition. Create a ruby file in features/step_definitions/.
I add following into step definition to features/step_definitions/add_no.rb
Given /^a user on (.+)$/ do |page|
visit path_to(page)
end
And edit the file features/support/paths.rb,in this file we declare all routing information. Add two line below, this means “home page” is pointing to application root.
features/support/paths.rb
when /home page/
‘/’
Run cucumber command.
Using the default profile…
1 scenario (1 pending)
3 steps (1 skipped, 1 pending, 1 passed)
0m2.233s
This means that we have one scenario have 3 steps, among them 1 skipped, 1 pending, 1 passed.Created an interface like this in ROR.
And edit the features/step_definitions/add_no.rb and changed features/support/paths.rb file
features/step_definitions/add_no.rb
Given /^a user on (.+)$/ do |page|
visit path_to(page)
end
When /^I fill (.+) and (.+) and click (.+) button$/ do |no1, no2,add|
visit home_url
fill_in “No1”, :with => no1
fill_in “No2”, :with => no2
click_button “Add”
end
Then /^goto (.+) and shows result$/ do |page|
visit path_to(page)
end
And edit features/support/paths.rb
when /home page/
‘/’
when /add page/
add_url
Run cucumber, will show following output
Feature: Add two numbers
As a user
I type two number and click sum button
So that give sum of two number
Scenario: Add two number # features/add_no.feature:6
1 scenario (1 passed)
3 steps (3 passed)
0m2.728s
The output shows that 3 steps are passed.
Conclusion
This post covered the basics of cucumber. Cucumber testing having several advantages. It provide writing high level behavioural testing. Cucumber allows the developer/tester to write behaviour of system in plain text.