Err the Blog Atom Feed Icon
Err the Blog
Rubyisms and Railities
  • “Colored Tests”
    – Chris on August 13, 2006

    In case you missed it, Pat Eyler released a nugget known as RedGreen back in May. It adds color to your bland testing lifestyle. Read that post if you haven’t. It’s good. I’ll wait.

    Okay, so you know RedGreen is run like so: $ ruby test_good.rb | Redgreen. You know it colorizes the stats bar printed at the end of a test run. You know it’s a great idea. Let’s hack.

    Grab my version of redgreen from the Err gem server:

    sudo gem install redgreen

    What this gem does is give you a script, rg, which can be used to run tests in place of your ruby binary. It’s a real big hack so who knows if it works for any Rubys before / after 1.8.4. When it does work, it looks something like this:

    $ rg test/test_parser.rb
    Loaded suite test/test_parser
    Started
    ...........F.........
    Finished in 0.058691 seconds.

      1) Failure:
    test_parse_options(TestParser) [./test/test_parser.rb:12]:
    Flunked.

    21 tests, 33 assertions, 1 failures, 0 errors

    If you’re on Windows, redgreen won’t work out of the box unless you’ve installed the Win32 console stuff as per gordon’s recommendation. The line of code he suggests adding is included in the gem, just get that console package and you should be okay. (I haven’t actually tested this. Let me know if it passes / fails.)

    That’s it, more or less. Run your tests with rg test_file.rb and thank Pat. Hrm, that gives me an idea…

    autotest

    Rob Sanheim recently posted a hack which lets you use Pat Eyler’s RedGreen with autotest. You know, the autotest in ZenTest (sudo gem install ZenTest).

    To get this gem version of redgreen working with autotest (from ZenTest 3.3.0), open up (or create) ~/.autotest and add this code wherever:

    Autotest.send(:alias_method, :real_make_test_cmd, :make_test_cmd)
    Autotest.send(:define_method, :make_test_cmd) do |*args|
      real_make_test_cmd(*args).sub('test/unit', %[rubygems -e "require 'redgreen'"])
    end
    

    Another hack won’t hurt, right? Autotest internally uses the make_test_cmd method to build commands needed to run your tests. We’re intercepting the string it returns and replacing instances of test/unit with rubygems -e “require ‘redgreen’”.

    Yum. Enjoy.

    Update: Issues. Thanks commentators and e-maulers. The status bar redness has been fixed and the .autotest code has been updated.

    Again:

    sudo gem install redgreen

  • Pat Eyler, 1 day later:

    Thanks for the kind words (about the blog and about redgreen). Thanks even more for taking it places I hadn’t thought of. I should probably email/chat with the folks writing RedGreen hacks to see if someone wants to take it over.

  • Rob Sanheim, 1 day later:

    Thanks for the link. Ryan Davis (part of ZenSpider, creators of autotest) has said that he will integrate all this stuff into the next version, fyi. So its just a temporary hack.

  • Kent, 1 day later:

    Very nice. Thank you.

  • Chris, 1 day later:

    Thanks for the inspiration, guys. It’s good to know autotest is going to officially color itself up soon.

  • Labrat, 3 days later:

    Nice one. You can also pipe the rake task “rake test | redgreen” to get the whole package or even alias it in a simple rake task.

    Another approach is to use Nuby on Rails’ method to generate dynamic rake tasks to achieve this. http://nubyonrails.com/articles/2006/07/28/foscon-and-living-dangerously-with-rake

    1 minute rake hack:

    task :rg do system “rake test | redgreen” end

    1. test a subset using redgreen on the fly
    2. “rake units” will translate to “rake test:units | redgreen”

    rule ”” do |t| system “rake test:#{t.name} | redgreen” end

  • Labrat, 3 days later:

    oops… bad formatting. sorry bout that.

  • iain d broadfoot, 9 days later:
    Two wee problems:
    • the autotest gsub breaks testing rails apps, as “test/unit” is part of the path to tests: using #sub instead works.
    • the results line always appears red, because you’re testing $1 != 0, but $1 will always be a string, so it’s doing “0” != 0. $1 != “0” works.

    cheers! (and props to alisdair for his l33t bug-noticing skillz)

  • Niels, about 1 month later:

    Am I the only one already having a rg binary as a shortcut for ./script/generator? Otherwise great gem.. :)

  • Akhil Bansal, 2 months later:

    Hi, I tried it on windows, but not worked. I have redgreen installed with win32console. I issued rb test_file.rb and got the following on prompt not the colored stuff: Started ←[32m.←[0m←[32m.←[0m←[32m.←[0m←[32m.←[0m←[32m.←[0m←[32

    Finished in 0.344 seconds.

    ←[32m8 tests, 22 assertions, 0 failures, 0 errors←[0m

    Any idea: please mail me at bansalakhil30.10 at gmail dot com

  • gordon thiesfeld, 8 months later:

    There’s a bit more code needed to get this working on windows. Here’s the diff:

    1d0 < require ‘Win32/Console/ANSI’ if PLATFORM =~ /win32/ 16a16 > 17a18,22 > if PLATFORM =~ /win32/ > require ‘Win32Console’ > @io = Win32::Console::ANSI::IO.new() > end >

  • Ten people have commented.
    Chime in.
    Sorry, no more comments :(
This is Err, the weblog of PJ Hyett and Chris Wanstrath.
All original content copyright ©2006-2008 the aforementioned.