Branch coverage now available in Undercover checks ✨

Jan Grodowski
2 min readMar 16, 2021

--

The purpose of branch coverage is to check whether every conditional branch in a given line of code has been tested and Ruby has this feature since 2.5. Ruby’s simplecov can report branch coverage too and now so does undercover with the new 0.4 release!

This addition allows you to detect even more untested code within each automated pull request review, including untested case statements, conditional method calls (e.g. foo&.jump), single-line ifs (return :foo if valid?) or ternaries (foo ? 1 : 2).

Head over to the readme if you’re getting started, otherwise here’s what to do to add branch coverage reporting to a pre-existing undercover or UndercoverCI project setup:

1️⃣ Ensure your test command reports coverage using simplecov >= 0.18.0 and simplecov-lcov >= 0.8.0

2️⃣ Add enable_coverage(:branch) to the simplecov configuration block in spec_ or test_helper.rb:

SimpleCov.start do
enable_coverage(:branch)
end

3️⃣ That’s it! From now on, your checks will pick up and flag any untested code branches, plus you’ll see the total number of branches per method in the UndercoverCI check summary. 🤙🕵

This is how it looks in a sample check result from UndercoverCI (GitHub App):

Check the exact count of untested branches in each line by expanding the “Raw output” view

And that’s a sample branch coverage result from undercover CLI:

Line 5 was flagged with a single hit, but only one branch covered

On a closing note, this addition to undercover was possible thanks to PragTob who added branch coverage to SimpleCov and Magne Land’s contribution to the branch cov feature in Undercover. Thank you! ❤️

--

--

Jan Grodowski
Jan Grodowski

Written by Jan Grodowski

Maker of UndercoverCI, writer of software, road cyclist

No responses yet