# Code Coverage results seem counterintuitive

**URL:** https://discourse.julialang.org/t/code-coverage-results-seem-counterintuitive/20356
**Category:** Tooling
**Tags:** coverage
**Created:** [February 1, 2019, 1:46pm UTC](https://discourse.julialang.org/t/code-coverage-results-seem-counterintuitive/20356 "2019-02-01T13:46:47Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![migarstka](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/migarstka/32/3828_2.png) [@migarstka](https://discourse.julialang.org/u/migarstka)
#### Post date: [February 1, 2019, 1:46pm UTC](https://discourse.julialang.org/t/code-coverage-results-seem-counterintuitive/20356/1 "2019-02-01T13:46:47Z")

</div>

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**

```julia
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**

```julia
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:

```julia
        - 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.

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [February 1, 2019, 2:16pm UTC](https://discourse.julialang.org/t/code-coverage-results-seem-counterintuitive/20356/2 "2019-02-01T14:16:30Z")

</div>

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

---

<div class="post-metadata">

### Author: ![migarstka](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/migarstka/32/3828_2.png) [@migarstka](https://discourse.julialang.org/u/migarstka)
#### Post date: [February 1, 2019, 2:55pm UTC](https://discourse.julialang.org/t/code-coverage-results-seem-counterintuitive/20356/3 "2019-02-01T14:55:14Z")

</div>

Above result is with Julia v.1.0.1.

With Julia v1.1 I get:

```julia
        - 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
