Simon
Simon

Reputation: 25993

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
  setup :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.

Upvotes: 4

Views: 950

Answers (2)

Jan
Jan

Reputation: 3154

At the moment, it's the method method_name.

Upvotes: 3

Stefaan Colman
Stefaan Colman

Reputation: 3715

The name method gives you a string "method name(class name)" and I think @method_name gives you the name of the current test.

Upvotes: 1

Related Questions