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


Ace the Technical Interview

  • Easily find the gaps in your knowledge
  • Get customized lessons based on where you are
  • Take consistent action everyday
  • Builtin accountability to keep you on track
  • You will solve bigger problems over time
  • Get the job of your dreams

Take the 30 Day Coding Skills Challenge

Gain confidence to attend the interview

No spam ever. Unsubscribe anytime.