Testing a Sample Cucumber Feature Using Ruby

There are a few examples I have found online, but it took quite a while for me to get all the information I needed to actually make this work. So here is everything that I was able to find to get Cucumber up and running.

Start by creating a new folder for the project. Doesn’t really matter where you put this folder, but try keep things organised. Create a new folder called features and inside make a “sample”.feature file that contains the feature which is just a simple description

Here is the most common sample that has been floating around the web.

sample.feature

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#encoding: utf-8
Feature: Cucumber scenario to chop a cucumber into halves
I have a cucumber that is Xcm long that i want to cut in half
I should have 2 cucumbers after i have cut the cucumber in half
Each piece should be 15cm long to make equal halves
Scenario: Cutting vegetables
Given a cucumber that is 30 cm long
When I cut it in halves
Then I have two cucumbers
And both are 15 cm long
#encoding: utf-8 Feature: Cucumber scenario to chop a cucumber into halves I have a cucumber that is Xcm long that i want to cut in half I should have 2 cucumbers after i have cut the cucumber in half Each piece should be 15cm long to make equal halves Scenario: Cutting vegetables Given a cucumber that is 30 cm long When I cut it in halves Then I have two cucumbers And both are 15 cm long
#encoding: utf-8
Feature: Cucumber scenario to chop a cucumber into halves
I have a cucumber that is Xcm long that i want to cut in half
I should have 2 cucumbers after i have cut the cucumber in half
Each piece should be 15cm long to make equal halves

Scenario: Cutting vegetables
Given a cucumber that is 30 cm long
When I cut it in halves
Then I have two cucumbers
And both are 15 cm long

So the first thing you can do is attempt to run this file. You should get back errors, but at least you now know that everything is set-up and working correctly.

Cucumber sample.feature

This will give back 4 errors because the steps for this have not been implemented yet. Copy and paste the suggested empty stubs that cucumber returns and paste them into a .rb file. If you are using a standard windows command shell, you can right click on the window and click “mark”. This allows you to highlight text in the command shell window. This file needs to be put inside a folder called step_definitions. eg. myproject/features/step_definitions/steps.rb

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Given(/^a cucumber that is (\d+) cm long$/) do |arg1|
pending # express the regexp above with the code you wish you had
end
When(/^I cut it in halves$/) do
pending # express the regexp above with the code you wish you had
end
Then(/^I have two cucumbers$/) do
pending # express the regexp above with the code you wish you had
end
Then(/^both are (\d+) cm long$/) do |arg1|
pending # express the regexp above with the code you wish you had
end
Given(/^a cucumber that is (\d+) cm long$/) do |arg1| pending # express the regexp above with the code you wish you had end When(/^I cut it in halves$/) do pending # express the regexp above with the code you wish you had end Then(/^I have two cucumbers$/) do pending # express the regexp above with the code you wish you had end Then(/^both are (\d+) cm long$/) do |arg1| pending # express the regexp above with the code you wish you had end
Given(/^a cucumber that is (\d+) cm long$/) do |arg1|
pending # express the regexp above with the code you wish you had
end

When(/^I cut it in halves$/) do
pending # express the regexp above with the code you wish you had
end

Then(/^I have two cucumbers$/) do
pending # express the regexp above with the code you wish you had
end

Then(/^both are (\d+) cm long$/) do |arg1|
pending # express the regexp above with the code you wish you had
end

Rerun the same feature and the result will be different. It should give an error for the first step telling you there has been no code implemented and then it will skip the rest of the tests.

Go back to the steps.rb file and now you can add some ruby code to the stubs that will successfully do what was described in the above scenario. Once this code has been added all of the codes will pass and uoi will be looking at a nice screen of green.

steps.rb

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Given /^a cucumber that is (\d+) cm long$/ do |length|
@cucumber = {:color => 'green', :length => length.to_i}
end
When /^I (?:cut|chop) (?:it|the cucumber) in (?:halves|half|two)$/ do
@choppedCucumbers = [
{:color => @cucumber[:color], :length => @cucumber[:length] / 2},
{:color => @cucumber[:color], :length => @cucumber[:length] / 2}
]
end
Then (/^I have two cucumbers$/) do
@choppedCucumbers.length == 2
end
Then /^both are (\d+) cm long$/ do |length|
@choppedCucumbers.each do |cuke|
cuke[:length] == length.to_i
end
end
Given /^a cucumber that is (\d+) cm long$/ do |length| @cucumber = {:color => 'green', :length => length.to_i} end When /^I (?:cut|chop) (?:it|the cucumber) in (?:halves|half|two)$/ do @choppedCucumbers = [ {:color => @cucumber[:color], :length => @cucumber[:length] / 2}, {:color => @cucumber[:color], :length => @cucumber[:length] / 2} ] end Then (/^I have two cucumbers$/) do @choppedCucumbers.length == 2 end Then /^both are (\d+) cm long$/ do |length| @choppedCucumbers.each do |cuke| cuke[:length] == length.to_i end end
Given /^a cucumber that is (\d+) cm long$/ do |length|
@cucumber = {:color => 'green', :length => length.to_i}
end

When /^I (?:cut|chop) (?:it|the cucumber) in (?:halves|half|two)$/ do
@choppedCucumbers = [
{:color => @cucumber[:color], :length => @cucumber[:length] / 2},
{:color => @cucumber[:color], :length => @cucumber[:length] / 2}
]
end

Then (/^I have two cucumbers$/) do
@choppedCucumbers.length == 2
end

Then /^both are (\d+) cm long$/ do |length|
@choppedCucumbers.each do |cuke|
cuke[:length] == length.to_i
end
end

Related Articles

Related Questions

Can I Recover Video from a Formatted CD?

I'm in a bit of a bind here. I bought a portable DVD player to watch old CDs and DVDs, especially a particular CD...

Can multiple devices share the same IP and cause connection issues?

My parents have banned my devices' IP addresses from our home WiFi, so to get around that, I changed the IP addresses of my...

Why is ChatGPT So Useless After Canceling My Subscription?

I've been using ChatGPT for a while in the free version, and last month I decided to try the Plus version because I had...

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Latest Tools

xAI Grok Token Calculator

This tool is a simple xAI Grok token calculator that helps you estimate the number of tokens your input text might consume when working...

DeepSeek Token Calculator

This tool is a straightforward DeepSeek token calculator, created to help you estimate how many tokens your text may use when working with DeepSeek...

Google Gemini Token Calculator

This tool is a simple Google Gemini token calculator, designed to help you estimate how many tokens your text might use with models in...

Meta LLaMA Token Calculator

This tool is a basic Meta LLaMA token calculator, designed to give you a fast estimate of how many tokens your input text might...

Anthropic Claude AI Token Calculator

This tool is a simple Claude token calculator, built to help you quickly estimate token usage when working with Anthropic’s Claude models. It’s an...

OpenAI Token Calculator

This tool is a simple OpenAI token calculator, web-based utility designed to help you quickly estimate the number of tokens in your text when...

Latest Posts

Latest Questions