如何在IEx中运行ExUnit测试 [英] How to run ExUnit tests within IEx
问题描述
我正在尝试在测试中启动 IEx.pry
。但是,我无法在iex会话中运行测试。请注意,我没有使用混合。
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
我尝试在 ExUnit.run
挂起的情况下运行它最终超时:
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
代码已正确加载,我可以直接使用 TheTest调用它。测试将两个数字相加({{})
。但是我希望这样做能启动整个套件。
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.
推荐答案
我假设您没有使用混合
。在运行测试用例之前,需要将它们加载到 ExUnit
服务器上。
I am assuming you are not using mix
. You need to load the test cases to the ExUnit
server before running them.
在Elixir v1.6之前,您需要像这样加载测试:
Before Elixir v1.6 you would load the tests like this:
ExUnit.Server.cases_loaded()
在Elixir v1.6之后,您将像这样加载它们(感谢@jeffreymatthias):
And after Elixir v1.6 you would load them like this (thanks to @jeffreymatthias):
ExUnit.Server.modules_loaded()
因此您应该编写代码用 iex
写应该是:
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()
我希望这会有所帮助。
这篇关于如何在IEx中运行ExUnit测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!