kelvinst

kelvinst

Why is defmodule line marked as executable on mix test --cover?

When I run mix test --cover in my Phoenix project, I get some files with poor coverage because they are tiny modules with a use Something, and some of them mark the defmodule X do line as executable.

e.g.

Any idea why?

Marked As Solved

sodapopcan

sodapopcan

This really piqued my curiosity so I put together a little test and it seems you’re… half right? I’m not exactly known for being able to come up with the most accurate scenarios for these things but it appears you just need to use something from the dependency module.

With this test:

defmodule CoverTest do
  use ExUnit.Case

  test "foobars the world" do
    assert Cover.hello() == "foobar"
  end
end

…and this dependency module:

defmodule Cover.Dep do
  defmacro __using__(_) do
    quote do
      def foo, do: "foo"
      def bar, do: "bar"
    end
  end
end

…this gets 50% coverage:

defmodule Cover do
  use Cover.Dep

  def hello do
    "foobar"
  end
end

…and this gets 100%:

defmodule Cover do
  use Cover.Dep

  def hello do
    foo() <> bar()
  end
end

…which I thought proved your theory right until I tried this:

defmodule Cover do
  use Cover.Dep

  def hello do
    foo() <> "bar"
  end
end

…which also scores 100% :face_with_spiral_eyes:

So ya, I know your post is only 5 hours old but BUMP for anyone who knows! :slight_smile:

EDIT: Oh, perhaps it’s because it doesn’t consider strings executable so the fact that you are using anything from the module means it’s covered?

Also Liked

christhekeele

christhekeele

I’m only 98.33% guilty of this, but otherwise agree!

kelvinst

kelvinst

It scores 100% because as foo and bar are defined by Cover.Dep, using any function defined from it would mark the defmodule line as covered. The defs inside quotes aren’t marked as executable by the coverage tool at all.

Anyway, thanks for the investigation! That’s definitely why I posted here, cause I knew there was going to be someone as curious as I am - but not as lazy as I was - that would test it :smile:

sodapopcan

sodapopcan

I feel so used :sweat_smile:

Right, that makes sense, thanks!

Schultzer

Schultzer

You could fix it, by improving Erlang cover tool, although it might also be a time to take a few steps back and meditate. Code coverage is a bad metric to begin with, and especially if the goal is to get to a 100%.

It’s great to get an idea if there is something you have missed testing for or even dead code. But that is all it is. I really wish that the report was inverted instead of fueling an addiction, which I’m 100% guilty off.

Where Next?

Popular in Questions Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
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
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
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
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
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
Mooodi
Given a string, how can I get access to its character by index? Enum.at("my_string", 2) doesn't work. Or rather, not char, but a substr...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

Other popular topics Top

yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
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
William
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
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
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New

We're in Beta

About us Mission Statement