In Ruby's Test::Unit, how do I get the current test_ method's name? In MiniTest, this can be done by self.__name__, however, it doesn't work for the full version.

有帮助吗?

解决方案

I got it!

The test name gets passed up the inheritance chain, so I just needed to capture it and save it locally for my reference.

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

其他提示

for completeness with Test::Unit 2.x it's method_name see http://git.io/7yngvg

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

yo can use method, like this example:

class MyTest
  def test_method_example
    __method__
  end
end

It includes a ":" char prefix to the method name

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top