A Rails testing environment with Cucumber, RSpec, and autotest

By Zach Church on 25 08 2011

Automated testing is an important part of what we do at Mutually Human Software. Automated tests can reduce bugs and regressions, give us confidence that we can safely refactor, reduce manual testing, and can also result in cleaner, more maintainable software.

At Mutually Human Software, we use Cucumber with Capybara for integration tests and RSpec for unit tests and Cucumber step definitions. Autotest also helps to keep our tests running while we’re working.

Getting this environment set up in a Rails 3 project is not difficult, but I haven’t seen a guide to getting all of these tools working together. Assuming we have a Rails 3 or 3.1 project up and running on Ruby 1.9.2, we’ll start out by adding some dependencies to the Gemfile.

group :development, :test do
gem 'rspec-rails'
gem 'cucumber-rails'
gem 'database_cleaner'
gem 'autotest-rails'
end

Make sure to run ‘bundle install’ after updating your Gemfile. Next, we have to run a couple generators to complete the installation:

rails g rspec:install
rails g cucumber:install

Before running the tests, you may need to get your test database ready. This step is required after any database migration.

bundle exec rake db:test:prepare

Now we’re ready to rock. Run ‘bundle exec autotest’ to run just your specs, or ‘AUTOFEATURE=true bundle exec autotest’ to run your specs and your features. Running autotest with features will produce something like this:

0 scenarios
0 steps
0m0.000s

We can now start writing our features. I’m working with a brand new Rails project, so I’ll start out with something simple:

features/first_feature.feature

Feature: First feature
As a new Cucumber user
I want to learn how to write a feature

Scenario: Seeing the homepage for a new Rails project
Given I am on the homepage
Then I should see "Welcome aboard"

When I run “AUTOFEATURE=true bundle exec autotest”, I see:

..

1 scenario (1 passed)
2 steps (2 passed)
0m0.623s

We have a passing scenario! Cucumber was able to match the steps in our feature to steps in features/step_definitions/web_steps.feature, and ran those against our application using Capybara and its rack_test driver.

Javascript

rack_test is the fastest driver for Capybara, but does not support javascript. Capybara supports Selenium 2 (Webdriver) through Firefox for scenarios that use javascript, but it’s much slower. Simply add a @javascript tag above the scenarios that require javascript. We’ll do this to our first scenario, just for illustration:

Feature: First feature
As a new Cucumber user
I want to learn how to write a feature

@javascript
Scenario: Seeing the homepage for a new Rails project
Given I am on the homepage
Then I should see "Welcome aboard"

Now, as long as autotest is still running, it will run our feature again once we save the file. This time it should use Selenium, which will start Firefox in the background and check the page through the browser. It will all happen fast, but you should be able to see it if you’re paying attention and watching your applications.

On the first pass, autotest will run all of your tests. It will continue to rerun failing tests as you save files, and then it will rerun your complete test suite again once you have no remaining failures.

Growl integration on OS X

Autotest also supports growl integration. Growl must be installed, along with the growlnotify executable that comes with the Growl installer. Test that growlnotify is working by running:

growlnotify -m "fine sir or lady" Hello there

If that doesn’t work, follow the growlnotify installation directions from Growl’s website: http://growl.info/extras.php#growlnotify

Next, add this to your Gemfile:

group :development, :test do
gem ‘autotest-growl’
end

And add this to .autotest in the root of the project directory:

require ‘autotest/growl’

Troubleshooting

If autospec fails and complains that you don’t have redgreen installed, you may need to remove the line requiring redgreen from ~/.autotest.

Further Resources

Once you have these tools up and running, you’re well on your way to building a valuable test suite.

Also make sure to check out the RSpec book, co-written by our own Zach Dennis, as well as these other helpful resources to learn more about writing tests now that you’re up and running with the tools: