Thoughts on software design, testing, and everything in between.

2 Easy Ways to Check Code Coverage3 min read

gem install pronto-simplecov

One of the most common tasks I need to undertake when reviewing code (whether that code is mine, or someone else’s) is checking the code coverage present in the changes.

Knowing that I’ve got a really solid foundation of tests allows me to be confident that the changes I’m looking at both works as intended and don’t break anything else. Of course, coverage alone doesn’t make for good tests – but without coverage, you can’t have good tests.

Finding this out manually is painful, and not something I’d be prone to repeat. And just getting your coverage for the entire app doesn’t really help you narrow down what you need. So, I present two methods I’ve found to be effective for quickly getting coverage for code changes. Note: I’m focusing on Ruby here, but I will write up options for client-side code soon.

Simplecov Custom Group

If you’ve used Ruby on Rails and write tests, there’s a good chance you’ve used simplecov. It’s a gem specifically designed to generate a nicely-formatted coverage report every time you run your test suite.

Using a combination of simplecov’s custom groups and our git history we can create a tab in its UI that only contains files changed since master.

Place the following code into your rails_helper.rb:

SimpleCov.start 'rails' do
# This adds a group (tab) to Simplecov's report that only shows coverage for
# files changes in the current branch. Useful for code reviews.
add_group 'Changed' do |source_file|
`git log --oneline --pretty="format:" --name-only master.. | awk 'NF' | sort -u`
.split("\n").detect do |filename|
source_file.filename.ends_with?(filename)
end
end
end

The git command outputs a list of changed files, each on its own line. We then break that string into an array and use those filenames as the basis for our own simplecov filter.

Super simple and really gives you the lay of the land you’re working in.

Pronto-Simplecov

One of my favorite tools for reviewing code is Pronto. Pronto is just a runner that lets you use other static analysis tools like RuboCop, Flay, Reek, and many others – but only on the code that’s changed since master.

This is huge since running a tool like RuboCop on a large codebase can generating a tremendous amount of noise, especially when you’re only interested in what’s changed.

Fortunately, there’s a little-known runner for pronto called pronto-simplecov. And it does just what you would guess.

Install Pronto with:

gem install pronto

Then install pronto-simplecov with:

gem install pronto-simplecov

Make sure you’ve run your full test suite so you’ve got an updated coverage report. Now you’re ready! Just

pronto run

and you’ll get the exact lines of any changes not covered by tests.

Wrapping up

These two tools make it really easy for me to get a sense of how solid the wall to my back is. Plus it helps direct my efforts in making it better. I hope it can be useful to you, as well.

What tools do you use when reviewing code? I’m always looking for ways to make the code review process more thorough and more efficient, so please leave a comment.