我们说我有一个名为的模块 example.erl
在本单元中,我使用以下构造进行调试:
%%% Switch debugging output on/off:
%-define(DBG(Str, Args), ok).
-define(DBG(Str, Args), io:format(Str, Args)).
Run Code Online (Sandbox Code Playgroud)
它帮助我将各种调试信息输出到Erlang shell:
?DBG("Function fun1 starting... ~n", [])
Run Code Online (Sandbox Code Playgroud)
但是,如果我打电话example.erl从example_tests使用example:test(),这个输出信息不会出现.
如何在EUnit测试期间将其显示出来?
UPD:我找到了一些相关信息,但我仍然不知道如何解决这个问题.
我用EUnit写了一个测试,但在控制台中没有任何异常细节输出.
exp_test() ->
?assertEqual(0, 1/0).
Run Code Online (Sandbox Code Playgroud)
运行此模块:下面的Erlang Shell输出中的exp_test()
** exception error: bad argument in an arithmetic expression
in function exp_test:'-exp_test/0-fun-0-'/1 (src/test/eunit/xxx_test.erl, line 8)
Run Code Online (Sandbox Code Playgroud)
但在EUnit输出之后
> eunit:test(xxx).
> xxx_test: exp_test...*failed*
::badarith
Run Code Online (Sandbox Code Playgroud)
EUnit不输出任何异常跟踪信息
我在eunit中尝试了详细的配置,但没有效果.
我想在eunit测试结果中输出一些异常细节.
谢谢〜
在一个简单的基于Erlang YAWS的RESTful应用程序中,我希望有一组测试将HTTP请求发送到RESTful API,从服务器获取响应,然后测试这些响应.
如果每个"send-request-get-request-test"测试都可以在EUnit中运行(可以使用测试生成器),那就太好了.
我也希望能够用rebar(make test)运行这组测试.
最近,我用 ibrowse在另一个应用程序(受Mochiweb),但我发现它安静的使用麻烦.
是否还有其他选项可以编写可以将HTTP请求发送到YAWS RESTful应用程序的Erlang/OTP测试?最常见的方法是什么?
我正在为Erlang代码编写EUnit测试.
我有一个源模块:
-module(prob_list).
-export([intersection/2,union/2]).
probability([], _Item) -> false;
probability([{First,Probability}|Rest], Item) ->
if
First == Item -> Probability;
true -> probability(Rest, Item)
end.
...
...
...
Run Code Online (Sandbox Code Playgroud)
和单元测试模块:
-module(prob_list_tests).
-include_lib("eunit/include/eunit.hrl").
-define(TEST_LIST,[{3,0.2},{4,0.6},{5,1.0},{6,0.5}]).
-define(TEST_LIST1,[{2,0.9},{3,0.6},{6,0.1},{8,0.5}]).
-define(TEST_UNO_LIST,[{2,0.5}]).
probability_test() -> ?assertNot(prob_list:probability([],3)),
?assertEqual(0.5,prob_list:probability(?TEST_UNO_LIST,2)),
?assertNot(prob_list:probability(?TEST_UNO_LIST,3)),
?assertEqual(0.2,prob_list:probability(?TEST_LIST,3)),
?assertEqual(1.0,prob_list:probability(?TEST_LIST,5)),
?assertNot(prob_list:probability(?TEST_LIST,7)).
...
...
...
Run Code Online (Sandbox Code Playgroud)
我跑的 eunit:test(prob_list,[verbose])时候说:
prob_list_tests: probability_test...*failed*
::undef
Run Code Online (Sandbox Code Playgroud)
但是当我probability/2在我的prob_list模块中导出时,一切都很好.
有没有办法测试私人功能?
我有一个中等版本的应用程序.我最近在发布版本的库应用程序中重构了一些常用功能.这使得我的EUnit测试undef在测试需要库应用程序的任何内容时都会失败.
设置是这样的:
% In apps/utils/src/utils.erl
-module(utils).
-export([foo/0]).
foo() -> "OH HAI".
Run Code Online (Sandbox Code Playgroud)
然后
% In apps/some_app/src/some_app.erl
-module(some_app).
-export([bar/0]).
bar() -> io:format("foo: ~s~n", [utils:foo()]).
% unit tests for bar()
Run Code Online (Sandbox Code Playgroud)
然后单元测试some_app:bar()失败.我正在运行它们rebar eunit skip_deps=true.我正在使用,skip_deps=true因为我的版本使用了一些第三方应用程序(SQL等).
我假设测试开始失败,因为EUnit正在调用没有依赖项的测试中的应用程序?有没有什么办法解决这一问题?我已将.app文件配置为显式声明依赖项.它在发布时运行良好,现在已经部署了大约一天没有问题,但如果我能让测试再次通过,我会感觉好多了:)
(我可以使用一个模拟应用程序来存根utils:foo/0,我可以看到那个特定的理想位置,但在这种情况下看起来有点过分,因为utils:foo/0(读:它是现实世界的对应物)做了一些非常简单的事情.)
我在gen_server上写了一些eunit测试:
-module(st_db_tests).
-include_lib("eunit/include/eunit.hrl").
main_test_() ->
{foreach,
fun setup/0,
fun cleanup/1,
[
fun db_server_up/1
]}.
setup() ->
{ok,Pid} = st_db:start_link(), Pid.
cleanup(Pid) ->
gen_server:call(Pid, stop).
db_server_up(Pid) ->
?_assertMatch({[{<<"couchdb">>,<<"Welcome">>},{<<"version">>, _}]},
gen_server:call(Pid, test)).
Run Code Online (Sandbox Code Playgroud)
当我进行测试时,我有这个:
./rebar eunit suite=st_db_tests skip_deps=true
==> site_stater (eunit)
Compiled test/st_db_tests.erl
... loading stuff ...
=PROGRESS REPORT==== 27-Jun-2011::12:33:21 ===
supervisor: {local,kernel_safe_sup}
started: [{pid,<0.127.0>},
{name,inet_gethost_native_sup},
{mfargs,{inet_gethost_native,start_link,[]}},
{restart_type,temporary},
{shutdown,1000},
{child_type,worker}]
module 'st_db_tests'
*** context cleanup failed ***
::exit:{normal,{gen_server,call,[<0.99.0>,stop]}}
in function gen_server:call/2
=======================================================
Failed: 0. Skipped: 0. Passed: 1.
Run Code Online (Sandbox Code Playgroud)
好像测试已经过去了,但在上下文清理中有错误,这是不对的,对吧?)
我怎样才能解决这个问题?
PS:我的gen_server
-module(st_db).
-behaviour(gen_server). …Run Code Online (Sandbox Code Playgroud) 我想知道如何在eunit中标记一个特定的测试,以便在下一次测试运行时强制它被忽略(即编译但不执行).我在TDD环境中问这个问题,即我想在绿色中进行重构,但仍然有一些测试用例,我稍后会介绍.
我宁愿不评论测试,这是一个忘记它的好方法.eunit的测试摘要行确实有一个跳过的行,但我找不到任何关于该功能的文档.
我正在编写一个单元测试,其中生成一个(相当冗长的)二进制文件,我想断言生成的二进制文件等于我期望生成的二进制文件.我通过" rebar eunit " 运行eunit .
事情是,当这个断言失败时,输出被" ...... "消除,我想看到完整的输出,所以我可以发现差异在哪里.
我现在使用" ?debugFmt() "作为临时解决方案,但我想知道是否有替代它(某个地方的配置选项或参数可以应用于" ?_assertEqual() "所以输出仅在断言失败时显示).
提前致谢!
编辑:由于legoscia的答案,我使用测试生成器包含一个测试样本,有多个断言:
can_do_something(SetupData) ->
% ... some code ...
[?_assertEqual(Expected1, Actual1), ?_assertEqual(Expected2, Actual2)].
Run Code Online (Sandbox Code Playgroud) 我正在编写一个模块的测试,该模块在一个简单的过程中运行spawn_link(?MODULE, init, [self()]).
在我的eunit测试中,我定义了一个设置和拆卸功能以及一组测试生成器.
all_tests_test_() ->
{inorder, {
foreach,
fun setup/0,
fun teardown/1,
[
fun my_test/1
]}
}.
Run Code Online (Sandbox Code Playgroud)
设置乐趣创建了测试过程:
setup() ->
{ok, Pid} = protocol:start_link(),
process_flag(trap_exit,true),
error_logger:info_msg("[~p] Setting up process ~p~n", [self(), Pid]),
Pid.
Run Code Online (Sandbox Code Playgroud)
测试看起来像这样:
my_test(Pid) ->
[ fun() ->
error_logger:info_msg("[~p] Sending to ~p~n", [self(), Pid]),
Pid ! something,
receive
Msg -> ?assertMatch(expected_result, Msg)
after
500 -> ?assert(false)
end
end ].
Run Code Online (Sandbox Code Playgroud)
我的大多数模块都是gen_server但是为此我认为没有所有gen_server样板代码会更容易...
测试的输出如下所示:
=INFO REPORT==== 31-Mar-2014::21:20:12 ===
[<0.117.0>] Setting up process <0.122.0>
=INFO REPORT==== 31-Mar-2014::21:20:12 ===
[<0.124.0>] Sending …Run Code Online (Sandbox Code Playgroud) erlang ×10
eunit ×10
testing ×2
unit-testing ×2
dbg ×1
erlang-otp ×1
process ×1
rebar ×1
rest ×1
yaws ×1