Created
October 2, 2012 21:24
-
-
Save myronmarston/3823399 to your computer and use it in GitHub Desktop.
How to make specs fail if they are too slow.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In response to https://twitter.com/joshuaclayton/status/253219624982814720:
I've played around with this a bit. In the example above, specs in
spec/unitfail if they take longer than 0.1 seconds, and specs inspec/integrationfail 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.