How do I get the name of a test being run from within a setup or teardown callback?
I'd like to add performance warnings to our tests, like so:
# in test_helper.rb
class ActiveSupport::TestCase
def record_test_start_time
@test_start_time = Time.now
end
s开发者_StackOverflowetup :record_test_start_time
def warn_long_running_test
running_time = Time.now - @test_start_time
if running_time > 10.seconds
puts "WARNING: Test #{test_name} ran for #{running_time}"
end
end
setup :record_test_start_time
end
How do I get the test name into the test_name variable? I've had poor results using Kernel#caller from setup/teardown callbacks in the past.
At the moment, it's the method method_name
.
The name method gives you a string "method name(class name)" and I think @method_name gives you the name of the current test.
精彩评论