How do I disable the error logger in EUnit test cases?

293 Views Asked by At

When running an EUnit test that tests an application or starts and stops (or tests killing of) gen_server or supervisor processs, error logger outputs crash reports and other messages by default:

$ rebar3 eunit                                                               1s
===> Verifying dependencies...
===> Compiling my_app
===> Performing EUnit tests...
...........=INFO REPORT==== 5-Sep-2019::16:32:18.760457 ===
    application: ranch
    exited: stopped
    type: temporary
=INFO REPORT==== 5-Sep-2019::16:32:18.760545 ===
    application: xmerl
    exited: stopped
    type: temporary
=INFO REPORT==== 5-Sep-2019::16:32:18.763882 ===
    application: my_app
    exited: stopped
    type: temporary
......=ERROR REPORT==== 5-Sep-2019::16:32:18.814431 ===
** Generic server my_app_sup terminating
** Last message in was {'EXIT',<0.279.0>,test_kill}
** When Server state == {state,
                            {local,my_app_sup},
                            simple_one_for_one,
                            {[undefined],
                             #{undefined =>
                                   {child,undefined,undefined,
                                       {my_app_server,start_link,[]},
                                       transient,5000,worker,
                                       [my_app_server]}}},
                            {maps,#{<0.355.0> => [my_app_test]}},
                            1,5,[],0,my_app_sup,[]}
** Reason for termination ==
** test_kill

=CRASH REPORT==== 5-Sep-2019::16:32:18.814598 ===
  crasher:
    initial call: supervisor:my_app_sup/1
    pid: <0.354.0>
    registered_name: my_app_sup
    exception exit: test_kill
      in function  gen_server:decode_msg/9 (gen_server.erl, line 432)
    ancestors: [<0.279.0>]
    message_queue_len: 0
    messages: []
    links: []
    dictionary: []
    trap_exit: true
    status: running
    heap_size: 1598
    stack_size: 27
    reductions: 6463
  neighbours:

...........
Finished in 0.457 seconds
28 tests, 0 failures

How can I avoid these expected messages during testing?

1

There are 1 best solutions below

0
Adam Lindberg On

These can be avoided by temporarily disabling TTY reports in the error logger. Surround the code which produces the reports with this:

my_test() ->
    error_logger:tty(false),
    try
        % code that produces error logger reports
    after
        error_logger:tty(true)
    end.

If you use this many times in the tests, this wrapper can be useful:

without_error_logger(Fun) ->
    error_logger:tty(false),
    try
        Fun()
    after
        error_logger:tty(true)
    end.

Which is used like so:

without_error_logger(fun() ->
    % code that produces error logger reports
end)