Get your Rails tests results via Growl notifications

In: Mac|Ruby|Ruby on Rails|Web development

1 Apr 2007

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).

24 Responses to Get your Rails tests results via Growl notifications

Avatar

Ryan Davis

April 3rd, 2007 at 4pm

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

Avatar

Chu Yeow

April 3rd, 2007 at 5pm

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!

Avatar

winson

April 29th, 2007 at 8pm

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.

Avatar

Chu Yeow

April 29th, 2007 at 8pm

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.

Avatar

winson

April 29th, 2007 at 9pm

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.

Avatar

Daniel Fischer

May 5th, 2007 at 9am

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.

Avatar

Daniel Fischer

May 5th, 2007 at 9am

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

Avatar

Nate Vack

May 8th, 2007 at 11pm

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

Avatar

Ruby on Rails » ?????? ????

August 8th, 2007 at 3pm

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

Avatar

Joshaven Potter

August 18th, 2007 at 5am

##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

Avatar

Yet another autotest desktop notifier … OSD « Geekvicious Journal

August 23rd, 2007 at 10pm

[…] 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 […]

Avatar

Inge Jørgensen

August 28th, 2007 at 6pm

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

Avatar

Woody

September 16th, 2007 at 2pm

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!

Avatar

Woody

September 16th, 2007 at 6pm

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.

Avatar

Christian Dalager

September 19th, 2007 at 7am

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

Avatar

bonq.net/flipp » Blog Archive » daily del.icio.us 2007-12-02

December 3rd, 2007 at 3am

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

Avatar

Raphael Schiller » Blog Archive » Testergebnisse via Growl/GnomeNotify

December 20th, 2007 at 6pm

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

Avatar

A Fresh Cup » Blog Archive » Double Shot #30

January 1st, 2008 at 10pm

[…] 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) […]

Avatar

Quelques notes sur autotest - RubyZness - Ruby on Rails, business et entreprises

August 2nd, 2008 at 5pm

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

Avatar

Autotest Notifications in Ubuntu with Mumbles « dambalah

October 8th, 2008 at 7am

[…] 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 […]

Avatar

Rails Test-flow: Shoulda, Autotest, RedGreen, with Growl Notifications | Keeping an ear to the rail.

December 18th, 2008 at 3pm

[…] blog.codefront.net errtheblog.com blog.internautdesign.com […]

Avatar

AutoTest – All your rails tests are belong to us « Think Bohemian

September 26th, 2009 at 6am

[…] tests on a file commit automatically so you don’t have to. You can even configure it with growl so red or green icons flash depending on a pass fail condition…really cool […]

Avatar

Productivity boosts with Growl « Scattered Showers

October 4th, 2009 at 4am

[…] Unit test results via Growl […]

Avatar

Autotest for Compiled Languages (C#, C++) using Watchr - Life, Teams, and Software Engineering

June 22nd, 2015 at 3am

[…] I was learning Rails I set up Autotest on Ubuntu with Growl notifications, which I thought was a pretty slick idea. On Ruby this whole technique is super easy and efficient […]