Showing full expected and value information when ?

2019-04-29 08:27发布

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)].

标签: erlang eunit
3条回答
做自己的国王
2楼-- · 2019-04-29 09:29

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]))
查看更多
爷的心禁止访问
3楼-- · 2019-04-29 09:30

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.

查看更多
来,给爷笑一个
4楼-- · 2019-04-29 09:32

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))

查看更多
登录 后发表回答