Ruby Coverage with SimpleCov
I find it really useful to use a coverage tool when writing code. I don’t depend on it, but I do like to see what percentage of lines are covered, so I can at least catch any glaring misses.
In Ruby 1.8, we had rcov, which was (and is) a really great tool. In Ruby 1.9, I’ve been depending on SimpleCov. I really enjoy their reporter interface, and the usage is very similar to rcov. In your
spec_helper, before you include the thing you’re testing, just start up SimpleCov. I prefer to write it like:
Some features I really like:
- Filtering is super easy
- Grouping is a powerful way to segment files like models, controllers, into their own groups.
- Merging results over time so that when you run a subset, you can merge them into the overall set from other subsets (so you don't end up with that weird situation where things look uncovered because they're covered in specs you're not currently running).
Most times, I want to be able to see coverage for one test really clearly. For the past few weeks, I’ve been using something like this to accomplish it in my Rails projects:
That way, when starting
rspec (or spork, which I highly recommend), I can say what files I want to see coverage for. Another way to accomplish this which may suite you better is to create groups for things specified in COV.
If you’re not currently using SimpleCov, try it out!