How can I do to profile ruby Unit tests execution?
I'd like to know how much took to run the 10 most time consuming tests.. like I can do with rspec, any 开发者_如何学Csuggestion?
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.
Maybe run your unit tests under the ruby profiler by executing the code with ruby -rprofile
instead of just ruby
?
Minitest::Profile is a new gem that lists the top offenders after a standard test run.
https://github.com/nmeans/minitest-profile
精彩评论