erlang - 如何在 EUnit 测试用例中禁用错误记录器?
问题描述
在运行测试应用程序或启动和停止(或测试杀死)gen_server
或进程的 EUnit 测试时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
在测试期间如何避免这些预期的消息?
解决方案
这些可以通过暂时禁用错误记录器中的 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)
推荐阅读
- ajax - 在 ASP.Net Core 中将不记名令牌添加到 ajax POST 请求
- php - php命令行中的js
- php - Angularjs 表单操作并返回错误
- symfony4 - 无效的 YAML Symfony4
- java - 使用 Java (netbeans) 连接到远程 SQL 服务器时出现 SSL 错误
- javascript - 带有 html 代码和打印方法的对象
- c# - 不能简单地遍历列表以匹配密码(c#winform)
- ios - 使用 dataTask 的 NSURLCaching 无法在离线时使用 NSURLRequest 读取缓存
- python - 熊猫数据框子集
- python - 如何确定递归中的最后一个堆栈空间