# Running Cucumber with a Rails Rake task

**URL:** <https://www.ruby-forum.com/t/running-cucumber-with-a-rails-rake-task/151255>\
**Category:** RSpec\
**Created:** [October 18, 2008, 10:33pm UTC](https://www.ruby-forum.com/t/running-cucumber-with-a-rails-rake-task/151255 "2008-10-18T22:33:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![benemson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a8b319/32.png) [@benemson](https://www.ruby-forum.com/u/benemson)\
**Post date:** [October 18, 2008, 10:33pm UTC](https://www.ruby-forum.com/t/running-cucumber-with-a-rails-rake-task/151255/1 "2008-10-18T22:33:11Z")

</div>

Hi All

I’ve just started experimenting with Cucumber and its great.  
However I wasn’t quite sure how I should integrate it into my Rails  
application.

What I would like to do is set my Rails app up so that I can run a  
Cucumber rake task using the standard Rails Rake system.

I’ve looked through the example source and understand how the Calculator  
demo Rake file works, but I’m not sure I’ve set it up correctly in my  
Rails app.

This is what I’ve done.

Created a new file:  
my\_app/lib/tasks/cucumber.rake

I experimented trying to use the following Cucumber rake task but could  
not get my steps to load my model code as I was hoping that I could tell  
Cucumber what my Rails lib files were:

Cucumber::Rake::Task.new do |t|  
t.libs = ["#{RAILS\_ROOT}/config/environment"] # This doesn’t seem to  
work  
Dir.chdir("#{RAILS\_ROOT}/stories/")  
profile = ENV[‘PROFILE’] || ‘default’  
t.cucumber\_opts = “–profile #{profile}”  
end

In the end I opted to just call Cucumber from the commandline:

desc “run stories”  
task :runner do  
cucumber = “cucumber stories”  
sh cucumber  
end

And then add a helper file to my stories directory and tell my steps to  
load the Rails environment, such that my\_steps.rb looks like this:

require ‘spec’  
require File.expand\_path(File.dirname( **FILE** ) + “/…/…/helper”)

# then all my step code…

And my helper.rb file looks like this:

ENV[“RAILS\_ENV”] = “test”  
require File.expand\_path(File.dirname( **FILE** ) +  
“/…/config/environment”)

So is this the correct way for adding a Rake task to the main Rails Rake  
file so that it will run all my stories?  
Should I still include the old all.rb file in the stories directory so  
that I can run it without using Rake?

Many thanks

BE…

---

<div class="post-metadata">

**Author:** ![benemson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a8b319/32.png) [@benemson](https://www.ruby-forum.com/u/benemson)\
**Post date:** [October 19, 2008, 12:12am UTC](https://www.ruby-forum.com/t/running-cucumber-with-a-rails-rake-task/151255/2 "2008-10-19T00:12:14Z")

</div>

On Sat, Oct 18, 2008 at 10:33 PM, Ben E. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Hi All
> 
> I’ve just started experimenting with Cucumber and its great.  
> However I wasn’t quite sure how I should integrate it into my Rails  
> application.

Have you read this?

> **[GitHub: Let’s build from here](https://github.com/)**
>
> GitHub is where over 100 million developers shape the future of software, together. Contribute to the open source community, manage your Git repositories, review code like a pro, track bugs and fea...

Aslak

---

<div class="post-metadata">

**Author:** ![benemson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a8b319/32.png) [@benemson](https://www.ruby-forum.com/u/benemson)\
**Post date:** [October 19, 2008, 5:31am UTC](https://www.ruby-forum.com/t/running-cucumber-with-a-rails-rake-task/151255/3 "2008-10-19T05:31:06Z")

</div>

To run all features you can run:

rake features

To run specific features you can do something like:

script/cucumber --require features/steps features/path/to/my.feature

If you want to run a particular scenario find out the line number of  
the first Given and run:

script/cucumber --line 41 --require features/steps  
features/path/to/my.feature

Zach

On Sat, Oct 18, 2008 at 4:33 PM, Ben E. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> demo Rake file works, but I’m not sure I’ve set it up correctly in my
> 
> desc “run stories”
> 
> file so that it will run all my stories?  
> removed\_email\_address@domain.invalid  
> [http://rubyforge.org/mailman/listinfo/rspec-users](http://rubyforge.org/mailman/listinfo/rspec-users)

–  
Zach D.  
[http://www.continuousthinking.com](http://www.continuousthinking.com)

> **[Homepage | Mutually Human](https://www.mutuallyhuman.com/)**
>
> Technology Made for Humans by humans Data Analytics Data Analytics Data has become a critical part of business for most organizations. We work with you to ensure you have confidence in your data, can properly access it, and present it in a way that...

---

<div class="post-metadata">

**Author:** ![benemson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a8b319/32.png) [@benemson](https://www.ruby-forum.com/u/benemson)\
**Post date:** [October 19, 2008, 3:36pm UTC](https://www.ruby-forum.com/t/running-cucumber-with-a-rails-rake-task/151255/4 "2008-10-19T15:36:08Z")

</div>

Zach D. wrote:

> To run all features you can run:
> 
> rake features
> 
> To run specific features you can do something like:
> 
> script/cucumber --require features/steps features/path/to/my.feature
> 
> If you want to run a particular scenario find out the line number of  
> the first Given and run:
> 
> script/cucumber --line 41 --require features/steps  
> features/path/to/my.feature
> 
> Zach
> 
> On Sat, Oct 18, 2008 at 4:33 PM, Ben E. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > demo Rake file works, but I’m not sure I’ve set it up correctly in my
> > 
> > desc “run stories”
> > 
> > file so that it will run all my stories?  
> > removed\_email\_address@domain.invalid  
> > [http://rubyforge.org/mailman/listinfo/rspec-users](http://rubyforge.org/mailman/listinfo/rspec-users)
> 
> –  
> Zach D.  
> [http://www.continuousthinking.com](http://www.continuousthinking.com)  
> [http://www.mutuallyhuman.com](http://www.mutuallyhuman.com)

Thanks Aslak and Zach

I must apologise in my excitement I completely missed the Rails wiki  
section.  
Thats just what I was looking for. Thanks and keep up the good work.

BE…
