Friday, May 26, 2006

Red and Green for Ruby

One of my hacking partners dislikes it when I talk about my unit tests being green or red. Since running Test::Unit tests from the command line return "Plain Old Text", he's got a point. Since I don't think I'll change the way I talk, I decided to write a quick wrapper for Test::Unit to make things work. About 15 minutes later, I had RedGreen.

pate $ ruby test_good.rb | Redgreen
Loaded suite test_good
Started
.
Finished in 0.000485 seconds.

1 tests, 1 assertions, 0 failures, 0 errors
pate $ ruby test_bad.rb | Redgreen
Loaded suite test_bad
Started
F
Finished in 0.001635 seconds.

  1) Failure:
test_char2num(TestCipher) [test_bad.rb:6]:
Flunked.

1 tests, 1 assertions, 1 failures, 0 errors

pate $

It's an embarassingly small amount of code. There are actually as many lines in the shebang and leading comments (name, description, copyright, license and version number) as there are of actual code. Nonetheless, it seems like a potentially useful little toy (at least to keep my friend happy when I tell him my tests are green).

If you'd like to use it, you can install a gem (it'll be in the repository shortly. Just do:
gem install --remote Redgreen

6 comments:

Ryan Platte said...

Any way to detect whether output is to a terminal, à la the ls command? If you did that, we could require RedGreen in a rake file and get its beauty without extra typing.

Thanks for hooking this up! Long live TDD!

pate said...

Ryan,
I think this is very doable. I'll see if I can get to it at lunch today.

Anonymous said...

I think this is not usable in Windows, as CMD.EXE and COMMAND.COM don't seem to know about 'ansi' escape sequences.

Crude visual feedback can be got by calling system("color a0") or system("color c0") to change the background colour of the DOS box you are working in.

DJ

Sean said...

I've hacked RedGreen into the autotest tool chain. I will blog about it soon. Right now, I'm just loving you for giving me back this invaluable faculty I've been missing.

Sean said...

You should ask zenspider and dr brain to add RedGreen to the ZenTest suite. It really belongs beside unit_diff.

Alastair Brunton said...

Hi,

Redgreen isnt working with Ruby 1.9.2 and autotest.

Would be great to get it working again but it seems to be related to ruby using minitest instead of test/unit?

Does anyone know what would be involved in getting this to work. As I am currently a bit stumped.

Cheers,
Alastair