【问题标题】:Ruby Test::Unit's current test method nameRuby Test::Unit 的当前测试方法名称
【发布时间】:2013-01-31 22:47:13
【问题描述】:

在 Ruby 的 Test::Unit 中,如何获取当前 test_ 方法的名称?在 MiniTest 中,这可以通过self.__name__ 完成,但是,它不适用于完整版。

【问题讨论】:

  • 得到它后你打算怎么做?如果您对此进行解释,也许我们可以推荐另一种编写测试的方法,该方法不涉及查找方法名称。
  • 我在测试方法上有一些注释,我需要通过它的方法名称来获取。

标签: ruby unit-testing methods testunit


【解决方案1】:

我明白了!

测试名称在继承链中向上传递,因此我只需将其捕获并保存在本地以供参考。

def initialize(name = nil)
    @test_name = name
    super(name) unless name.nil?
end

【讨论】:

    【解决方案2】:

    对于 Test::Unit 2.x 的完整性,它是 method_name 参见 http://git.io/7yngvg

    def setup
      puts self.method_name # will output e.g. "test_check_username"
    end
    

    【讨论】:

      【解决方案3】:

      你可以使用方法,比如这个例子:

      class MyTest
        def test_method_example
          __method__
        end
      end
      

      它在方法名称前包含一个“:”字符前缀

      【讨论】:

      • 是的,这在方法之内。我说的是方法之外。在继承链上。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-20
      相关资源
      最近更新 更多