Close and Go BackBack to Viget

How (& Why) to Run Autotest on your Mac

David Eisinger
David Eisinger, Web Developer, June 19, 2009 11

If you aren’t using Autotest to develop your Ruby application, you’re missing out on effortless continuous testing. If you’d like to be using Autotest, but can’t get it running properly, I’ll show you how to set it up.

Autotest is a fantastic way to do TDD/BDD. Here’s a rundown of the benefits from the project homepage:

  • Improves feedback by running tests continuously.
  • Continually runs tests based on files you’ve changed.
  • Get feedback as soon as you save. Keeps you in your editor allowing you to get stuff done faster.
  • Focuses on running previous failures until you’ve fixed them.

Like any responsible Ruby citizen, Autotest changes radically every month or so. A few weeks ago, some enterprising developers released autotest-mac (now autotest-fsevent), which monitors code changes via native OS X system events rather than by polling the hard drive, increasing battery and disk life and improving performance. Here’s how get Autotest running on your Mac, current as of this morning:

  1. Install autotest:

    gem install ZenTest
    
  2. Or, if you’ve already got an older version installed:

    gem update ZenTest
    gem cleanup ZenTest
    
  3. Install autotest-rails:

    gem install autotest-rails
    
  4. Install autotest-fsevent:

    gem install autotest-fsevent
    
  5. Install autotest-growl:

    gem install autotest-growl
    
  6. Make a ~/.autotest file, with the following:

    require "autotest/growl"
    require "autotest/fsevent"
    
  7. Run autotest in your app root.

Autotest is a fundamental part of my development workflow, and well worth the occasional setup headache; give it a shot and I think you’ll agree. These instructions should be enough to get you up and running, unless you’re reading this more than three weeks after it was published, in which case all. bets. are. off.

Kevin said on 06/20 at 12:16 AM

You can color the autotest results (green/red for pass/fail) by adding this line to your ~/.autotest:

require ‘autotest/redgreen’

You may also have to run this first, in case you don’t have the RedGreen gem installed:

gem install --remote Redgreen

This gem will basically just add a red or green bar to the test results. This helps me glance over at my terminal window to see whether or not the changes I made to my code had any effect on the tests.

Scott Radcliff said on 06/21 at 11:43 AM

I’m still quite new to Ruby and building Rails apps, and recently discovered cucumber. I will definitely look into Autotest when I switch to a Mac (I’m jumping the PC ship in a few weeks). Thanks for the primer, you guys have a great blog.

David Eisinger said on 06/21 at 01:21 PM

@Kevin: cool, I’ll definitely pull that down.

@Scott: Thanks! As far as I know, you can run Autotest on Windows or Linux, you just won’t be able to run the fsevent & growl add-ons. Not that I don’t recommend switching to a Mac, of course :)

George said on 06/24 at 08:06 AM

Hey thanks for the short way procedure of Auto test.
But i have Linux,Is it applicable to it?

David Eisinger said on 06/24 at 10:03 AM

@George: to run Autotest on Linux, you’ll want to skip steps four through six. If you’d like to get Growl-style notifications, this page looks like a good place to start.

Andrew Cox said on 07/08 at 08:21 PM

Great stuff - the first post I found that actually worked.  It sure is easy to find outdated information about autotest :)

One thing that’s really bugging me though is that autotest isn’t rerunning all of my tests after passing a failed test. According to the strategy on autotest’s home page, it looks like it’s supposed to rerun all tests once passing a subset of tests:

1. Find all files and associate them from impl <-> test.
2. Run all tests.
3. Scan for failures.
4. Detect changes in ANY (ruby?. file, rerun all failures + changed files.
5. Until 0 defects, goto 3.
6. When 0 defects, goto 2.

Is it supposed to do that still? If it doesn’t do that anymore, then is there a hook I can add to make it do that? Because that seems really useful. I’m using ZenTest (4.1.3), autotest-fsevent (0.1.1), and autotest-rails (4.1.0).

Kevin Dewalt said on 07/18 at 01:56 PM

Great blog post, thanks.
I had no luck getting redgreen to work.  WRT the above comments, I had to use sudo gem install redgreen and not sudo gem install Redgreen

redgreen gem seems to be working fine after rake test but after autotest i get `gem_original_require’: no such file to load—autotest/redgreen (LoadError)

Any suggestions?.  Thanks.

Patrick Reagan said on 07/20 at 12:30 PM

@Kevin: I ran into the exact same thing when I tried just now.  It seems that with the version you’re using (labeled as an expanded version of the Redgreen gem), you’ll need to use: require ‘redgreen/autotest’ instead. 

I got this working on my machine - let me know if it works for you.

Kevin Dewalt said on 07/20 at 12:37 PM

@Patrick,

That worked....thanks!

Tadej Murovec said on 07/26 at 01:36 PM

Thanx for short and effective guide!

Kevin Dewalt said on 07/28 at 06:59 AM

One challenge I have had with autotest is slower tests as applications get bigger.  Has anyone tried test::unit 2.0.3?

They now have startup/shutdown methods that can be used as alternatives to setup.  Startup/shutdown supposedly only get run once per test instead of before every method.

Commenting is not available in this weblog entry.

We're the Developers

at Viget Labs. We write about web development trends, tips, best practices, industry events, and our projects — all with an emphasis on Ruby on Rails.

Contact Us

Have any questions, comments, ideas, or secrets to share? Let us know.


How many days in a non-leap year?

Sorry, you need to have Javascript enabled to use this form. (Don't blame us, blame the spammers!) If you'd like to contact us, please visit our Contact page.