Software Developer

Enough Coverage To Beat The Band

Thank You! πŸ”—

Thanks for checking out my talk! To keep in touch, you can join my newsletter or follow me on the Internet.

Presentation Resources πŸ”—

Code Coverage Modes πŸ”—

Each mode answers a different question about the code that was run under coverage:

  • Lines - how many times was each line executed?
  • Oneshot Lines - which lines were executed?
  • Methods - how many times was each method executed?
  • Branches - how many times was each conditional executed?

Lines πŸ”—

🎸

This is the β€œclassic” implementation of providing coverage. Each relevant line, that is those that aren’t things like empty lines or end statements, has a counter that is incremented each time the line is visited in code execution while coverage is running. At the conclusion, you will see how many times each line is executed.

Benefits πŸ”—

  • This is the default mode for coverage.
  • Most of the time, this option will provide you with the information you want.

Oneshot Lines πŸ”—

🎹

Similar to lines coverage, this also documents that a relevant line was executed while coverage was running. However, it’s a binary report of whether it was executed or not. It will not tell you how often. This may be sufficient in many cases, and comes with the benefit of being more performant every subsequent time a particular line of code is executed under coverage.

Benefits πŸ”—

  • Oneshot provides you with nothing more than if a line of application code is executed in a test suite.
  • As long as being constrained to knowing if something ran or not, and not knowing how often, is sufficient, oneshot lines coverage provides the same feedback as lines coverage with better performance.

Methods πŸ”—

πŸ’‘

Methods coverage brings the granularity of lines coverage up to a coarser grain. Rather than tracking individual lines, it’s concerned with whether a particular method is executed. It can be a 10 line method where the first line is the only line ever executed. Methods coverage will still consider that as executed the same as a 20 line method where each line is executed.

Benefits πŸ”—

  • This has a targeted focus to be able to answer a more specific question - is this method executed? - with easier to process feedback.

Branches πŸ”—

🎀

Branches coverage tracks execution of different conditional paths and documents how often those different paths are run. The unique benefit that this provides over lines coverage is in conditionals that execute multiple code paths in a single line, such as ternary statements. You may have a part of that conditional that’s never run or tested, but you wouldn’t know that if you’re relying on lines coverage alone.

Benefits πŸ”—

  • It provides a different frame of reference than lines coverage, which ends up being either coarser or more granular than line coverage in different situations.
  • For conditionals that lay out multiple code paths on a single line, this provides feedback on their individual execution where lines coverage only considers whether any part of the line was run.
  • When interested in conditionals, and only conditionals, it has less noise than lines coverage.

Gem Support πŸ”—

If you’d like to leverage existing abstractions on top of ruby’s coverage module in your current applications, I recommend:

  • SimpleCov for test coverage. It now supports branches coverage!
  • Coverband for measuring coverage of production code. Here’s an example on how to configure it to run using oneshot lines coverage.

Supplemental Content πŸ”—

I have a series of blog posts using the live music examples I wrote for the talk to describe various software design principles. The first post is about dependency injection. You can see the full series here.

Presentation Fonts πŸ”—

The main content of all my slides, for code examples and similar text, is Arial Narrow.

Slide titles and big statement fonts are The Fragile, available from The NIN Hotline.

Supplemental content, like my twitter handle and text pointing to areas explaining what coverage output represents are The Wretched, also available from The NIN Hotline.

Presentation Photo Credits πŸ”—

All photos used in the presentation are from the band Nine Inch Nails, and released on their flickr account with a Creative Commons Attribution-NonCommercial-ShareAlike 2.0 Generic license.

Below I’ve embedded all the images used to link directly to their original source.

Introduction πŸ”—

Nine Inch Nails Live @ Lollapalooza - Chicago, IL, 8.03.08 Nine Inch Nails live @ PNC Bank Arts Center, Holmdel, NJ, 6.06.09

Lines Coverage πŸ”—

Nine Inch Nails Live @ Shoreline Amphitheatre - Mountain View, CA, 5.22.09 Nine Inch Nails Live @ Shoreline Amphitheatre - Mountain View, CA, 5.22.09 Nine Inch Nails Live: Tension 2013 Nine Inch Nails live @ Bonnaroo Festival in Manchester, TN, 6.13.09 Nine Inch Nails Live @ Cricket Wireless Amphitheater - Chula Vista, CA, 5.16.09 Nine Inch Nails live @ Starlight Theatre, Kansas City, MO, 5.27.09

Oneshot Lines Coverage πŸ”—

Nine Inch Nails Live @ Sasquatch Festival in George, WA, on 5.24.09 Nine Inch Nails Live @ Bell Centre - Montreal, QC, 11.12.08 Nine Inch Nails Live @ Air Canada Centre - Toronto, ON, 8.05.08 Nine Inch Nails Live @ Santa Barbara Bowl - Santa Barbara, CA, 5.21.09 Nine Inch Nails Live @ Razzmatazz - Barcelona, Spain, 2.19.07 Nine Inch Nails Live @ Cricket Wireless Amphitheater - Chula Vista, CA, 5.16.09 Nine Inch Nails Live @ Olympia - Paris, France, 2.21.07 Nine Inch Nails Live @ Seagate Convention Center - Toledo, OH, 8.25.08 Nine Inch Nails Live @ Gwinnett Arena - Duluth, GA, 8.13.08 Nine Inch Nails Live @ Arena Santiago - Santiago, Chile, 10.04.08

Methods Coverage πŸ”—

Nine Inch Nails Live: Tension 2013 Nine Inch Nails Live @ The Pearl - Las Vegas, NV, 5.18.09 Nine Inch Nails Live: Tension 2013 Nine Inch Nails Live: Tension 2013 Nine Inch Nails Live: Tension 2013 Nine Inch Nails Live: Tension 2013 Nine Inch Nails Live @ Pemberton Festival - Pemberton, BC, 7.25.08 Nine Inch Nails Live @ Pemberton Festival - Pemberton, BC, 7.25.08 Nine Inch Nails Live @ Q Arena - Cleveland, OH, 8.22.08 Nine Inch Nails Live @ Coliseum - Lisbon, Portugal, 2.11.07 Nine Inch Nails Live @ Key Arena - Seattle, WA, 7.26.08

Branches Coverage πŸ”—

Nine Inch Nails Live @ DCU Center - Worcester, MA, 11.09.08 (with surprise guest Peter Murphy) Soundcheck in Buenos Aires, Argentina - 10.01.08 Soundcheck in Buenos Aires, Argentina - 10.01.08 Nine Inch Nails Soundcheck - Pemberton, BC, 7.24.08 Nine Inch Nails Soundcheck - Pemberton, BC, 7.24.08 Soundcheck in Mexico City, Mexico - 10.17.08 Nine Inch Nails Live @ House Of Blues - Atlantic City, New Jersey, 11.06.08 (with surprise guest Peter Murphy)

Closing πŸ”—

Nine Inch Nails live @ DTE Energy Music Theatre, Clarkston, MI, 5.31.09