Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rake tests running very slow

After running some tests, I'm convinced there has to be something wrong with my setup (windows, rubymine and latest ruby versions). My times right now are:

Finished tests in 14.289817s, 0.0700 tests/s, 0.3499 assertions/s.

1 tests, 5 assertions, 0 failures, 0 errors, 0 skips

Process finished with exit code 0

With 5 VERY easy tests (just checking if validation on empty fields works). The total time for these 5 unit tests is 160 seconds, over 2 minutes.

What could I do to improve this speed?

Here are the tests:

require 'test_helper'

class ItemTest < ActiveSupport::TestCase
  test 'item attributes must not be empty' do
    item = Item.new
    assert item.invalid?
    assert item.errors[:name].any?
    assert item.errors[:description].any?
    assert item.errors[:image_url].any?
    assert item.errors[:rating].any?
  end
end
like image 270
Organiccat Avatar asked Aug 05 '12 20:08

Organiccat


2 Answers

Your problem is Windows. We use JRuby on Windows and it actually runs faster than RubyInstaller(mingw) ruby on Windows but we do see very slow results when running test suites or starting a rails server. About 1 minute for a single test run due to the loading of the Rails environment. You have a few options:

  1. Switch to linux / osx
  2. Use spork to keep a couple rails environments pre-loaded for your tests. Note that this isn't perfect but it will reduce your times substantially. With this option you'll probably want to use minitest or rspec, I had trouble getting spork to work on Windows with testunit. With spork you should be able to get your single test run time down to about 10 seconds.
  3. Write as many of your tests to run outside of Rails, in other words to not require the Rails stack. This will be very fast, you should be able to run a test in only a few seconds but as you could guess, it's hard to test a lot of things (controllers, views) outside of rails. Works perfectly though for functions you've broken out into modules that already do not require anything from rails.

Good luck!

like image 189
Dark Castle Avatar answered Oct 25 '22 04:10

Dark Castle


What's the rest of your gem stack? Sometimes third-party gems are initialized by rails and will try to phone home (New Relic, Airbrake) which can inflate your test times (though probably not by this much). If something isn't strictly required for your test suite, you should try to pull it into the proper env group, or set require :false via bundler:

group :production do
  gem 'newrelic_rpm'
end
like image 42
jodell Avatar answered Oct 25 '22 05:10

jodell