Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing Rails test names to find the slowest tests

I'd like to find which of the tests are the slowest in my test suite based on this blog post. Here's the minified version of the code:

# test/test_time_tracking.rb
module TestTimeTracking
  class ActiveSupport::TestCase
    setup    :mark_test_start_time 
    teardown :record_test_duration

    def mark_test_start_time
      @start_time = Time.now
    end

    def record_test_duration
      puts "Test class: #{self.class.name}"
      puts "Duration:   #{Time.now - @start_time}"
    end
  end
end

# test/test_helper.rb
require 'test_time_tracking'
include TestTimeTracking
# ...

Is there a way to print out the test name during either the settup or teardown? In the blog post they call name attribute in the teardown block, but this throws an error in my case. I've also tried @name and @method_name with no success.

I'm using shoulda-contexts gem on top of the default Rails test framework. I know that I can get the test name and duration with rake test TESTOPTS=-v, but I will then have to run another script to parse the output.

like image 583
sakovias Avatar asked Dec 14 '22 18:12

sakovias


1 Answers

Use minitest-reporters. Installation Guide is provided on this page. After configration use rspec reporter. i.e in your test_helper.rb file write

Minitest::Reporters.use! [Minitest::Reporters::SpecReporter.new()]

And run the test. This 'll format output like this: enter image description here

You can see the time taken by each test.

like image 76
Malik Shahzad Avatar answered Jan 05 '23 11:01

Malik Shahzad