Wednesday, January 26, 2011

Timing your rake test cases in Rails 3: test_benchmark

i was overriding the setup and teardown of ActiveSupport::TestCase in test_helper to time my rake test cases. but in my current project, there are many test cases already overriding the setup method. So, if i still use this way of time tracking, I would have to change too many files.

so, after googling (oh, this google-ing form passes the checkspell ! ) site:github.com, i found test_benchmark. it is super easy to setup.


Include Gem and Rake

if you are using Rails 3, just

  1. add following line to Gemfile (better put it in group :test):
    gem "test_benchmark"
  2. run:
    bundle install
  3. run:
    bundle install
    (or rake test:units if you want to find slow model test cases)

then you can see the top 15 slowest test cases:


Turn it off

you dont want to see all this figures every time you rake test. to switch it off, provide
rake test BENCHMARK=false


Turn it off by default

this is cool gem and some options available to configure.
but actually, i want the other way round.
I want it off by default and turn it on ad-hoc

so i added one line in my test_helper.rb:
Test::Unit::UI::Console::TestRunner.set_test_benchmark_limits(0, 0) unless ENV["SHOW_TIME"]

so now the usage becomes
rake test SHOW_TIME=true



there are some more options there, check out the details at timocratic github:
https://github.com/timocratic/test_benchmark

No comments:

Post a Comment