使用eunit启动相关应用程序

时间:2013-10-31 21:30:59

标签: unit-testing erlang eunit

当我运行单元测试时,我开始作为依赖应用程序启动,但由于某种原因,测试中的代码看不到它。

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

main_test_() ->
{foreach,
 fun distr_setup/0,
 fun distr_cleanup/1,
 [
  fun must_retain/1
  ]}.

must_retain(_) ->
{"Should do ping pong when is fully initialized",
 fun() ->
     ?assertEqual(pong, abuse_counter:ping())
 end}.



%%------------------------------------------------------------------
distr_setup() ->
abuse_counter:start_link(),
ok.

distr_cleanup(_) ->
abuse_counter:stop(),
ok.

这是日志的输出,抱怨没有定义lager {undef,[{lager,info,[“up and running”],[]} 虽然在运行输出中肯定存在。

以下是我如何运行它:

erl -pa ebin/ ../../deps/*/ebin -s lager -eval 'eunit:test(main_tests,[verbose]),    init:stop().'

输出失败

Eshell V5.10.2  (abort with ^G)
1> 17:13:31.506 [info] Application lager started on node nonode@nohost
======================== EUnit ========================
module 'main_tests'
undefined
17:13:31.528 [error] CRASH REPORT Process <0.57.0> with 1 neighbours exited with reason: call to undefined function lager:info("up and running") in gen_server:init_it/6 line 328
*unexpected termination of test process*
::**{undef,[{lager,info,["up and running"],[]}**,
      {abuse_counter,init,1,[{file,"src/abuse_counter.erl"},{line,37}]},
      {gen_server,init_it,6,[{file,"gen_server.erl"},{line,304}]},
      {proc_lib,init_p_do_apply,3,[{file,"proc_lib.erl"},{line,239}]}]}

=======================================================
 Failed: 0.  Skipped: 0.  Passed: 0.
 One or more tests were cancelled.

已经花了3-4个小时在谷歌和堆栈溢出但似乎没有任何工作。

一个选项是将此调用隐藏在?INFO(Mgs)宏后面,但不喜欢这个想法。

任何帮助都将受到高度赞赏。

1 个答案:

答案 0 :(得分:4)

所以看起来src/abuse_counter.erl文件没有使用{parse_transform, lager_transform}选项进行编译。

函数lager:info("message!")确实不存在,parse_transform将lager:info("message!")转换为此函数:lager:dispatch_log(info, Metadata, "message!", [], Size)

尝试使用{parse_transform, lager_transform}选项重新编译模块。

Lager展示了如何执行此操作:link to the Readme