ruby单元测试启动或设置中的记录器

ruby单元测试启动或设置中的记录器,ruby,unit-testing,Ruby,Unit Testing,我正在用ruby编写一些测试,我有这个问题 class MyTest < Test::Unit::TestCase LogFile = "test.log" Log= Logger.new(LogFile) def startup @log = Log end def shutdown end def setup end .... end classmytest

我正在用ruby编写一些测试,我有这个问题

class MyTest < Test::Unit::TestCase
    LogFile = "test.log"
    Log= Logger.new(LogFile)

    def startup
       @log = Log
    end

    def shutdown
    end

    def setup
    end

   ....

end
classmytest
我试图在启动时创建一个记录器,但是,它不能很好地工作。在测试用例中,@log是nil。
我知道我可以将它放入设置中,但是,我不想为每个测试用例重新分配它。为什么它不起作用?正确的方法是什么?

startup
应该用作类方法。在那里可以定义一个类变量,如
@@log

如果要使用实例变量
@log
,则必须将其填入
setup

例如:

require 'test/unit'
require 'logger'
class MyTest < Test::Unit::TestCase
    LogFile = "test.log"
    Log= Logger.new(LogFile)

    def self.startup
       @@log = Log
    end

    def setup
       @log = Log
    end

    def test_class_var
      assert_kind_of(Logger,@@log)
    end
    def test_instance_var
      assert_kind_of(Logger,@log)
    end

end
要求“测试/单元”
需要“记录器”
类MyTest
这正是我想要的。那么在这种情况下,日志文件和日志呢?它们是否被视为实例变量?另外,是否建议将测试类实例和记录器置于启动状态?日志和日志文件是常量。在实际示例中,不需要定义@log或@log,也可以使用log。