ruby - 如何将 Ruby 测试/单元日志记录设置为 quiet_mode?
问题描述
我正在尝试为我在 test/unit 中运行的测试设置日志记录级别
require 'test/unit'
require 'logger'
class MyMath
def self.adder(a,b)
a+b
end
end
class Tester < Test::Unit::TestCase
$logger = Logger.new($stdout)
def test_adder()
$logger.info "Starting Test"
assert_equal(4, MyMath.adder(2,2) )
$logger.info "Ending Test"
end
end
输出是
Loaded suite ruby.test
Started
I, [2021-07-23T13:12:26.497311 #49542] INFO -- : Starting Test
I, [2021-07-23T13:12:26.497375 #49542] INFO -- : Ending Test
.
Finished in 0.000358 seconds.
---------------------------------------------------------------------------------------------------------------------------------------------------------------------
1 tests, 1 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
100% passed
---------------------------------------------------------------------------------------------------------------------------------------------------------------------
2793.30 tests/s, 2793.30 assertions/s
如何让它不打印日志消息?在它谈到的 Test::Unit::UI::Console::TestRunner 代码中
# Creates a new TestRunner for running the passed
# suite. If quiet_mode is true, the output while
# running is limited to progress dots, errors and
# failures, and the final result. io specifies
# where runner output should go to; defaults to
# STDOUT.
什么是 quiet_mode,我该如何设置?
解决方案
我认为您的问题是您需要相应地设置记录器的日志级别。
$logger = Logger.new($stdout)
$logger.level = Logger::WARN
https://ruby-doc.org/stdlib-2.4.0/libdoc/logger/rdoc/Logger.html
不确定您是否使用任何框架,因此您可以在测试中设置日志级别,例如
if ENV["test"]
$logger.level = Logger::WARN
end
推荐阅读
- android - 是否可以使用 Google Play Vision 从一个 CameraSource 运行两个检测器
- go - Golang 模板不会加载
- javascript - 触摸屏的 HTML Canvas 帮助
- java - Servlet 过滤器模式来过滤一些路径外的 URL
- postgresql - 如何使用 Golang lib/pq API 将二进制数据插入 PostgreSQL BYTEA 列?
- android - 用高程完成两个面板布局的最有效方法是什么?
- windows - 从“Matlab”调用时,如何在成功分析提交的作业后退出“abaqus”命令窗口到“Matlab”脚本
- tensorflow - tf.keras.layers 中的转置卷积
- matlab - Matlab:ode15i 求解器引发尺寸错误,我看不到在哪里
- c - 无法在代码中找到错误的原因