0 Comments By Damon on Oct 15 in Rubyonrails.

For those familiar with the Rails community, it's likely at some point you've bumped into a Ruby gem called ZenTest. Zentest aims to make the process of ensuring test code coverage and automating testing a whole lot easier. One of the components of ZenTest is called 'autotest'. Autotest is a continuous testing facility that automatically runs tests based on changes in your code. In other words, when you make changes to your code, autotest detects it and runs the corresponding tests. Take autotest, stir in some of Pat Eyler’s RedGreen gem, add a touch of growlnotify, and now you have a setup that automatically runs tests when you make source code changes to your Rails project and then notifies you of the results via Growl.

It's fairly painless to get this type of setup operational and I'll try to just make this a walkthrough. Additional references are included at the end.

1. Install ZenTest

sudo gem install ZenTest

2. Install RedGreen gem

 sudo gem install --remote Redgreen 

3. Install Growl

You can download Growl from the Downloads section of their website. 

4. Add growlnotify to your path

Once you mount the Growl image, there is an "Extras" directory included which contains an install script (install.sh) that you can run. This script will install growlnotify in the /usr/local/bin directory. Just cd into that directory and run ./install.sh.

5. Create a '.autotest' file 

Autotest automatically looks for the .autotest file either in the root of your home directory or in your Rails project root directory. Therefore, create it in one of those two places. After you've created the file open it for editing. You can use the built-in growl support for autotest, or you can use a custom chunk of code in your .autotest file. Both examples are included below.

Built-in growl support

require 'autotest/redgreen'
require 'autotest/timestamp'
require 'autotest/growl'

My custom script

require 'autotest/redgreen'
require 'autotest/timestamp'

module Autotest::Growl

    # Feel free to configure the next four lines
    @path_to_custom_images = "~/"
    @pass_image = File.join(@path_to_custom_images, "pass.png")
    @fail_image = File.join(@path_to_custom_images, "fail.png")
    @include_time = true

  def self.growl title, msg, img="", pri=0
    msg += " at #{Time.now}" if @include_time
    system "growlnotify -n autotest --image #{img} -p #{pri} -m #{msg.inspect} #{title}"
  end

  Autotest.add_hook :red do |at|
    growl "Tests Failed", "#{at.files_to_test.size} tests failed", "#{@fail_image}", 2
  end

  Autotest.add_hook :green do |at|
    growl "Tests Passed", "Tests passed", "#{@pass_image}", -2 if at.tainted
  end

  Autotest.add_hook :all_good do |at|
    growl "Tests Passed", "All tests passed", "#{@pass_image}", -2 if at.tainted
  end
end

6. Download some testing images

You can create your own images for the pass/fail icons, but there have been a number already created. Here are a few I've found below.

You'll want to download those images and adjust the script above accordingly based on where you store them on your disk.

Now that we have all that out of the way, the next step is to try and get this all working. Open a terminal window and change to the root directory of your Rails project. Next, type the following:

autotest -rails

 Autotest should run a few tests and display the results in the terminal windows. Try making a few changes that would cause your tests to fail. If you have everything configured properly, you should now be receiving growl notification like the ones below.

Enjoy!!! 

References 

Current Rating: 5.0 rating from 1 vote

  • Current rating is 5
  •  
  •  
  •  
  •  
  •  

No Responses to "Growl notification on Rails testing"

Comments are Closed

Name: (Required)
Website:
Comment:
Remember my info