Fl4m3Ph03n1x

Fl4m3Ph03n1x

How to run ExUnit tests with erlang debugger?

Background

I have a small function with some branching logic and I have some tests for it. I would like to use the erlang debugger when I run a given test to make sure I am executing the correct code.

Code

Lets assume we are testing DebuggerTest.greet/1:

defmodule DebugerTest do
  def greet(greeting) do
    if greeting == "Hola" do
      str = "#{greeting} mundo!"
      IO.puts(str)
      :spanish_greet
    else
      str = "#{greeting} world!"
      IO.puts(str)
      :normal_greet
    end
  end
end

Now, this function has some branching logic, and the code is overall horrible, but it does give us a lot of juicy executable lines that we attach a breakpoint to.

These are the tests I am running:

defmodule DebugerTestTest do
  use ExUnit.Case
  doctest DebugerTest

  test "greets the world" do
    assert DebugerTest.greet("Hello") == :normal_greet
  end

  test "greets the world in spanish" do
    assert DebugerTest.greet("Hola") == :fail_greet
  end
end

As you can see, my spanish greeting test will fail. However, I want to find that out using the erlang debugger:

Running the tests

Given that I am only interested in running the failing test, I change my code to:

@tag :wip
 test "greets the world in spanish" do
    assert DebugerTest.greet("Hola") == :fail_greet
  end

And then I run mix test --only wip.

There, now I am running only the failing test. But I am still not using the debugger.
To try to fix this I tried running the tests via an iex shell iex -S mix test --only wip but the run is automated and it ends before I can setup the debugger:

:debugger.start()
:int.ni(DebugerTest)
:int.break(DebugerTest, 3)

Question

How can I run the erlang debugger when using mix test ?

Marked As Solved

stefanchrobot

stefanchrobot

Seems to be working for me:

  test "greets the world in spanish" do
    :debugger.start()
    :int.ni(DebugerTest)
    :int.break(DebugerTest, 3)
    assert DebugerTest.greet("Hola") == :fail_greet
  end

and then:

iex -S mix test --trace

waits for the debugger to finish.

Also Liked

lukaszsamson

lukaszsamson

ElixirLS Core Team

ElixirLS VSCode extension supports running tests with debugging right from the UI. Navigate to Testing tab and click Debug test or right click an icon next to test definition and select Debug test.

Where Next?

Popular in Questions Top

joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

Other popular topics Top

joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
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
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
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44265 214
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New

We're in Beta

About us Mission Statement