Showing full expected and value information when ?_assertEqual fails

╄→尐↘猪︶ㄣ 提交于 2019-12-22 06:45:06

问题


I'm coding a unit test where a (rather lengthy) binary is generated, and I want to assert that the generated binary equals the one I expect to be generated. I'm running eunit through "rebar eunit".

Thing is, when this assertion fails, the output is abreviated with "...", and I want to see the complete output so I can spot where the difference is.

I'm now using "?debugFmt()" as a temporary solution, but I'd like to know if there's an alternative to it (a config option or argument somewhere that can be applied to "?_assertEqual()" so the output is only shown when the assertion fails).

Thanks in advance!

EDIT: Due to legoscia's answer, I'm including a test sample using a test generator, with multiple asserts:

can_do_something(SetupData) ->
    % ... some code ... 
    [?_assertEqual(Expected1, Actual1), ?_assertEqual(Expected2, Actual2)].

回答1:


The best I can think of for actually showing the value in the console is something like this:

Actual =:= Expected orelse ?assert(?debugFmt("~p is not ~p", [Actual, Expected]))

?debugFmt returns ok, which is not true, so the assertion will always fail.

Alternatively, to use it as a test generator, the entire thing can be put inside ?_assert:

?_assert(Actual =:= Expected orelse ?debugFmt("~p is not ~p", [Actual, Expected]))



回答2:


1). Open your eunit sources. In my system:

cd /usr/lib/erlang/lib/eunit-2.3.2/src

2). Edit eunit_lib.erl in such way:

diff

54c54
<     format_exception(Exception, 20).
---
>     format_exception(Exception, 99999).

3). sudo erlc -I ../include eunit_lib.erl

4). mv eunit_lib.beam ../ebin

5). Have a good day))




回答3:


The way I usually achieve this is by having Eunit output XML files (in "Surefire" format, AKA "Junit" format). The XML files have much higher limits for term print depth, and thus probably contain the information you need.

Add this to your rebar.config:

{eunit_opts, 
 [verbose,
  %% eunit truncates output from tests - capture full output in
  %% XML files in .eunit
  {report,{eunit_surefire,[{dir,"."}]}}]}.

Then you can find the results for module foo in .eunit/TEST-foo.xml. I find the files quite readable in a text editor.



来源:https://stackoverflow.com/questions/10952767/showing-full-expected-and-value-information-when-assertequal-fails

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!