繁体   English   中英

如何在 IEx 中运行 ExUnit 测试

[英]How to run ExUnit tests within IEx

我正在尝试在测试中启动IEx.pry 但是我无法在 iex 会话中运行测试。 请注意,我没有使用混合。

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挂起的情况下运行它并最终超时:

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."test adds two numbers"({})调用它TheTest."test adds two numbers"({}) 但我希望这样做可以启动整个套件。

我假设您没有使用mix 在运行之前,您需要将测试用例加载到ExUnit服务器。

在 Elixir v1.6 之前,您将像这样加载测试:

ExUnit.Server.cases_loaded()

在 Elixir v1.6 之后,您将像这样加载它们(感谢 @jeffreymatthias):

ExUnit.Server.modules_loaded()

所以你应该在iex编写的代码应该是:

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()

我希望这会有所帮助。

根据ExUnit 文档ExUnit.run/0只应在您不想在调用ExUnit.start/1时自动启动测试时使用。

您总是必须调用ExUnit.start() ,它会自动运行所有测试,除非您通过autorun: false

alias ExUnit.Assertions
require Assertions
Assertions.assert 1==1
true

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM