Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EUnit output debug info from tested modules

Tags:

erlang

dbg

eunit

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.

like image 614
skanatek Avatar asked Feb 10 '12 19:02

skanatek


2 Answers

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.

like image 53
Manoj Govindan Avatar answered Sep 27 '22 17:09

Manoj Govindan


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.

like image 45
shino Avatar answered Sep 27 '22 15:09

shino