Here is a quick scenario.. You have over 200 tests and 800 assertions for your Rails application and you always run them before every subversion commit, but because you love tests so much or maybe you’re doing some TDD you want these tests to be running all the time.
You need ZenTest and its beautiful autotest, which will not only keep your test running but it will do it in a very clever way. autotest keeps an eye on all your code changes, once you change a file it will run its according tests only, instead of running the whole bundle.
sudo gem install ZenTest
So now we have a wonderful tool, let’s pimp it with some colors, how about red for fails and green for passes? Thanks to Pat Eyler and his cute Red and Green for Ruby gem that does the this for us:
sudo gem install --remote Redgreen
Now let’s make this pimped ride growl. If you are on Mac then you probably know about Growl so why don’t we use growlnotify to notify us with autotest results? Download and install Growl in case you don’t have it and then install growlnotify:
“In your shell, cd to the directory on the Growl disk image containing growlnotify, and type ./install.sh. That script will install growlnotify to /usr/local/bin and the manpage to /usr/local/man.” from Growl documentation
Getting too much growl notification can get very nasty with every test running for every code change, the best solution for this is to popup a growl autotest notifier when and only when we have new test result, in other words, if any of my tests fails I should be notified, if they pass again I should also be notified, now as long as my tests are passing I don’t need to be notified anymore, right?
We’ll add a light file to our home directory named .autotest with all these configurations we talked about, in addition to all the above let’s have a colored Rails icon for each red/green test notification too.
# Save this as ".autotest" in root_folder of computer or project # These require growlnotify, but once you have that you can tweak priorities # in the Growl control panel to get RedGreen in the growl bubble itself! # http://growl.info/documentation/growlnotify.php #Taken straight from the example in Autotest gem module AutoGrowl def self.growl title, msg, img='', pri=0, stick="" system "growlnotify -n autotest --image #{img} -p #{pri} -m #{msg.inspect} #{title} #{stick}" end Autotest.add_hook :run do |at| growl "Run", "Run" unless $TESTING end Autotest.add_hook :red do |at| growl "Tests Failed", "#{at.files_to_test.size} tests failed", '~/rails_fail.png', 2 end Autotest.add_hook :green do |at| growl "Tests Passed", "All tests passed", '~/rails_ok.png', -2 if at.tainted end Autotest.add_hook :init do |at| growl "autotest", "autotest was started" unless $TESTING end Autotest.add_hook :interrupt do |at| growl "autotest", "autotest was reset" unless $TESTING end Autotest.add_hook :quit do |at| growl "autotest", "autotest is exiting" unless $TESTING end Autotest.add_hook :all do |at|_hook growl "autotest", "Tests have fully passed", '~/rails_ok.png', -1 unless $TESTING end end class Autotest # All code borrowed from: # http://www.robsanheim.com/2006/08/07/hacking-green-bar-color-output-into-autotest/ BAR = "=" * 80 # filter output for colorized green/red bar def filter_output(results) filtered = "" results.each do |line| if line =~ /\\d+ tests, \\d+ assertions, 0 failures, 0 errors/ line = "\\e[32m#{BAR}\\n#{$&}\\e[0m\\n\\n" elsif line =~ /\\d+ tests, \\d+ assertions, (\\d+) failures, (\\d+) errors/ if $1 != 0 || $2 != 0 line = "\\e[31m#{BAR}\\n#{$&}\\e[0m\\n\\n" end end filtered <<line end filtered end def run_tests find_files_to_test # failed + changed/affected cmd = make_test_cmd @files_to_test puts cmd @results = `#{cmd}` hook :ran_command puts filter_output(@results) handle_results(@results) end end
Just place the following Rails icons in your home directory along with the .autotest file, run autotest -rails and enjoy being notified, Happy Hacking.
Update: In case you don’t really care about having the rails icons you can make your .autotest file way simpler (Thanks to Ryan’s comment) by requiring autotest/redgreen and autotest/growl and you are all set.
require 'autotest/redgreen' require 'autotest/growl'
Additional Resources:
Here are a couple of other resources from where I collected the solution I have here:



ryan davis said..
why? your .autotest could simply read:
require ‘autotest/redgreen’
require ‘autotest/growl’
I don’t see much of anything that is added, except for the complexity. If something is added, I’d prefer to see a patch submitted, not a blog post I have to search for.
on April 5th at 2:43 pmRida said..
Hello Ryan,
Thank you for commenting here on ridaalbarazi.com, your comment is completely valid, I had some outdated information on how to customize autotest from different blogs that I linked to here. Although I could have known about that if there is enough documentation for autotest on your website.
The value I’m trying to add here is to offer some tips and tricks to Rails developers, I didn’t intend to patch or add value to autotest itself.
on April 5th at 5:22 pmMEZYAN said..
Hi Reda,
on May 3rd at 8:51 pmFrom what I see it is very impressive, all I can say, carry on , with God’s blessing, hopping that I can understand something.
Ahmad said..
Hi Rida,
on May 3rd at 9:00 pmVery nice website, and impressive too, keep going and I wish you luck.
David Lowenfels said..
Hi Rida,
on August 7th at 6:22 amthanks for giving me credit for the images! :)
Complete And Utter Rails Bliss | Mornography UK said..
[...] autotest is part of the ZenTest package. It will automatically and continuously run your Rails tests every time you modify a file (and it will always just run the tests for the part off your app you’ve changed). When you’re on OS X, spice it up with Growl notifications. Together with the Shoulda plugin mentioned above, writing tests has become fun again. [...]
on January 14th at 3:13 am15 Top Tools for a Rails Programmer | Cheer Factory - Technology, Outdoor and Design said..
[...] 8. Zentest:”http://www.zenspider.com/ZSS/Products/ZenTest/” especially autotest which runs your tests automatically each time you save a relevant file. Linking in the Growl notifications You might also want to get this working with Rspec. If you do then.. this might be of interest. [...]
on February 12th at 11:20 pm