i-n-g-m-a-r

i-n-g-m-a-r

How to improve phoenix test coverage?

Source

$ mix test --cover
......
Generating cover results ...

Percentage | Module
-----------|--------------------------
     0.00% | DemoWeb.Layouts
     0.00% | DemoWeb.PageHTML
    16.67% | DemoWeb.CoreComponents
    25.00% | Demo.DataCase
    50.00% | Demo.Repo
    50.00% | DemoWeb.ErrorHTML
    75.00% | Demo.Application
    75.00% | DemoWeb.Router
    80.00% | DemoWeb.Telemetry
   100.00% | Demo
   100.00% | Demo.Mailer
   100.00% | DemoWeb
   100.00% | DemoWeb.ConnCase
   100.00% | DemoWeb.Endpoint
   100.00% | DemoWeb.ErrorJSON
   100.00% | DemoWeb.Gettext
   100.00% | DemoWeb.PageController
-----------|--------------------------
    27.41% | Total

Coverage test failed, threshold not met:

    Coverage:   27.41%
    Threshold:  90.00%

Generated HTML coverage results in "cover" directory

Showing Posts 1 to 10

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Hi @i-n-g-m-a-r I’m confused you’re asking a question but then also linking to a blog post that answers your question. Can you elaborate as to what you’re looking for?

dimitarvp

dimitarvp

Most, if not all, of these files are pretty safe to put in a ignore clause for code coverage. I mean how would you meaningfully have a 100% test coverage of a telemetry module that’s mostly generated code?

i-n-g-m-a-r

i-n-g-m-a-r OP

Hi Ben,

I’m looking for actual test coverage.
With regard to the (ignore_modules) answer this would feel acceptable:

test_coverage: [
  ignore_modules: [
    # DemoWeb.Layouts,
    # DemoWeb.PageHTML,
    # DemoWeb.CoreComponents,
    Demo.DataCase,
    Demo.Repo,
    # DemoWeb.ErrorHTML,
    Demo.Application,
    DemoWeb.Router,
    DemoWeb.Telemetry
  ]
]

To exclude every single view does not feel acceptable.
I don’t know how to make a view pass any test.
With regard to CoreComponents it would be nice to have a working example.
Then we could update the tests after making changes to (modules using) CoreComponents.

While on the topic of testing phoenix applications,
I find myself putting this kind of not very pretty code into ConnCase:

using do
  quote do
    # ...

    def put_session(conn, session) when is_map(session) do
      Enum.reduce session, conn, fn {k, v}, c ->
        put_session c, k, v
      end
    end

    def run(conn, session \\ %{}, fun) do
        old = conn
              |> build_conn
              |> put_session(session)
              |> load_cookies

        ref = "#{conn.scheme}://#{conn.host}"
              |> Path.join(conn.request_path)

        build_conn()
        |> put_skb
        |> Plug.Test.recycle_cookies(old)
        |> put_req_header("referer", ref)
        |> fun.()
    end

    def build_conn(%{resp_cookies: resp_cookies}) do
      build_conn()
      |> put_skb
      |> put_cookies(resp_cookies)
      |> fetch_session
    end

    defp put_skb(%_{secret_key_base: nil} = conn) do
      conn
      |> Map.put(:secret_key_base, @skb)
      |> Plug.Session.call(@session_opts)
    end

    defp put_skb(conn), do: conn

    defp load_cookies(%_{private: private} = conn) do
      Enum.reduce(private[:before_send] || [], %{conn | state: :set}, & &1.(&2))
    end

    defp put_cookies(conn, resp_cookies \\ nil) do
      Enum.reduce resp_cookies || conn.resp_cookies, conn, fn {key, %{value: value}}, acc ->
        put_req_cookie acc, key, value
      end
    end
  end
end

In order to test all kinds of scenario’s involving sessions / cookies.
Probably this kind of code shouldn’t be necessary.

i-n-g-m-a-r

i-n-g-m-a-r OP

The generated code could be tested / covered ?

dimitarvp

dimitarvp

Why should it? And how do you test the generated telemetry module, for example? It serves as a callback / handler that’s registered elsewhere; not in your application logic.

D4no0

D4no0

What do you mean, what about 100% coverage :joy: ?

BTW totally agree, this is definitely not the responsibility of the developer to test generated modules, even if they are theoretically now part of your application.

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Taking a bit of a step back, I wonder if there is perhaps gaps in how the test coverage is measured? If he’s got basic tests to render say the front page you’d expect that to run the DemoWeb.PageHTML and Layouts modules.

D4no0

D4no0

To be fair I don’t understand even to this day what this metric achieves, the fact that tests execute 100% of your code, doesn’t mean that the tests won’t miss a bug.

In the world of agile development, where we make software that evolves over time, having 100% coverage is like shooting yourself in the leg, a lot of effort to write tests, tests are brittle because they cover all implementation details, a lot of effort to change code and rewrite tests.

I even worked at a company where they implemented mutation testing, where it would be literally impossible to change a line of code without a couple of tests failing.

i-n-g-m-a-r

i-n-g-m-a-r OP

Hi Ben,

This is a regular pattern:

     0.00% | MyappWeb.ThingHTML
   100.00% | MyappWeb.ThingController

Where ThingController is tested using (also) lots of different ConnCase calls (get, post, etc).

i-n-g-m-a-r

i-n-g-m-a-r OP

Personally I would trust phoenix code to be well tested.
So most of the time I’m more rigorous regarding my own code.
Sometimes though I will collaborate with or work for people that demand good coverage.
Starting with near 100% would be nice.

Where Next? Top

Trending in Questions Top

itzmidinesh
Hi all, I’m working on a Phoenix project where I need to make external HTTP requests and I’m trying to choose between Req and HTTPoison....
New
yarrichar
I’m very new to Elixir, so sorry if this is a silly question: I am wondering what happens to the mailbox of a process when the machine i...
New
pragdave
How do I tell Hologram not to escape HTML when substituting into templates? {Textile.render(@product.long_description)} escapes the HTM...
New
Fl4m3Ph03n1x
Background When trying to execute mix release on a Windows 11 machine for a Phoenix project I get the following error: * assembling mark...
New
rlipscombe
I’d like to create a file with 600 permissions (rw-------), but there’s a race condition if I create it and then change the permissions. ...
New
saleyn
Is there a way to import record definitions in the iex shell for pretty printing (similar to the rr command in the erl shell)? This woul...
New
rlipscombe
When I use Mix.install with a git dependency, using git@github.com:... how do I change the SSH identity used? I’ve got two Github accoun...
New

Other Trending Topics Top

dominicletz
Hey there, I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first. ...
New
raa-ziq
Hi everyone :waving_hand: I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New
kip
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews