Code Coverage results seem counterintuitive

coverage

#1

Assume I have two files. main.jl with some functions and test.jl with unit tests. I want to find out which lines in main.jl are covered by the unit tests.

main.jl

struct MyStruct
  a
  b
end

function to_be_covered(x)
  a = x
  i = 0
  for n in eachindex(x)
    i += 1
    x[n] = 2 * x[n]
  end
  return x, i
end

get_b(x::MyStruct) = x.b

function to_be_covered_two(x)
  a = x.a
  b = get_b(x)
  return a + b
end

test.jl

using Test, LinearAlgebra

include("./main.jl")

r1, r2 = to_be_covered([1; 2])

@testset "All tests" begin
  @test r1 == [2; 4]
  @test r2 == 2
  @test to_be_covered_two(MyStruct(1, 2)) == 3
end

If I run julia --code-coverage=user (with Julia v.1.0.1) and then include("test.jl") I get the following report:

        - struct MyStruct
        2   a
        -   b
        - end
        - 
        - function to_be_covered(x)
        1   a = x
        -   i = 0
        1   for n in eachindex(x)
        -     i += 1
        2     x[n] = 2 * x[n]
        -   end
        1   return x, i
        - end
        - 
        - get_b(x::MyStruct) = x.b
        - 
        - function to_be_covered_two(x)
        1   a = x.a
        -   b = get_b(x)
        1   return a + b
        - end

A few things about the report seem counter-intuitive to me:

  1. The line with b in Mystruct wasnt executed.
  2. The lines i = 0 and i += 1 weren’t executed.
  3. The line b=get_b(x) and the corresponding function get_b(x::MyStruct) = x.b weren’t executed.

#2

Which version of Julia are you using? Note that coverage calculations are not perfect (but improved a lot with 1.1).


#3

Above result is with Julia v.1.0.1.

With Julia v1.1 I get:

        - struct MyStruct
        2   a
        -   b
        - end
        - 
        - function to_be_covered(x)
        1   a = x
        -   i = 0
        2   for n in eachindex(x)
        2     i += 1
        3     x[n] = 2 * x[n]
        -   end
        1   return x, i
        - end
        - 
        1 get_b(x::MyStruct) = x.b
        - 
        - function to_be_covered_two(x)
        2   a = x.a
        1   b = get_b(x)
        1   return a + b
        - end

which seems to capture more of the lines