Skip to content

Instantly share code, notes, and snippets.

@myronmarston
Created October 2, 2012 21:24
Show Gist options
  • Select an option

  • Save myronmarston/3823399 to your computer and use it in GitHub Desktop.

Select an option

Save myronmarston/3823399 to your computer and use it in GitHub Desktop.
How to make specs fail if they are too slow.
RSpec.configure do |c|
{ unit: 0.1, integration: 0.5 }.each do |type, max_time|
c.after(:each, example_group: { file_path: %r|spec/#{type}| }) do
run_time = Time.now - example.metadata.fetch(:execution_result).fetch(:started_at)
if run_time > max_time
raise "Example took too long: #{run_time} seconds (max is #{max_time} seconds)."
end
end
end
end
@myronmarston
Copy link
Author

In response to https://twitter.com/joshuaclayton/status/253219624982814720:

I've played around with this a bit. In the example above, specs in spec/unit fail if they take longer than 0.1 seconds, and specs in spec/integration fail if they take longer than 0.5 seconds.

Problem is, I found the runtime of individual specs to be pretty inconsistent, presumably due to the random times GC kicks in. You'd probably have to control the GC so that it never runs during an example to make the times more consistent to use this approach, or make the times reallly lenient.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment