---
title: A Rails testing environment with Cucumber, RSpec, and autotest
description: 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,
---

[Home](https://mutuallyhuman-pr-605.herokuapp.com/)

- [Team](https://mutuallyhuman-pr-605.herokuapp.com/team)
- [Work](https://mutuallyhuman-pr-605.herokuapp.com/work)
- [Blog](https://mutuallyhuman-pr-605.herokuapp.com/blog)
- [Contact](https://mutuallyhuman-pr-605.herokuapp.com/contacts)

# A Rails testing environment with Cucumber, RSpec, and autotest

By [Zach Church](https://resources.mutuallyhuman.com/test/author/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:

- [http://pragprog.com/book/achbd/the-rspec-book](http://pragprog.com/book/achbd/the-rspec-book)
- [http://pragprog.com/book/hwcuc/the-cucumber-book](http://pragprog.com/book/hwcuc/the-cucumber-book)
- [https://github.com/cucumber/cucumber/wiki/tutorials-and-related-blog-posts](https://github.com/cucumber/cucumber/wiki/tutorials-and-related-blog-posts)

### Related Topics

- [Uncategorized (106)](https://resources.mutuallyhuman.com/test/topic/uncategorized)
- [User Experience (20)](https://resources.mutuallyhuman.com/test/topic/user-experience)
- [Ruby (11)](https://resources.mutuallyhuman.com/test/topic/ruby)
- [Interaction Design (10)](https://resources.mutuallyhuman.com/test/topic/interaction-design)
- [JavaScript (9)](https://resources.mutuallyhuman.com/test/topic/javascript)

### Related Posts

### About Mutually Human

Mutually Human is a custom software design and development consultancy specializing in mobile and web-based products and services. We help our clients design, develop and bring to market innovative products and services based on insightful research and strategy aligned with business objectives. We’ve helped Fortune 500 companies, state governments, and startups.

- [Home](https://www.mutuallyhuman.com)
- [Team](https://www.mutuallyhuman.com/team)
- [Work](https://www.mutuallyhuman.com/work)
- [Blog](https://www.mutuallyhuman.com/blog)
- [Contact](https://www.mutuallyhuman.com/contacts)

logo

[Mutually Human Grand Rapids: 401 Hall St. SW Suite 430 Grand Rapids, MI 49503 USA](https://www.mutuallyhuman.com/contacts#grand-rapids)

[Mutually Human Columbus: 243 N. Fifth Street Suite 300 Columbus, OH 43215 USA](https://www.mutuallyhuman.com/contacts#columbus)

+1 616 475-4225

[hello@mutuallyhuman.com](mailto:hello@mutuallyhuman.com)

<https://twitter.com/mutuallyhuman/><https://www.linkedin.com/company/mutually-human-software> <https://www.facebook.com/mutuallyhuman/> <https://github.com/mhs>

© 2017 Mutually Human