Skip to content

Test coverage gap when using defstruct #13761

@liamwhite

Description

@liamwhite

Elixir and Erlang/OTP versions

Erlang/OTP 27 [erts-15.0] [source] [64-bit] [smp:32:32] [ds:32:32:10] [async-threads:1] [jit:ns]

Elixir 1.17.0 (compiled with Erlang/OTP 27)

Operating system

Linux

Current behavior

After creating a project structure with the following files:

# mix new coverage && cd coverage

# cat > lib/coverage.ex 
defmodule Coverage do
  defstruct x: %{},
            y: %{},
            z: %{}

  def new(opts) do
    struct!(__MODULE__, opts)
  end
end

# cat > test/coverage_test.exs
defmodule CoverageTest do
  use ExUnit.Case

  test "exercises all functionality" do
    assert %{} = Coverage.new(x: "x", y: "y", z: "z")
  end
end

running mix test --cover produces a report where only 50% of the module has coverage:

Percentage | Module
-----------|--------------------------
    50.00% | Coverage
-----------|--------------------------
    50.00% | Total

However, every executable line contained in this file was executed. In the HTML report, one can observe that this is caused by the defstruct line, presumably due to the two clauses of the __struct__ function:
image

Expected behavior

This test run should have 100% coverage.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions