Information on debugging EUnit from tested modules

Let's say I have a module called example.erl

In this module, I use the following construction 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 information to the Erlang shell:

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

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

How can I make this visible during the EUnit test?

UPD: I found some related information , but I still don't know how to solve the problem.

+4
source share
2 answers

As described on the mentioned page, you can use debugMsg or debugFmt.

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

or

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

Make sure that the eunit header file is included in the module file.

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

If you want to disable debug macros, for example, at the configuration stage, define NODEBUG when compiling the modules.

+15
source

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

This is described in the EUunit captures standard output section of 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. 

An example of the source file:

 -module(foo). -export([ api/1 ]). -include("trace.hrl"). api(Arg) -> ?TRACE("Called with Arg ~p~n", [Arg]), ok. 

I prefer this method ?debugFmt , since the latter introduces special EUnit code in your source code (and not a test).

0
source

Source: https://habr.com/ru/post/1395827/


All Articles