Panasonic Youth rob sanheim writes about software, business, ruby, music, stuff and things



Posted
7 August 2006 @ 11am

Tagged
Command Line, Open Source, Rails, Ruby, TDD

Discuss

Hacking green bar color output into Autotest

update: You don't need to do this hack anymore - the RedGreen plugin is builtin into autotest with the latest versions.

A much welcome upgrade of the indispensable autotest recently came out which speeds things up greatly, but the output was still plain test_unit output. There are now hooks to add plugins to add widgets or whatever crazy output you want, but all I wanted was a green or red bar in the console. Taking some help from RedGreen by Pat Eyler for the escape chars, here's out make your autotest output a bit more feedbackie:

add this to the utility section of autotest.rb (yes, just hack the gem - its a minor change and the codebase is simple enough):

BAR = "=" * 80

  # filter output for colorized green/red bar
  def filter_output(results)
    filtered = ""
    results.each do |line|

      if line =~ /\d+ tests, \d+ assertions, 0 failures, 0 errors/
        line = "\e[32m#{BAR}\n#{$&}\e[0m\n\n"
      elsif line =~ /\d+ tests, \d+ assertions, (\d+) failures, (\d+) errors/
        if $1 != 0 || $2 != 0
          line =  "\e[31m#{BAR}\n#{$&}\e[0m\n\n"
        end
      end
      filtered <<line
    end
    filtered
  end

Then change the method "run_tests" to call filter output:

def run_tests
....
    @results = `#{cmd}`
    hook :ran_command
    puts filter_output(@results)
....

And you'll have output like this:

autotest red green outpout


11 Comments

Posted by
hoyo
10 August 2006 @ 5am

Great job! I really needed this….


Posted by
Some Rails Testing Links
14 August 2006 @ 7am

[…] Posted by labrat Mon, 14 Aug 2006 13:20:28 GMT Finally taking the plunge with writing tests. Still, not quite getting all things covered but this test-driven development is an excellent way to keep development moving without fear of breaking stuff. Developing without tests is like walking down a dark alley in a rough neighborhood. Tests throw floodlights all around you so you know what’s coming. It wont stop you from getting mugged if you walk down the wrong street, but you’ll see it coming from a mile away with ample time to respond rather than being caught by surprise after it’s too late. Same with pickpockets and murderers. For example, I found a method I was using was a redundant and brittle re-implementation of something that could be done with default associations. After changing the methods and views, test failures went from 0 to 15 or so. I immediately knew it had something to do with my change and was probably related to old fixtures and controller tests. The fix maybe took 5 minutes and now I was back to 0 failures and errors. This is much better than waking up 3 months later, scratching my head about what went wrong. Of course, it wont make an amateur coder a rails hacker but even if my methods are ugly and code is badly coupled, it is nice to know stuff works to a certain extent and it provides an excellent basic form to improve upon. A combination of ZenTest’s “autotest -rails” and this Red/Green terminal hack ensures I’m always up to speed with tests running in the background anytime there’s a change and color coded to boot! I really can’t do testing any justice compared to this wonderful article or any of the other links below, but this should give you a good start. […]


[…] There’s a redgreen craze going on with all sorts of hacks. After applying the autotest hack I was pretty much hooked on seeing test output turn green or red in the terminal so I started looking around for ways to apply this to default rake testing tasks. […]


Posted by
Pat Eyler
17 August 2006 @ 2pm

Rob,
Nice work! I love seeing the cool things people are doing with RedGreen. I’m actually looking for someone to take it over and give it the love it needs. Are you interested? Drop me an email.


Posted by
RedGreen Tests with Autotest & Growl Revisited
12 September 2006 @ 7am

[…] I’ve been happily using Rob Sanheim’s hack but since it involves modifying the autotest gem itself, it will surely break when a new version comes out. […]


Posted by
bitbutter
23 October 2006 @ 3am

Thanks very much for this! One osx n00b question: where would i find autotest.rb? (spotlight doesn’t seem to find it, but i do have autotest running).


Posted by
Rob
23 October 2006 @ 10am

bitbutter: It would be in your standard rubygems location, but with the latest version of ZenTest you don’t need this hack anymore. The RedGreen is included so all you have to do is include that in your .autotest file. More details here:
http://blog.zenspider.com/archives/2006/09/zentest_340_rel.html


Posted by
bitbutter
24 November 2006 @ 4pm

lovely thanks!


Posted by
ZenTest 3.6.0: Turbocharge Your Tests
29 May 2007 @ 6am

[…] […]


Posted by
xnot » Blog Archive » Oh testing
29 May 2007 @ 8am

[…] There is something very satisfying about seeing the green bar. If you are not sure what I am talking about, check out TDD, Textmate, and most importantly ZenTest (okay, this is also a big help). […]


Posted by
xnot » Blog Archive » Oh testing
29 May 2007 @ 8am

[…] There is something very satisfying about seeing the green bar. If you are not sure what I am talking about, check out TDD, Textmate, and most importantly ZenTest (okay, this is also a big help). […]


Leave a Comment

Comand Line Tip: How to List Only Directories Using Autotest with Integration Tests