Thursday, October 18, 2007

Rails Debugger: Use RCov With Selenium, Watir, Or Just In Real Life

Jim Weirich and Joe O'Brien came up with something cool today at the TDD w/Rails studio. If you're using RCov to keep your test coverage total, it's useful to realize that rcov is just a command-line tool.

You might be using Selenium or Watir for browser testing. If you want to use RCov to figure out how much of your app actually gets run by your Selenium or Watir tests, it just requires using a few command-line options:

rcov script/server -o server_coverage --rails -x config/boot.rb -- -e test -p 3002

open server_coverage/index.html

Red: rcov command
Blue: script/server args (the -- means "end rcov args")
Black: OS X command to view the output

This will start a Mongrel instance, wrapped by RCov, which will find out how much of the code actually runs, and give you a detailed, color-coded report. Obviously, this technique can be very useful if you're dealing with a legacy Rails code base. Any time the path of execution through the code is unclear, using tools which can easily determine that path makes your life easier. For anybody who wishes Ruby had kick-ass debugger support, congratulations: now it does. In two lines of Unix. (And unlike even the Smalltalk debugger, this debugger only works if you use automated testing.)


  1. Interesting idea Giles. I just had a conversation with one of our QA engineers about something like this:

    They want to measure the code coverage of manual web ui tests (to see if its even worthwhile to automate or rethink their testing strategy).

    This isn't Rails though (its a legacy Java web UI, I was thinking of using JRuby). I just like the general idea of QA & Developers using the same reporting mechanisms for determining test quality, regardless of test type.

    Have I read too much into your post, or is there some merit to the strategy?

  2. No, that's **exactly** what I'm describing here. I'm pretty sure that coverage tools exist for Java as well, although I'm not sure what the details are there. I've seen one in action, but I don't remember its name. I think it was in connection with Selenium. Daniel Hinojosa from the Albuquerque Java Users' Group was giving the presentation. If you email me privately I can give you his email.

  3. Cool. Theres lots of Java tools (too much really) but the details all come down to how you instrument the source.

    Still, I have had my mind blown wide open from metaprogramming + test/unit + watir, to not chase this one down.

    Will pop you a mail later about it.


Note: Only a member of this blog may post a comment.