How to debug ExUnit tests with :debugger?

I have the idea to use the (Erlang) debugger to debug my Elixir tests, using breakpoints set via the UI and stepping through code by line or expression. I also want to avoid littering the code with Pry or breakpoint statements. Is that something that’s possible?

I searched docs and more broadly, but I didn’t find a conclusive answer yet.

My understanding for now is, that the debugger needs to be started from the iex repl, but there doesn’t seem to be reliable way to run tests from there.

Can you call :debugger.start from within the failing test / or setup block? Or before the module?

I don’t see how I would set breakpoints then, before the test has finished.

I meant something like

:debugger.start()
:int.ni(DebugTest)
:int.break(DebugTest, 16) # needs to be an executable line http://erlang.org/doc/apps/debugger/debugger_chapter.html#breakpoints
# or use http://erlang.org/doc/man/int.html#break_in-3
# :int.break_in(DebugTest, :hello, 0)

defmodule DebugTestTest do
  use ExUnit.Case
  doctest DebugTest

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

and then mix test --trace.


If you only want to “look around” in the test block, I think it’d be a bit simpler with IEx.pry

defmodule DebugTestTest do
  use ExUnit.Case
  doctest DebugTest
  require IEx

  test "greets the world" do
    result = DebugTest.hello()
    IEx.pry()
    assert result == :world
  end
end

and then iex -S mix test --trace.

5 Likes

Thanks! The first approach works well.

(Although I was surprised to see a lot of metadata and mangled names for parameters. I was assuming the Elixir layer was more immediate above the Erlang…? But that’s tangential.)

With the second suggesting I didn’t see a way to step through the running code. Are you using that? For me that is pretty essential, in cases where I want to debug at all.

Yeah, there is no way to “step through” with IEx.pry. I still use it, yes, usually right before the assert macros to have a “look around”.

1 Like

yep, gotcha. In my case it’s about an algorithm that is recursive, so it would hit the line in code multiple times. That is a bit annoying if there isn’t any conditional break feature. That’s the reason I prefer a full debugger here.