Gw

Rails Test-flow: Shoulda, Autotest, RedGreen, with Growl Notifications

December 18, 2008


I’ve been hooked on the Shoulda testing framework for Rails lately, as you may well know if you follow my twitter feed.

I could go on and on about Shoulda, but instead I’ll let Tammer Saleh tell you about it himself. In short, it makes tests more readable.

No matter what framework you’re writing your tests in, it’s nice to know exactly what’s going on, and exactly when it’s happening. Whether you’re writing development code or test code, it’s imperative you know that what sort of effect the code you are writing is having on your application. That is the basic reasoning behind testing. As for how often you are told about these changes, well, normally you run rake or rake test from your command line, the entire test suite is run, and you get your results. Generally it takes a second or two and you can go back to coding.

Autotest changes your workflow by testing your changes as they are saved. Again, you could be writing test code or development code; as the changes are saved, they are automatically tested. This can save you time and keystrokes. Just check in with your terminal window every time you save a line of code.

Wait, what?

Yeah, that’s a pile of window-switching going on. Let’s get some help from Growl.

Autotest can work with the Growl Notifier to tell you when your tests are passing and failing. We can even sweeten the deal by utilizing the RedGreen gem. RedGreen colorizes your test output to help you more easily spot failing tests. So in this case, let’s use it to colorize our Growl Notifications.

This setup really doesn’t require Shoulda, so I’m not going to talk about the install and usage of Shoulda here, but I encourage you to look into it further for own sanity.

first let’s install autotest, it’s a part of the ZenTest suite:


$ sudo gem install ZenTest

you may notice if you try to run autotest, it’s looking for the hoe gem, install it with:


$ sudo gem install hoe

next, install redgreen:


$ sudo gem install redgreen

now, just for kicks, path to your rails app and run autotest:


$ autotest

at this point autotest should run your test suite and not return you to your prompt in terminal. Try editing some of your test code and save it. Now check back in with your command prompt; autotest should have run the associated tests. (colorized with redgreen)

So far so good, let’s get Growl to help us out. In your home path (~/) let’s edit (or create, if you have to) the .autotest file. I’m using Textmate so:


$ mate ~/.autotest

In this file, let’s add the following lines of code:


  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}", '~/library/autotest/rails_fail.png', 2
    end
  
    Autotest.add_hook :green do |at|
      res = at.results.scan(/Finished.*failures/m).to_s.gsub(/\e\[32m/,'')
      growl "Test Results", "#{res}", '~/library/autotest/rails_ok.png'
    end
  end

You may notice that we are relying on 3 things right now. The ability to use ‘growlnotify’ from the command line, and two image files: ‘rails_fail.png’ and ‘rails_ok.png’. Let’s get all 3 of these.

First, you can use growlnotify from the command line by adding the executable to your path. The executable is found in the growl.dmg in the ‘extras>growlnotify’ directory. Copy that file to your ‘/usr/local/bin’ directory. Test it’s been installed correctly by:


$ growlnotify -m "testing growlnotify install" Growl Test

Now that we’ve got that working, download these two images:

rails_okrails_fail

and save them to your ‘~/library/autotest’ directory. Now run autotest and marvel at the fact that your just streamlined your testing workflow even further.

credits:

*blog.codefront.net
*errtheblog.com
*blog.internautdesign.com
*maintainable.com

0 Comments


New Comment