EUnit 从被测模块输出调试信息
假设我有一个名为 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_tests
和 example: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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如您提到的页面中所述,您可以使用 debugMsg 或 debugFmt。
或
确保您的模块文件中包含 eunit 的头文件。
如果要禁用调试宏(例如在暂存中),请在编译模块中定义 NODEBUG。
As describe in the page you mention, you can use debugMsg or debugFmt.
or
Make sure you have include eunit's header file in your module file.
If you want to disable debug macros for example in staging, define NODEBUG in compiling modules.
遇到了类似的问题。感谢@rvirding,找到了另一种方法。解决方案是写入user输出流(如
io:format(user, "~w", [Term])
)。这在 eunit 文档。
我的“trace.hrl”文件如下所示:
示例源文件:
与
?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:
A sample source file:
I prefer this method to
?debugFmt
, as the latter injects EUnit specific code into your source (rather than test) code.