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:
- The line with
b
inMystruct
wasnt executed. - The lines
i = 0
andi += 1
weren’t executed. - The line
b=get_b(x)
and the corresponding functionget_b(x::MyStruct) = x.b
weren’t executed.