views:

299

answers:

3

I'd like to know how much took to run the 10 most time consuming tests.. like I can do with rspec, any suggestion?

A: 

Maybe run your unit tests under the ruby profiler by executing the code with ruby -rprofile instead of just ruby?

Timo Geusch
Thanks for the answer, i'm trying to run it like you said but i get this:/usr/lib/ruby/1.8/profiler.rb:13:in `hash': bignum too big to convert into `long' (RangeError)
ClaudioA
+1  A: 

Perhaps this plugin can help you.

Simone Carletti
Right now i'm trying to port rspec to unit test...
ClaudioA
+1  A: 

Short answer:

gem install minitest # Install MiniTest
gem install minitest_tu_shim # Install Test::Unit shim
use_minitest yes # Use MiniTest Test::Unit shim instead of stdlib Test::Unit 
ruby your_test.rb -v # Run your test in verbose mode

Ruby 1.9 use MiniTest as its default testing framework instead of Test::Unit. MiniTest is smaller, faster, has more useful features, and is largely backward compatible with Test::Unit. One of those newer features is measuring the time each test takes with the -v flag. When you run the e sure you place this flag after the script

If, as in rails, you use Rake::TestTask to run your tests, you'll can either specify it at runtime by running

rake test TESTOPTS='-v'

or specify it in the task by adding -v to the options attribute, like so

Rake::TestTask.new do |t|
  t.options = '-v'
end

Finally, if you're using rails and MiniTest just isn't good enough for you, you might appreciate the test_benchmark plugin. Usage is easy. Add the following line to your config/environments/test.rb

config.gem "timocratic-test_benchmark",
  :lib => 'test_benchmark',
  :source => 'http://gems.github.com'

Install it with

RAILS_ENV='test' rake gems:install

From then on, you'll get a nice sorted list when you run your tests

rake test:units
[...]
Test Benchmark Times: Suite Totals:
7.124 test_destroy(FeedTest)
7.219 test_create(FeedTest)
7.646 test_subscribe_to_auto_discovery(FeedTest)
9.339 test_auto_discover_updates_url(FeedTest)
9.543 test_find_or_create_by_auto_discover_url(FeedTest)
15.780 test_import_from_opml(FeedTest)

I'm sorry to say that MiniTest and the test_benchmark plugin are incompatible with each other, but I strongly encourage you to try MiniTest, since it'll make your tests faster, and will continue to be supported in Ruby 1.9.

Joseph Holsten
Really cool! Thanks a lot man!
ClaudioA
Recent versions of the `test-unit` gem also display time taken in verbose mode.
Andrew Grimm
oops I've test this and I get: ARGH! someone defined Test::Unit::TestCase rather than requiring/usr/local/lib/site_ruby/1.8/rubygems.rb:777:in `report_activate_error': Could not find RubyGem test-unit (= 1.2.3) (Gem::LoadError)Any idea?
ClaudioA