Complete Code Coverage For Rails

Rails applications are likely to have a variety of automated tests and even a handful of manual tests. At Magpie we use a combination of unit tests and Selenium on Rails, so the code coverage metric for unit tests never quite adds up to what is expected.  I’d bet that on most Rails projects getting coverage to 100% seems impossible from a unit test standpoint. I’m going to describe a way to combine code coverage results from all testing done on a product. It’s relatively easy to do.

First, install rcov. Gemcutter.org has the latest version with sortable charts.

gem install rcov --source http://gemcutter.org

Windows users may run into an error about nmake. If that happens try installing an older version of rcov.

gem install rcov -v 0.8.1.1.0

Start the server with rcov:

rcov script/server -o "test/coverage" -x "/gems/" --rails --aggregate coverage.data -- -e test

Now that the server is up, run all of the manual and/or Selenium tests. When the tests are finished, hit Ctrl-C back at the command line to generate a report and the coverage.data file. Check out the results at test/coverage/index.html. Watch out because running the next step will overwrite the report.

Append the following rake task to the Rails project Rakefile.

require 'rcov/rcovtask'

namespace :test do
  Rcov::RcovTask.new("coverage") do |t|
    t.libs << "test"
    t.test_files = FileList["test/**/*_test.rb"]
    t.output_dir = "test/coverage/"
    t.verbose = true
    t.rcov_opts << ["--sort coverage",
                    "--rails",
                    "--exclude /gems/",
                    "--aggregate coverage.data"]
  end
end

This will provide the rake test:coverage task that you may already be familiar with. The main difference in the task is that it adds its results to the coverage.data file. Run it next:

rake test:coverage

The updated coverage results should contain a very clear picture of what isn’t covered by testing at this point. The Rails community has many different testing techniques, so this won’t work for everyone. Hopefully from reading this you can still compose a strategy for seeing the full coverage of your tests.

Note: Make sure to delete the coverage.data file before test runs otherwise the left over data is likely to cause confusion.

Magpie Spikes in XP projects

405px-ThorMagpie develops software solutions for a variety of clients. When we secure a new client project, it requires a lot of learning to understand the project domain, legacy code, project tool chain, and other project technologies. Since we run our projects via agile methods, much of this learning is captured in task cards called “Spikes”.

Since our business success depends upon our ability to ramp up quickly, we have to make sure to work these spike cards efficiently so we learn quickly.

Here’s what we do:

  • Discuss Spike tasks during our task breakdown of a story in detail. What is the goal of this spike? What are the outputs? How will we communicate the information we find to the team?
  • Time-box the Spike. It is very easy to spend extra time on spike cards as they are learning tasks. Developers love to learn…which causes some to spend more time on spikes than necessary.
  • Speed!- Since delay is waste in software projects, the time it takes to do all the spikes can greatly impact the project success. Figure out ways to go through the learning process quickly. Maybe you need to take a class, or bring a class on-site, or hire a consultant who is an expert in what you need to learn. The cost for classes or consultants can easily offset the cost of the learning effort and the delay caused by the learning time.
  • Partner! We often will partner (pair) our developers with client developers that have expertise in the domain and project tools and technologies. Reduces risk and speeds up the program.
  • Make sure to communicate the learning to the whole team, not just the person who did the spike. Everyone needs to know.

In summary, spikes represent learning that is required and represent risk in your project. Manage that risk by managing your spike cards carefully.