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.
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:
You can see the time taken by each test.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With