Coverage

Starting coverage

Coverage has to start before the code under test is loaded, or the lines are already compiled without the counter. The test file starts it first, then requires the library:

require "coverage" Coverage.start(lines: true) require_relative "../lib/calculator" require "minitest/autorun"

Coverage.start takes the kinds of coverage: lines, branches, methods and oneshot_lines. Line coverage is the cheap default; branch coverage answers the harder question of whether each side of an if ran.

The report

Coverage.result returns a hash keyed by file. Each value has a :lines array with one entry per line: nil for a line that cannot run, zero for a line that did not, a positive count for a line that did. The summary is four lines of arithmetic:

Minitest.after_run do Coverage.result.each do |file, data| next unless file.end_with?("lib/calculator.rb") lines = data[:lines] covered = lines.count { |n| n && n.positive? } total = lines.count { |n| !n.nil? } puts format("%s: %d/%d lines (%.0f%%)", File.basename(file), covered, total, covered * 100.0 / total) end end

The filter on the file name matters: a report that includes the test file and every gem reports a number that means nothing. A project reports the files it owns.

Running it

ruby test/coverage_test.rb

Finished in 0.000829s, 1206.7224 runs/s, 1206.7224 assertions/s. 1 runs, 1 assertions, 0 failures, 0 errors, 0 skips calculator.rb: 3/5 lines (60%)

The test only calls add, so the two lines of divide and its guard are uncovered and the percentage is honest. Add a test for the zero case and the same report moves to 100% — which is the moment to read the next section.

Reading it honestly

What to watch for

Starting coverage after a require misses the code that was already loaded, which is why the order in the first block is not cosmetic.

A coverage run in the same process as a profiler or a reloader measures the tool as well as the code. Run coverage in its own invocation, or the numbers drift between machines.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does this program print? lines = [1, 1, nil, 1, 0, nil, 0, nil, nil] covered = lines.count { |n| n && n.positive? } total = lines.count { |n| !n.nil? } puts format("%d/%d (%.0f%%)", covered, total, covered * 100.0 / total)

    Answer

    3/5 (60%)

    The nil entries are lines that cannot run, so the total counts the five executable ones and three of them were hit.

  2. Why must Coverage.start run before the library is required?

    Answer

    The counter is installed when the file is compiled

    A file compiled before the counter exists has no instrumentation, which is why the order in the block matters.

  3. What does a nil entry in the :lines array mean?

    Answer

    The line cannot run, such as a blank line or a comment

    A line that is not executable has nothing to count, while a zero means an executable line never ran.

  4. Which kinds can Coverage.start measure?

    Answer

    Lines, branches, methods and oneshot_lines

    The library names those four kinds, and branch coverage is the one that answers the harder question.

  5. Why does the report filter on the file name?

    Answer

    A report that includes the test file and every gem gives a number that means nothing

    A project reports the files it owns, which is what the percentage is about.

  6. What does a covered line prove?

    Answer

    That the line ran, whether or not anything was asserted

    Coverage counts execution, so a test that calls a method and asserts nothing still counts its lines.

  7. Why is 100% a floor rather than a goal?

    Answer

    The last percent is often an error branch that is hard to reach, and chasing it writes tests of the test

    The page warns that a team chasing the last branch writes tests that check the test rather than the code.

  8. What does branch coverage add to line coverage?

    Answer

    It says both outcomes of a condition ran

    A line can run while only one side of its if was ever taken, which is the gap branch coverage closes.

  9. Which coverage gate is the useful one?

    Answer

    A changed file in a commit

    A gate on a changed file catches the untested branch a commit adds, while a repository gate blocks the first refactor.

  10. Why should a coverage run not share a process with a profiler or a reloader?

    Answer

    It measures the tool as well as the code, so the numbers drift

    The counter sees whatever runs in the process, so coverage belongs in its own invocation.

Article author: Arthur Isaev