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

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
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
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
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
Codball
Mix format works fine if run from the cmd. I’ve followed this to facilitate the implementation into VSC which involves downloading an ext...
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

Other popular topics Top

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
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
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement