erlang - EUnit and io:format -


i want test third-party erlang code using eunit.

the output code's functions displayed standard output using io:format/2. capture output , perform ?assert test on string printed out. cannot modify third-party code.

is way erlang? (for instance, in java can use system.setout() output stream).

update:

the group_leader/2 seems on right track.

but, still don't see how allows me capture string printed io:format can test assertion. simplified example of code is:

result(value) ->     io:format("result: ~w~n", [value]).  test_result() ->     ?assertmatch("result: 5~n", result(5)). 

clearly, return function result/1 atom ok, want test string output console (i.e. "result: 5~n").

am wrong approach, because seems nobody else (judging lack of search results)?

background: third-party code interactive console application, of functions use io:format show results.

you could use dbg (the erlang tracer) this. can trace calls made io:format/2 process , receive trace message it. can use trace message assert being used call io:format/2,3 correct. benefit don't have interfere eunit since capturing actual io messages.

a small example (adjust unit test[s]):

1> handlefun = fun(trace, parent) -> parent ! trace, parent end. #fun<erl_eval.12.113037538> 2> dbg:tracer(process, {handlefun, self()}). {ok,<0.119.0>} 3> iocallingfun = fun(f) ->  3>   timer:sleep(5000), 3>   io:format("random: ~p~n",[random:uniform(1000)]),  3>   f(f)  3> end. #fun<erl_eval.6.13229925> 4> pidtotrace = erlang:spawn_link(fun() -> iocallingfun(iocallingfun) end). <0.123.0> random: 93 random: 444 5> dbg:p(pidtotrace, [c]). {ok,[{matched,nonode@nohost,1}]} 6> dbg:tp(io, format, []). {ok,[{matched,nonode@nohost,3}]} random: 724 random: 946  random: 502  7> flush(). shell got {trace,<0.123.0>,call,{io,format,["random: ~p~n",[724]]}} shell got {trace,<0.123.0>,call,{io,format,["random: ~p~n",[946]]}} shell got {trace,<0.123.0>,call,{io,format,["random: ~p~n",[502]]}} ok 8> exit(pidtotrace). ** exception exit: <0.123.0> 9> dbg:stop_clear(). ok 10>  

so in other words start trace before start unit test, test trace messages , kill trace. make sure trace on process making calls! otherwise you'll messages on place. can see how trace messages here: http://www.erlang.org/doc/man/erlang.html#trace-3

using can test things process takes correct path (e.g. calls right functions expect) or sending correct messages other processes etc. overlooked in unit tests can quite powerful. 1 point though can quickly become on engineering, careful.

this might not accepted answer tool use testing :)

good luck.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -