Do you routinely test your code? Are you using rcov to run code coverage reports for your tests? If you aren’t, you should be. rcov is a code coverage tool for Ruby that is available as a Gem or a Rails plugin. It can generate text or HTML reports outlining what percentage of your code base is being exercised by your tests. The HTML reports even allow you to drill down and see exactly which lines in your code are being touched. If you’ve ever used a tool like Cobertura for Java, you’ll know exactly what to expect with rcov.
Using rcov on a regular basis will enable you to isolate parts of your codebase that aren’t being tested very well (if at all). For example, the first time I ran rcov on one of my projects, it reported that the tests written against my Rails models touched 87% of the code. Not too shabby! However, my Rails controllers only had 45% coverage. Where do you think I concentrated my testing efforts after rcov was kind enough to inform me of these facts?
Installing and using rcov with an existing Rails project is a cinch. Let’s get started:
gem install rcov
Now move to the root of your Rails project and type:
script/plugin install http://svn.codahale.com/rails_rcov
Voila! Installation complete. Don’t you just love Rails? Now let’s fall in love with Rake:
rake test:units:rcov rake test:functionals:rcov
Running these tasks will generate a text-based report of your project’s code coverage. The percentage of each class covered is listed, along with a total for the entire set of tests (unit vs. functional). Rcov also creates a /coverage
directory beneath the root of your Rails project. Inside this directory, you’ll find some beautiful HTML-based coverage reports.
Want to report on just your model classes? What about controllers? Easy enough:
rake test:units:rcov SHOW_ONLY="app/models" rake test:units:rcov SHOW_ONLY="app/controllers"
Delightful. Now I’d be set if only rcov integrated with Zen’s autotest to display a coverage report that automatically updates every time I change some code. One other minor nitpick is that, much as it seems like rake test:rcov
should work, it doesn’t. Something else to add to a future release, I suppose.
Are you in a team environment? Hook rcov up to a continuous integration system and catch Jonny slacking off on his tests… a full half hour before the morning stand-up meeting.
For the price, you can’t beat what rcov provides. It won’t tell you if your tests are logically correct, but it sure as heck will scream at you if you’re not writing them to begin with.
Very cool. Thanks for sharing it.
P.S. Code coverage on my app is appalling 🙁