NR Time - Powerful time tracker for Mac OS X

Feel the ease of time tracking with NR Time. Log your time expenses in the billing-friendly format with minimal effort.

Autotest and Growl for both RSpec and Test::Unit

Posted: November 8th, 2008 | Filed under: Programming | Tags: , , | 4 Comments »

GrowlPreviously I wrote about BDD and zen testing in my post “Ruby / Rails: Zen Testing with Autotest and XOSD“. I mentioned a couple of links where you could find an .autotest script for your Linux / Mac environment to throw the results of your tests to Growl or XOSD for a nice on-screen text display. This kept you on top of background testing at all times.

Over the course of the last few months I was taking part in different Rails projects. Some of them used RSpec and some Shoulda/Mocha for unit testing. The original .autotest script that I picked and adopted was intended for RSpec, so I had to modify it over and over to support the environment I was currently working in. Finally, as I realized there was not a slightest hope to end this jumping back and forth, I came up with a multi-environment script that works with both RSpec and Test::Unit.

It detects it all automatically, and the only thing you need to do is to give the paths to your pass / fail / pending icons.

Here’s the script for your copy-pasting convenience. Name it “.autotest” and place in your home directory. Enjoy!

module Autotest::Growl

  STATUS_ICONS = {
    :pass     => "~/Library/autotest/pass.png",
    :fail     => "~/Library/autotest/fail.png",
    :pending  => "~/Library/autotest/pending.png"
  }

  def self.growl(title, msg, img, pri=0, stick="")
    system "growlnotify -n autotest --image #{img} -p #{pri} -m #{msg.inspect} #{title} #{stick}"
    sleep 1
  end

  Autotest.add_hook(:ran_command) do |autotest|
    results = [ autotest.results ].flatten.join("\n")

    rspec_regexp     = /(\d+)\s+examples?,\s*(\d+)\s+failures?(,\s*(\d+)\s+pending)?/
    test_unit_regexp = /(\d+)\s+tests?,\s*(\d+)\s+assertions?,\s*(\d+)\s+failures?,\s*(\d+)\s+errors?/

    if !(output = results.scan(rspec_regexp).flatten).empty?
      # RSPEC
      examples, failures, x, pending = output
      if failures.to_i > 0
        prefix, icon, priority, stick = "FAIL:", :fail, 2, "-s"
      elsif pending > 0
        prefix, icon, priority, stick = "PENDING:", :pending, 2, "-s"
      else
        prefix, icon, priority, stick = "PASS:", :pass, 0, ""
      end

      info  = "Examples: #{examples}"
      info += " Failures: #{failures}" if failures.to_i > 0
      info += " Pending: #{pending}" if pending.to_i > 0

    elsif !(output = results.scan(test_unit_regexp).flatten).empty?
      # Test::Unit
      tests, assertions, failures, errors = output
      failed = failures.to_i > 0 || errors.to_i > 0
      if failed
        prefix, icon, priority, stick = "FAIL: ", :fail, 2, "-s"
      else
        prefix, icon, priority, stick = "PASS: ", :pass, 0, ""
      end

      info  = "Tests: #{tests}/#{assertions}"
      info += " Errors: #{errors}" if errors.to_i > 0
      info += " Failures: #{failures}" if failures.to_i > 0

    else
      # Unknown format
      return
    end

    growl prefix, info, STATUS_ICONS[icon], priority, stick
  end
end

4 Comments

  1. 1 William H. Harle Jr. said at 15:40 on November 10th, 2008:

    Hey Aleksey,

    I was wondering what version of growl and zentest you are using. I’ve just installed the newest versions of each and have been having trouble getting autotest to work. After implementing your code and fixing the > to be >, i try running autotest -rails in my project folder and it just reads “loading autotest/rails” and nothing else happens. Any ideas? Thanks so much for any help you can give me.

  2. 2 Aleksey Gureiev said at 16:41 on November 10th, 2008:

    Hey William,

    Unfortunately, there’s one other problem with the script in line 17: “rspect_regexp” should be “rspec_regexp”. I’m going to update the post now. That may be the case. Stupid of me, did a bit of refactoring right in wordpress to fit the long regex lines nicely.

    I’m using ZenTest 3.9.3. It’s probably far from the latest but can’t see a reason to update since it works nicely. As for the growl — it’s the latest 1.1.4.

    Let me know if it helped.

  3. 3 Andrew Dashin said at 19:21 on November 10th, 2008:

    Heh, nice, but I hate Growl :)

    ps Growl is a good example of third-party application that became a standart de-facto. I’ll give it one more chance to live on my latptop.

  4. 4 Jeff Wigal said at 07:53 on December 19th, 2008:

    @William, I had the same problem. See this:

    http://www.ruby-forum.com/topic/173162

    Look through your plugins for a lib/autotest folder. I ended up deleting the folder, then it worked like a charm. My offending plugin was subdomain_fu.

    I don’t use rspec (yet),


Leave a Reply