Get your Rails tests results via Growl notifications

I’ve been using Autotest (part of the ZenTest package) while testing my Rails applications and never thought of asking for more. After all, getting my tests (well, specs actually) run automatically whenever I make a relevant change and having diff-level granularity on which tests Autotest re-runs is pretty damn useful enough. That is, until I came across this RSpec and Autotest with Growl notifications blog entry (on JavaBlogs, no less!)

How’re these for red/green test results?

Tests (specs) passing!

Growl notification of tests passing


Tests (specs) failing :(

Growl notification of tests failing :(


Much better! Now I don’t have to glance at my terminal window (I recently realized how painful it is while coding with a single monitor) and get unobtrusive notifications.

Unfortunately, this is Mac-only (or wherever Growl is supported). I had to follow links to several blog entries to get it setup nicely (see Growl + Autotest Rails with Zentest 3.4.0 and Green goodness with autotest + growl - those are my references).

Basically you need Growl installed, and the growlnotify tool in your path. growlnotify can be found in the Extras/growlnotify directory in the Growl dmg (for example, after you’ve mounted the Growl-0.x.x.dmg, you can find it here: /Volumes/Growl/Extras/growlnotify). Just copy the growlnotify executable into your path (I copied it into /opt/local/bin since I’m using Darwin Ports and don’t have a /usr/local/bin directory). Verify it’s running by entering:

growlnotify -m "Testing growlnotify" Boo Ya!

into a terminal window. You should see a Growl notification.

Now, you need to create an Autotest hook by creating an autotest init file in your home directory named .autotest, and copy this chunk of text in:


module Autotest::Growl
  def self.growl title, msg, img, pri=0, sticky=""
    system "growlnotify -n autotest --image #{img} -p #{pri} -m #{msg.inspect} #{title} #{sticky}"
  end

  Autotest.add_hook :ran_command do |at|
    output = at.results.last.slice(/(\d+)\s.*specifications?,\s(\d+)\s.*failures?/)
    if output =~ /[1-9]\sfailures?/
      growl "Test Results", "#{output}", "/Data/Pictures/Icons/rails_fail.png", 2, "-s"
    else
      growl "Test Results", "#{output}", "/Data/Pictures/Icons/rails_ok.png"
    end
  end
end

Replace the paths to the pass/fail icons (as you can see, mine are in /Data/Pictures/Icons/) with the correct paths to any images you want to display (grab them from Green goodness with autotest + growl or roll your own). You can change the parameters passed to growlnotify however you want (use growlnotify -h to see what parameters it acccepts). The pri parameter sets the priority level of the message (test failures come up with a priority of 2, hence the red background of the Growl notification). Passing -s to sticky tells Growl to keep the notification sticky. If you’re running your (auto)tests on a remote server (e.g. in a Continuous Integration system), you can even use growlnotify to send remote notifications (see the -H switch) to your development workstations!

Lovely! I love you, MacBook Pro serial no. W86280NAVWW (despite your broken DVD drive and you randomly freezing up on me when you get too hot).

Are your Rails tests not coming out as expected? Maybe you should take a career goal test to see if you should be in the computer field. If you do want to change careers, you can always get an accredited online degree in a new field. Online college classes are easy and can be taken from home.

20 Comments & TrackBacks ()

Paper doll icon
Ryan Davis's Gravatar

I don’t get it. What does this do that “require ‘autotest/growl’” in your .autotest file doesn’t do?

Posted by: Ryan Davis on April 3, 2007 4pm

Paper doll icon
Chu Yeow's Gravatar

I was wondering as well when I read the blog entries, but it actually
does reduce the amount of growl messages, and it also makes it work
with RSpec on Rails (as opposed to vanilla Test::Unit).

Thanks for writing ZenTest, lovely tool!

Posted by: Chu Yeow on April 3, 2007 5pm

Paper doll icon
winson's Gravatar

Hi,

I follow yours but I got an error: “./.autotest:13:in `slice’: can’t convert Regexp into Integer “. It seems like at.results.slice(/(\d+).*errors/) had some problem. I run this on my Powerbook with MacPort.

Posted by: winson on April 29, 2007 8pm

Paper doll icon
Chu Yeow's Gravatar

With the latest ZenTest (3.5.1), the results property has been changed. I’ve updated the code to reflect the required change (basically, autotest.results.last is used instead.

Posted by: Chu Yeow on April 29, 2007 8pm

Paper doll icon
winson's Gravatar

It works!! Very thanks & your reply is so fast!!

New ZenTest seems not compatible with png, because Growl throw me a “CMSCreateDataProviderOrGetInfo : Invalid colorspace type” error message while I autotest -rails.

Posted by: winson on April 29, 2007 9pm

Paper doll icon
Daniel Fischer's Gravatar

For some reason this isn’t working for me, all that I get is a growl notify with a pass, but no output message at all.

Posted by: Daniel Fischer on May 5, 2007 9am

Paper doll icon
Daniel Fischer's Gravatar

It also never shows a failure message, all of them are ‘passes’ so something is wrong :(

Posted by: Daniel Fischer on May 5, 2007 9am

Paper doll icon
Nate Vack's Gravatar

FYI, this breaks with Zentest 3.5.x — which actually ships with some growl support out of the box. I didn’t care for its output, though… so this made life happy for me:


module Autotest::Growl
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 :red do |at|
errors = at.files_to_test.map { |k, v| "#{k} (#{v})"}.join("\n")
#failed_tests = at.files_to_test.inject(0){ |s,a| k,v = a; s + v.size}
growl "Tests failed:", "#{errors}", '~/Library/Autotest/rails_fail.png', 2, "-s"
end

Autotest.add_hook :green do |at|
res = at.results[/\d+ tests.*$/]
growl "Test Results", "#{res}", '~/Library/Autotest/rails_ok.png'
end
end

Posted by: Nate Vack on May 8, 2007 11pm

Paper doll icon
Ruby on Rails » ?????? ????'s Gravatar

[…] ??? ?????? ????, ??????? ????????? ???? ????????????, ??? ? ???? ? ????? ????????????? ??????????: RSpec, Migrations, Autotest with Growl notifications. […]

Posted by: Ruby on Rails » ?????? ???? on August 8, 2007 3pm

Paper doll icon
Joshaven Potter's Gravatar

##Your .autotest code did not work for me
##Here is what did work for me though:

module Autotest::Growl
def self.growl title, msg, img, pri=0, sticky=”"
system “growlnotify -n autotest –image #{img} -p #{pri} -m #{msg.inspect} #{title} #{sticky}”
end

Autotest.add_hook :ran_command do |at|
output = at.results.last.slice(/\d* tests, \d* assertions, \d* failures, \d* errors/)
if output =~ /0 failures, 0 errors/
growl “Test Results”, “#{output}”, “~/Library/autotest/rails_ok.png”
else
growl “Test Results”, “#{output}”, “~/Library/autotest/rails_fail.png”, 2, “-s”
end
end
end

Posted by: Joshaven Potter on August 18, 2007 5am

Paper doll icon
Yet another autotest desktop notifier … OSD « Geekvicious Journal's Gravatar

[…] it is not possible to choose the color, for the notifications. So based in the growl autotest notifier, I wrote a version for osdsh an X OSD manager, it is not as pretty as growl ;)… but it gives […]

Posted by: Yet another autotest desktop notifier … OSD « Geekvicious Journal on August 23, 2007 10pm

Paper doll icon
Inge Jørgensen's Gravatar

The .autotest file above doesn’t work with the newest version of autotest, I did some tweaking to make it work.

Posted by: Inge Jørgensen on August 28, 2007 6pm

Paper doll icon
Woody's Gravatar

Nate Vack already mentioned that the author’s .autotest file didn’t work anymore. I found Nate’s to be really great, except there’s some newline stuff that bothered me. I fiddled with it a bunch, and found something that works:


module Autotest::Growl
def self.growl title, msg, img, pri=0, stick=""
system "growlnotify -n autotest --image #{img} -p #{pri} -m '#{msg}' #{title} #{stick}"
end

Autotest.add_hook :red do |at|
errors = at.files_to_test.map { |k, v| "#{k}:\n #{v.join("\n ")}"}.join("\n\n")
#failed_tests = at.files_to_test.inject(0){ |s,a| k,v = a; s + v.size}
growl "Tests failed:", "#{errors}", '~/.fail.png', 2#, '-s'
end

Autotest.add_hook :green do |at|
res = at.results[/\d+ tests.*$/]
growl "Test Results", "#{res}", '~/.pass.png'
end
end

the only differences are in line 7, where I manhandle the newlines to show up and add some spacing for multiple errors, and then put ‘ ‘ around the message (again, for newline handling purposes).

Much easier on the eyes!

Posted by: Woody on September 16, 2007 2pm

Paper doll icon
Woody's Gravatar

Ok, one last worthy change. It’s nice to know stuff about passing test results, so I replaced line 13 above (res = at.results[/\d+ tests.*$/]) with res = at.results.scan(/Finished.*failures/m).to_s.gsub(/\e\[32m/,''), and now my completed tests give some more info.

Posted by: Woody on September 16, 2007 6pm

Paper doll icon
Christian Dalager's Gravatar

I modified the .autotest hook to stop growling good and green tests again and again. It’s all about cutting down the redundancies.

See this pastie: http://pastie.textmate.org/98487

Posted by: Christian Dalager on September 19, 2007 7am

Paper doll icon
bonq.net/flipp » Blog Archive » daily del.icio.us 2007-12-02's Gravatar

[…] Get your Rails tests results via Growl notifications - redemption in a blog (tags: autotest rubyonrails ruby test testing growl rails) […]

Posted by: bonq.net/flipp » Blog Archive » daily del.icio.us 2007-12-02 on December 3, 2007 3am

Paper doll icon
Raphael Schiller » Blog Archive » Testergebnisse via Growl/GnomeNotify's Gravatar

[…] unter Gnome via GnomeNotify Notifications unter OSX via Growl Share and Enjoy: These icons link to social bookmarking sites where readers can share and […]

Posted by: Raphael Schiller » Blog Archive » Testergebnisse via Growl/GnomeNotify on December 20, 2007 6pm

Paper doll icon
A Fresh Cup » Blog Archive » Double Shot #30's Gravatar

[…] Get your Rails tests results via Growl notifications - I’m starting to rethink whether every bloody thing should come in via Growl. If test results come back quickly enough to be useful, aren’t they foreground information? Still, eye candy is seductive. (via dzone) […]

Posted by: A Fresh Cup » Blog Archive » Double Shot #30 on January 1, 2008 10pm

Paper doll icon
Quelques notes sur autotest - RubyZness - Ruby on Rails, business et entreprises's Gravatar

[…] - Video d’utilisation d’autotest - Autotest et growl pour […]

Posted by: Quelques notes sur autotest - RubyZness - Ruby on Rails, business et entreprises on August 2, 2008 5pm

Paper doll icon
Autotest Notifications in Ubuntu with Mumbles « dambalah's Gravatar

[…] notified of the test results without having to switch to the terminal window running AutoTest. Some Mac users already found a way to do this using Growl. However, that only works if you have a Mac, and I […]

Posted by: Autotest Notifications in Ubuntu with Mumbles « dambalah on October 8, 2008 7am

You can subscribe to the RSS feed for comments on this post.

Sorry, this entry is no longer accepting comments. If you have something you really want to say, you can write me.