Как отключить регистратор ошибок в тестовых примерах EUnit?

При запуске теста EUnit, который тестирует приложение или запускает и останавливает (или тестирует уничтожение) gen_server или supervisor процессов, регистратор ошибок выводит отчеты о сбоях и другие сообщения по умолчанию:

$ 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

Как я могу избежать этих ожидаемых сообщений во время тестирования?


person Adam Lindberg    schedule 05.09.2019    source источник


Ответы (1)


Этого можно избежать, временно отключив отчеты TTY в журнале ошибок. Окружите код, который создает отчеты, следующим образом:

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

Если вы используете это много раз в тестах, эта оболочка может быть полезной:

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

Который используется так:

without_error_logger(fun() ->
    % code that produces error logger reports
end)
person Adam Lindberg    schedule 05.09.2019