Internal Testing
Measuring Code Coverage
Code coverage is not the only way to judge the quality of a test suite, but it is a leading indicator. Low code coverage likely means the test suite is inadequate, but high coverage does not mean the test suite is bullet-proof.
CoverMe
Ruby 1.9 changed enough of the implementation’s internals that the old coverage library, rcov
, no longer works. Thankfully, the Ruby development team added hooks to make coverage measurement even easier than it was in 1.8.
The best coverage library for 1.9 is CoverMe: https://github.com/markbates/cover_me/
Setup
Add gem "cover_me"
to your Gemfile
in the development/test section and run bundle
.
Once the gem is installed there are two additional steps:
- Open
spec/spec_helper.rb
and addrequire 'cover_me'
as the first line - Run
rails generate cover_me:install
from the terminal to add it to the default RSpec testing tasks
Usage
Run your tests normally with bundle exec rake
. After each run there will be a coverage data file output in your project directory. To generate and open a report using the data, execute the provided Rake task:
Terminal
$
|
|
That’s about all there is to it!
Want More Metrics?
If you want to go totally bananas with metrics, try the metric_fu
gem: https://github.com/jscruggs/metric_fu
It includes tools to estimate code complexity, find repetitive code snippets, and more.