Rails: Test Coverage Reports with Rcov
Ah testing, the bane of my existence. It's a necessary evil, and a love/hate relationship. I told myself I wouldn't complain about it...just accept it...
So I'm finishing up my latest app, and I wanted to beef up my automated tests. Once we go into bug fix mode, I want to have a solid set of tests to make sure I don't screw one thing up while fixing another. I thought to myself, "surely there's something out there that can tell me what's covered by tests and what's not". And by-goly there is, it's called rcov. Rcov is a Ruby gem that will basically run your tests and determine which lines of code are run by the test. It then generates a pretty HTML report of your test coverage. Not only does it tell you percentages, it actually shows which lines of code are un-tested.
Needless to say this is freaking awesome. It actually makes writing tests (almost) fun. You can see your progress.
However it doesn't seem to work with Rails out of the box. I installed rcov and ran the example command from their docs, and it only covered a couple tests. After doing some searching, I found that it's not necessarily going to work perfectly with Rails, so you need the rails_rcov plugin. Basically this adds some rake tasks to make rcov run properly with Rails.
After I installed that plugin, it worked perfectly. This is very helpful to me, and now I have some direction for writing tests.
While this is a great tool, it's important not to rely on it too much. Just because your 100% green, doesn't mean that everything is truly tested. Integration tests look at the result of a series of actions, this wont be covered by rcov.
Now I'm off to make all those red bars green. BTW: am I the only person that likes to watch the windows defrag utility move those colored lines around? I know, I'm a moron.