如何使用MiniTest测试记录器消息? [英] How can I test logger-messages with MiniTest?

查看:71
本文介绍了如何使用MiniTest测试记录器消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,我想测试是否正确 来自记录器的消息.

I have an application and I want to test if I get correct messages from my logger.

一个简短的示例(您可以在log4r和logger之间切换):

A short example (you may switch between log4r and logger):

gem 'minitest'
require 'minitest/autorun'
require 'log4r'
#~ require 'logger'
class Testlog < MiniTest::Test
  def setup
    if defined? Log4r
      @log = Log4r::Logger.new('log')
      @log.outputters << Log4r::StdoutOutputter.new('stdout', :level => Log4r::INFO)
    else
      @log = Logger.new(STDOUT)
      @log.level = Logger::INFO
    end
  end

  def test_silent
    assert_silent{ @log.debug("hello world") }
    assert_output(nil,nil){ @log.debug("Hello World") }
  end
  def test_output
    #~ refute_silent{ @log.INFO("Hello") }#-> NoMethodError: undefined method `refute_silent'        
    assert_output("INFO log: Hello World\n",''){ @log.info("Hello World") }
  end

end

但是我得到了

  1) Failure:
Testlog#test_output [minitest_log4r.rb:27]:
In stdout.
Expected: "INFO log: Hello World\n"
  Actual: ""

在输出屏幕上,我看到消息. 我对Log4r::StderrOutputterLog4r::Outputter.stdout有类似的结果.

On my output screen I see the message. I have similar results with Log4r::StderrOutputter and Log4r::Outputter.stdout.

因此,似乎它已发送到输出屏幕,但在STDOUT或STDERR中却没有被minitest捕获.

So it seems it is send to the output screen, but it is not catched by minitest in STDOUT or STDERR.

在我开始编写minitest-log4r-Gem之前:

Before I start to write a minitest-log4r-Gem:

是否可以在minitest中测试记录器的输出?

如果不是: 关于如何实施minitest-log4r-Gem的任何建议?

If not: Any recommendations how to implement a minitest-log4r-Gem?

示例我可以想象的:

  • 为minitest(Log4r :: MinitestOutputter)定义新的输出器
  • 模拟记录器.
  • 新的断言(将新的输出程序添加为参数?):
    • assert_message('INFO log: Hello World'){ @log.info("Hello World") }
    • assert_messages(:info => 1, :debug => 2){ @log.info("Hello World") }对邮件进行计数.
    • assert_info_messages('Hello World'){ @log.info("Hello World") }
    • assert_debug_messages('Hello World'){ @log.info("Hello World") }
    • define new outputter for minitest (Log4r::MinitestOutputter)
    • Mock the logger.
    • new assertions (add the new outputter as parameter?):
      • assert_message('INFO log: Hello World'){ @log.info("Hello World") }
      • assert_messages(:info => 1, :debug => 2){ @log.info("Hello World") } to count messages.
      • assert_info_messages('Hello World'){ @log.info("Hello World") }
      • assert_debug_messages('Hello World'){ @log.info("Hello World") }

      推荐答案

      同时,我创建了一个 minitest-logger -宝石

      一个代码示例如何使用它:

      A code example how to use it:

      require 'log4r'
      require 'minitest-logger'
      
      class Test_log4r < MiniTest::Test
        def setup 
          @log = Log4r::Logger.new('log')
          @log.level = Log4r::INFO
        end    
        def test_output_1 
          assert_log(" INFO log: Hello World\n"){ @log.info("Hello World") }
        end
        def test_output_regex
          assert_log(/Hello World/){ @log.info("Hello World") }
        end  
      
        def test_silent
          assert_silent_log(){
            @log.debug("Hello World")
            #~ @log.info("Hello World")     #uncomment this to see a failure
          }
          refute_silent_log(){
            @log.warn("Hello World")     #comment this to see a failure
          }
        end
      
      end
      

      在测试期间,将临时输出器添加到记录器@log.测试之后,输出器将再次被移除.

      During the test a temporary outputter is added to the logger @log. After the test the outputter is removed again.

      gem支持 log4r 记录器.

      这篇关于如何使用MiniTest测试记录器消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆