Trouble Shooting Slow Tests in RSpec

To investigate slow running tests in RSpec you can add the --profile to the .rspec file in the root of your Rails project. The output will show the top 10 slow running tests. Some of the tests might seem stuck, in order to see if it is making progress add the following code to spec_helper.rb:

puts "rspec pid: #{Process.pid}"

trap 'USR1' do
  threads = Thread.list

  puts
  puts "=" * 80
  puts "Received USR1 signal; printing all #{threads.count} thread backtraces."

  threads.each do |thr|
    description = thr == Thread.main ? "Main thread" : thr.inspect
    puts
    puts "#{description} backtrace: "
    puts thr.backtrace.join("\n")
  end

  puts "=" * 80
end

When you run the tests, you will see the process id of the RSpec process in the terminal. You can use that process id and run the following command in another terminal.

 kill -USR1 <process id>

This will print the stack trace where you ran the kill command and show which line is currently being executed. If you run it again, you can see whether it is making any progress or not. If it is making progress you will see a different line in your tests in the stack trace.


Related Articles


Create your own user feedback survey