【问题标题】:How to run ExUnit tests with erlang debugger?如何使用 erlang 调试器运行 ExUnit 测试?
【发布时间】:2021-09-28 16:13:33
【问题描述】:

​​背景

我有一个带有一些分支逻辑的小函数,我对它进行了一些测试。我想在运行给定测试时使用 erlang 调试器,以确保我正在执行正确的代码。

代码

假设我们正在测试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

现在,这个函数有一些分支逻辑,代码整体很糟糕,但它确实给了我们很多有趣的可执行行,我们将断点附加到。

这些是我正在运行的测试:

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

如您所见,我的西班牙语问候测试将失败。但是,我想使用 erlang 调试器找出答案: http://blog.plataformatec.com.br/2016/04/debugging-techniques-in-elixir-lang/

运行测试

鉴于我只对运行失败的测试感兴趣,我将代码更改为:

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

然后我运行mix test --only wip

在那里,现在我只运行失败的测试。但我仍然没有使用调试器。 为了解决这个问题,我尝试通过 iex shell iex -S mix test --only wip 运行测试,但运行是自动的,并且在我设置调试器之前就结束了:

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

问题

使用mix test时如何运行erlang调试器?

【问题讨论】:

    标签: debugging testing erlang elixir ex-unit


    【解决方案1】:

    回答

    事实证明,如果我以以下方式修改我的测试:

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

    然后使用mix test --trace --only wip 运行它,执行将启动调试器并等待它完成。

    原始答案可以在这里找到:

    https://elixirforum.com/t/how-to-run-exunit-tests-with-erlang-debugger/41162/4?u=fl4m3ph03n1x

    【讨论】:

      猜你喜欢
      • 2017-04-09
      • 1970-01-01
      • 1970-01-01
      • 2016-10-09
      • 2018-04-09
      • 1970-01-01
      • 2019-11-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多