12

I'm trying to launch IEx.pry within a test. However I cannot get to run the tests within an iex session. Note that I'm not using mix.

ExUnit.start

defmodule Calc do
  def add(a,b) do
    a + b
  end
end

defmodule TheTest do
  use ExUnit.Case

  test "adds two numbers" do
    require IEx
    IEx.pry
    assert Calc.add(1, 2) == 3
  end
end

I try run it with ExUnit.run hangs and eventually times out:

manuel@laptop:~/exercism/elixir/nucleotide-count$ iex test.exs             
Erlang/OTP 18 [erts-7.3] [source] [64-bit] [smp:4:4] [async-threads:10] [kernel-poll:false]

Interactive Elixir (1.3.4) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> ExUnit.run
** (exit) exited in: GenServer.call(ExUnit.Server, {:take_async_cases, 8}, 60000)
** (EXIT) time out
 (elixir) lib/gen_server.ex:604: GenServer.call/3
(ex_unit) lib/ex_unit/runner.ex:71: ExUnit.Runner.loop/2
 (stdlib) timer.erl:166: :timer.tc/1
(ex_unit) lib/ex_unit/runner.ex:13: ExUnit.Runner.run/2

The code is loaded correctly and I can invoke it directly with TheTest."test adds two numbers"({}). But I was hoping to do this launching the whole suite.

Manuel M
  • 469
  • 4
  • 13
  • 2
    Why are you not using `mix`? – Simone Nov 25 '16 at 13:07
  • You may find this helpful http://stackoverflow.com/questions/29671156/pry-while-testing – Simone Nov 25 '16 at 13:14
  • I believe since you are not using `mix` you should be brave enough to start `ExUnit` server application yourself. – Aleksei Matiushkin Nov 25 '16 at 13:46
  • 1
    Thanks @simone, that answer assumes that this is a `mix` project. Which is not the case. I did not expect to need to setup a mix project to be able to do this in the same way that I did not expect to setup Rake to run Pry within RSpec. I hope that's not the answer. – Manuel M Nov 25 '16 at 14:05
  • Good tip @mudasoba, but `:observer.start` shows `Elixir.ExUnit.Server` running. Is that what you were talking about? If so, the problem must be somewhere else. – Manuel M Nov 25 '16 at 14:07

3 Answers3

9

I am assuming you are not using mix. You need to load the test cases to the ExUnit server before running them.

Before Elixir v1.6 you would load the tests like this:

ExUnit.Server.cases_loaded()

And after Elixir v1.6 you would load them like this (thanks to @jeffreymatthias):

ExUnit.Server.modules_loaded()

So the code you should write in iex should be:

ExUnit.start()

defmodule Calc do
  def add(a,b) do
    a + b
  end
end

defmodule TheTest do
  use ExUnit.Case

  test "adds two numbers" do
    require IEx
    IEx.pry()
    assert Calc.add(1, 2) == 3
  end
end

ExUnit.Server.modules_loaded() # Or ExUnit.Server.cases_loaded()

ExUnit.run()

I hope this helps.

Alex de Sousa
  • 1,531
  • 12
  • 14
0

According to the ExUnit documentation, ExUnit.run/0 should only be used if you don't want to autostart your tests when you call ExUnit.start/1.

You always have to call ExUnit.start() which would automatically run all the tests unless you pass autorun: false.

narrowtux
  • 664
  • 7
  • 24
0
alias ExUnit.Assertions
require Assertions
Assertions.assert 1==1
true
double-beep
  • 5,031
  • 17
  • 33
  • 41
Chris
  • 1