EUnit 从被测模块输出调试信息

发布于 2025-01-04 15:30:36 字数 598 浏览 1 评论 0原文

假设我有一个名为 example.erl 的模块

在这个模块中,我使用以下结构进行调试:

%%% Switch debugging output on/off:
%-define(DBG(Str, Args), ok).
-define(DBG(Str, Args), io:format(Str, Args)).

它帮助我将各种调试信息输出到 Erlang shell 中:

?DBG("Function fun1 starting... ~n", [])

但是如果我调用 example .erl 来自 example_testsexample:test(),此输出信息不会出现。

如何使其在 EUnit 测试期间可见?

UPD:我找到了一些相关信息,但我仍然这样做不知道如何解决问题。

Let us say I have a module called example.erl

In this module I use the following construct for debugging:

%%% Switch debugging output on/off:
%-define(DBG(Str, Args), ok).
-define(DBG(Str, Args), io:format(Str, Args)).

It helps me to output various debugging info into the Erlang shell:

?DBG("Function fun1 starting... ~n", [])

But if I call example.erl from example_tests with example:test(), this output info does not appear.

How can I make it visible during a EUnit test?

UPD: I have found some related info, but I still do not know how to solve the issue.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

末が日狂欢 2025-01-11 15:30:36

正如您提到的页面中所述,您可以使用 debugMsg 或 debugFmt。

?debugMsg("Function fun1 starting...")

?debugFmt("Function fun1 starting...", [])

确保您的模块文件中包含 eunit 的头文件。

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

如果要禁用调试宏(例如在暂存中),请在编译模块中定义 NODEBUG。

As describe in the page you mention, you can use debugMsg or debugFmt.

?debugMsg("Function fun1 starting...")

or

?debugFmt("Function fun1 starting...", [])

Make sure you have include eunit's header file in your module file.

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

If you want to disable debug macros for example in staging, define NODEBUG in compiling modules.

怀里藏娇 2025-01-11 15:30:36

遇到了类似的问题。感谢@rvirding,找到了另一种方法。解决方案是写入user输出流(如io:format(user, "~w", [Term]))。

这在 eunit 文档

我的“trace.hrl”文件如下所示:

%% If compiled with {d, TEST, true}
%% This happens automatically during `rebar3 eunit`
-ifdef(TEST).
-define(TRACE(Template, Args), io:format(user, "TRACE ~p:~p ~p~n", [?MODULE, ?LINE, lists:flatten(Args)])).
-else.
-define(TRACE(_T, _A), void).
-endif.

示例源文件:

-module(foo).
-export([ api/1 ]).

-include("trace.hrl").

api(Arg) ->
    ?TRACE("Called with Arg ~p~n", [Arg]),
    ok.

?debugFmt 相比,我更喜欢此方法,因为后者将 EUnit 特定代码注入到您的中(而不是比测试)代码。

Ran into a similar problem. Found another way thanks to @rvirding. The solution is to write to the user output stream (as in io:format(user, "~w", [Term])).

This is documented in the section titled "EUnit captures standard output" of the eunit docs.

My "trace.hrl" file is shown below:

%% If compiled with {d, TEST, true}
%% This happens automatically during `rebar3 eunit`
-ifdef(TEST).
-define(TRACE(Template, Args), io:format(user, "TRACE ~p:~p ~p~n", [?MODULE, ?LINE, lists:flatten(Args)])).
-else.
-define(TRACE(_T, _A), void).
-endif.

A sample source file:

-module(foo).
-export([ api/1 ]).

-include("trace.hrl").

api(Arg) ->
    ?TRACE("Called with Arg ~p~n", [Arg]),
    ok.

I prefer this method to ?debugFmt, as the latter injects EUnit specific code into your source (rather than test) code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文