numso

numso

Code Coverage incorrect for function with multiple clauses

Hi all!

I noticed something odd while testing one of my LiveViews today. I wrote a test for one of the handle_event/3 function clauses. Made the test pass. Verified that the assigns were getting set correctly. Then discovered that code coverage wasn’t marking that code as having been run. I added an IO.inspect/2, saw the log in my test output, but code coverage still says that IO.inspect line was never run.

I was able to make a very minimal reproduction in a brand new mix new project:

elixir 1.17.3-otp-27
erlang 27.1

defmodule CoverageBug do
  def hello do
    check("target", nil)
    :world
  end

  def check("one", _) do
    nil
  end

  def check("two", _) do
    nil
  end

  def check(_, %{some_match: :three}) do
    nil
  end

  def check("four", _) do
    nil
  end

  def check("target", _) do
    IO.inspect("This line is run but not covered")
    nil
  end
end

defmodule CoverageBugTest do
  use ExUnit.Case

  test "greets the world" do
    assert CoverageBug.hello() == :world
  end
end

When running mix test --cover, you’ll see the IO.inspect line logged out in the test output. But it won’t show up as covered in the test coverage report. If you delete any of those first 4 check function clauses, then the IO.inspect in the 5th clause will start being reported as covered. Has anyone experienced anything like this? Am I missing something obvious?

Marked As Solved

numso

numso

I ended up trying this with a different version of erlang and it worked:

elixir 1.17.3-otp-26
erlang 26.2.5.3

So looks like this was a regression in the latest erlang major release.

Where Next?

Popular in Questions Top

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
kostonstyle
Hi all I want to have a unix time, from the current time plus 1 hour. DateTime.now + 1 hour How to get it in elixir? Thanks
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

Other popular topics Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

We're in Beta

About us Mission Statement