【问题标题】:How to debug my Eunit test suite run when using rebar3?使用 rebar3 时如何调试我的 Eunit 测试套件运行?
【发布时间】:2016-04-12 01:17:29
【问题描述】:

我使用 rebar3 (beta-4) 创建了一个 release 应用程序。 添加了一些 eunit 测试并编写了一些代码。

现在我必须调试一个测试用例,看看我必须添加什么才能使实现正常工作。

我从 Erlang 控制台中找到了一些关于使用 dbg 的文章,并找到了如何从 Eunit 编写调试信息。但我需要从必须测试的代码中获取信息(实际实现(逻辑))。

rebar3eunit 参数一起使用时,有没有办法调试Erlang 代码(实际源代码,而不是测试代码)?

我在终端中使用跟踪:https://aloiroberto.wordpress.com/2009/02/23/tracing-erlang-functions/

【问题讨论】:

  • Rebar 在运行 eunit 测试时使用 -noshell 标志,因此当 rebar 运行时,您无法附加到 Erlang 控制台来调试代码。调用eunit 测试就像调用函数一样简单,因此当您的应用程序通过./your-app-release console./your-app-release remote_shell 命令启动时,您可以从Erlang 控制台调用它们。

标签: debugging erlang rebar eunit


【解决方案1】:

一种方法是使用rebar3 在测试配置文件下运行shell,然后启动调试器并设置断点等:

$ rebar3 as test shell
...
1> debugger:start().
{ok, <0.67.0>}

这将弹出debugger GUI。调试器设置好并准备就绪后,在 eunit 下运行测试:

2> eunit:test(your_test_module,[verbose]).
======================== EUnit ========================
your_test_module: xyz_test_ (module 'your_test_module')...

假设您在调试器中设置了一个合适的断点,这会命中它,但您可能会遇到这种方法的问题:默认情况下,eunit 测试在 5 秒后超时,这不会给出你有很多时间进行调试。您需要为您的测试指定一个longer timeout,这就是为什么上面的示例显示正在运行的是一个名为xyz_test_test fixture,它用一个长时间的超时来包装实际测试。这样的夹具非常简单:

-include_lib("eunit/include/eunit.hrl").

xyz_test_() ->
    {timeout,3600,
     [fun() -> ?assertMatch(expected_value, my_module:my_fun()), ok end]}.

这里,实际测试的是匿名函数,它匹配来自my_module:my_fun/0的返回值,在这个例子中代表被测业务逻辑。这个示例夹具将测试超时设置为一小时;您当然可以根据应用程序的需要进行设置。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-20
    • 2012-04-06
    • 2016-07-25
    • 1970-01-01
    • 2015-06-13
    • 2012-12-28
    • 2011-10-15
    • 2011-07-02
    相关资源
    最近更新 更多