gpt4 book ai didi

testing - 测试时抑制记录器

转载 作者:行者123 更新时间:2023-11-28 19:43:22 25 4
gpt4 key购买 nike

我想知道如何在测试时禁用 Elixir 中的日志记录。在我当前的代码中,我测试记录器消息,所以我不想完全禁用它,而是隐藏消息直到任何测试停止通过。

我正在使用 mix 和 ExUnit 来管理和测试我的项目。

mix test
Compiling 2 files (.ex)
.........
17:59:18.446 [warn] Code seems to be empty
.
17:59:18.447 [warn] Code doesn't contain HLT opcode
.
17:59:18.448 [warn] Code doesn't contain HLT opcode

17:59:18.448 [error] Label error doesn't exist
.....

Finished in 0.07 seconds
16 tests, 0 failures

最佳答案

要么使用ExUnit.CaptureLog:

import ExUnit.CaptureLog

test "example" do
assert capture_log(fn ->
Logger.error "log msg"
end) =~ "log msg"
end

或者,如果您只想忽略任何日志,那么:

@tag capture_log: true
test "example" do
Logger.error "log msg"
end

关于testing - 测试时抑制记录器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47996005/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com