pmonson711
Surprising lines missing from coverage results
Hey Elixir-land!
It’s been a while since I’ve been around. Getting back into lots of elixir work and I found something unexpected regarding the coverage system. Essentially function calls wrapped in literals don’t seem to get counted as coverable statements.
Steps to reproduce:
- run
mix new coverage_why - paste in
lib/coverage_why.ex
defmodule CoverageWhy do
def hello do
:world
end
def i_has_coverage do
hello()
end
def i_has_no_coverage do
{:ok, hello()}
end
def i_has_no_coverage_as_well do
[hello()]
end
def i_has_no_coverage_as_either do
[hello()]
:ok
end
end
- run
mix test --cover
I get the results of
Expectations
I would expect all the lines except the original def hello... to be uncovered. If I had to guess the wrapping the call in a literal causes the function call to be inlined somehow. Is this the expected coverage results and is it documented anywhere?
Trending in Questions
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated!
To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead.
Sta...
New
Hello !
We want new/edit form pages to POST/PUT to their own URL rather than the resources REST defaults (post /things, put /things/:id)...
New
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app?
Looking for hints regarding:
Addi...
New
Hi all, I wanted to ask how the community is dealing with post-release steps.
Today we have Ecto migrations, which make sure that the db...
New
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New
Other Trending Topics
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #elixirconf-us
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex











First 10 of 15 Posts!
josevalim
Please open up a bug report, we can likely fix the tuple and list ones, but not the atom because literals do not have line numbers in the AST.
EDIT: I have fixed this locally and I will push it soon.
pmonson711
Amazed with your response!
https://github.com/elixir-lang/elixir/issues/10847
semmitmondo
I think I just ran into the same issue.
Lines in the source files containing simple terms, that have no metadata in thier AST representations (e.g.: single atoms, 2-tuples with only no-metadata elements, lists os any length with only no-metadata elements, binary literals, things like that) are kicked out of code coverage tests, which is a big problem. (For instance, a function or case clauses returning
:ok,{:error, :not_found}and such simple expressions are just not counted.) I can wrap them intoelem({X}, 0)where X is the simple expression to force the coverage tests to work properly, but this is just plain ugly.Is it an option to somehow change the Elixir compiler in order to compile the source with some tricks like the above one, when compiling for the
:testenv? This does not change the semantics of the code, but fixes the coverage problem.josevalim
What is your Elixir version? We have improved this in more recent releases.
semmitmondo
I tried it with Elixir 1.12.2 and 1.12.3 (on Erlang/OTP 23.2.3). Also tried it with 1.12.3 on Erlang/OTP 24.0.6.

:okand{:error, :reason}lines are not included in coverage reports on these versions for me.josevalim
Yup, I could reproduce it. Fixes coming to Elixir master shortly.
semmitmondo
Found the commit on master, thanks for the improvements. As far as I can see, case patterns are now covered, which is great. It is now visible which case branch was tested. But bodies with atoms and 2-tuples are still missing. I also miss the
elseline of anifmacro. Here only theif ... dopart comes up in the coverage test report. The:errorand theelselines don’t:In case of a
case, thecase ... doline and the patterns are included now, not the atom and 2-tuple:Is it impossible to also cover those terms with no metadata in their AST? (Actually, if you could cover the clause bodies, then case pattern coverage has little information.)
josevalim
They are not missing per se. If the pattern was covered, then by definition what immediately follows the pattern is being covered too. We can’t highlight it because we don’t have precise line information, but the coverage of the pattern is enough in those cases.
semmitmondo
Well, in case of the
casestatement, it is fine. But theifstatement above is a good example when this is not enough. Theif .. doline is highlighted, but none of the following 4 lines are, so we cannot know if we covered both thethenbranch and theelsebranch, or just one of them.Isn’t it a solution if you wrap those values without line information into an identity function call when compiling for test env? If you could compile them into something with line info in it, that, when executed, produces the original value, that would be great.
semmitmondo
Thank for fixing the
caseproblem, José. I can say that almost everything is fine with coverage tests now. There are only a few parts missing, namely theif,unlessand thewithspecial forms still need some fixing. I can give you example codes about the problem:withThe problem with this code is that none of the lines between the
doand theendlines are flagged for coverage. Only the first two lines of the code above can be green or red. (I put thedoto a new line just to see if it gets red/green, but unfortunately it didn’t.)The good news is that
tryhas a very similarelsebranch, that works perfectly well, so maybe this can be fixed just by mimicking howtry..rescue..elseworks.ifandunlessIn this case lines from 2 to 5 are not coverable (not green, nor red, just plain white). The exact same is true is you replace
ifwithunless. This is very sad.I also have to mention that there are other special forms that are fine.
caseandcondare now OK, just likereceive(even withafter),tryanddeffunction definitions.Is there any hope these errors are gonna be fixed?
Last Post!
josevalim
As mentioned earlier:
There is no option besides making
I thought we would be able to use Erlang’s maybe, but guard support is missing.
withraise false warnings for those using Dialyzer