Setting up Rspec, Autotest and Growl on Mac OS X

Posted by tohchye

p> We have recently decided to use BDD for our upcoming projects. To make testing more fun and efficient I have hook up Growl with ZenTest’s autotest to display test results from Rspec. Some of the following instructions is taken from http://wincent.com/knowledge-base/Setting_up_autotest_to_use_Growl

What follows are the instructions to install the software to setup your own if you like to try out.

  1. Growl
  2. ZenTest
  3. Rspec

Installing Growl

  1. Download the dmg package from Growl
  2. Open the disk image, and double click in the Growl.prefPane icon.
  3. Next is to install growlnotify . Double click on the Growl disk image again if you have close it and execute the following commands from a terminal
 cd /Volumes/Growl/Extras/growlnotify
 sudo ./install.sh
 cd
 hdiutil detach /Volumes/Growl 

Installing ZenTest

ZenTest provides 4 different tools and 1 library: zentest, unit_diff, autotest, multiruby, and Test::Rails. For our purpose we will be using autotest. The latest version of ZenTest has added rspec support and it’ll auto detect rspec and run tests in the background whenever there’s any changes to the specs.

Installing ZenTest is easy just run the following command:

 sudo gem install ZenTest 

Installing Rspec

To test rails with rspec you will need to install rspec and rspec_on_rails plugins . Run the following command from your project root directory.

 script/plugin install svn://rubyforge.org/var/svn/rspec/tags/CURRENT/rspec
 script/plugin install svn://rubyforge.org/var/svn/rspec/tags/CURRENT/rspec_on_rails 

After you have installed the plugins you will need to run following command once to bootstrap your Rails app with RSpec.

 script/generate rspec 

Customize Autotest

The last step is to customize Autotest. Download the following 2 images and save them under ~/.autotest_images (create this directory if it does not exist). You can also create your own images if you want.

Open your favorite editor and copy and paste the code, then save the file as .autotest under your user home directory.

 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|
    results = [at.results].flatten.join("\n")
    output = results.slice(/(\d+)\s+examples?,\s*(\d+)\s+failures?(,\s*(\d+)\s+not implemented)?/) 
      if output
        if $~[2].to_i > 0
          growl "FAIL", "#{output}", "~/.autotest_images/fail.png", 2
       else
         growl "Pass", "#{output}", "~/.autotest_images/pass.png" 
       end
     end
   end 
 end 

Configure Growl

Finally you can configure growl to with different display style via System Preference -> Growl. Try out all the different display style to see which one you prefer. Personally I like “Music Video”.

Once you have setup all the above. Run autotest under your project root directory and you will see Growl in action. Have fun!!

Comments

Leave a response