<p>Whenever I run rspec tests for my Rails application it takes forever and a day of overhead before it actually starts running tests.  Why is rspec so slow?  Is there a way to speed up Rails' initial load or single out the part of my Rails app I need (e.g. ActiveRecord stuff only) so it doesn't load absolutely everything to run a few tests?</p>

[{'answer_id': 66783, 'author': 'Loren Segal', 'author_id': 6436, 'author_profile': 'https://Stackoverflow.com/users/6436', 'pm_score': 2, 'selected': False, 'text': "<p>Are you running this over Rails? If so, it's not RSpec's initialization that's slow, it's Rails'. Rails has to initialize the entire codebase <em>and yours</em> before running the specs. Well, it doesn't <em>have</em> to, but it does. RSpec runs pretty fast for me under my small non-rails projects.</p>\n"}, {'answer_id': 67678, 'author': 'Scott Matthewman', 'author_id': 10267, 'author_profile': 'https://Stackoverflow.com/users/10267', 'pm_score': 6, 'selected': True, 'text': '<p>You should be able to to speed up your <code>script/spec</code> calls by running <code>script/spec_server</code> in a separate terminal window, then adding the additional <code>-X</code> parameter to your spec calls.</p>\n'}, {'answer_id': 67779, 'author': 'Marc-André Lafortune', 'author_id': 8279, 'author_profile': 'https://Stackoverflow.com/users/8279', 'pm_score': 2, 'selected': False, 'text': '<p>Running tests can be really slow because the whole rails environment has to load (try script/console) and only then can all tests run. You should use <a href="http://www.zenspider.com/ZSS/Products/ZenTest/" rel="nofollow noreferrer">autotest</a> which keeps the environment loaded and will check which files you edit. When you edit and save a file, only the tests that depend on these will run automatically and quickly.</p>\n'}, {'answer_id': 68538, 'author': 'Pelle', 'author_id': 10724, 'author_profile': 'https://Stackoverflow.com/users/10724', 'pm_score': 2, 'selected': False, 'text': '<p>If you\'re using a Mac I recommend using Rspactor over autotest as it uses a lot fewer resources for polling changed files than autotest. There is both a full Cocoa version</p>\n\n<p><a href="http://rubyphunk.com/projects/rspactor" rel="nofollow noreferrer">RSpactor.app</a></p>\n\n<p>or the gem version that I maintain at <a href="http://github.com/pelle/rspactor/tree/master" rel="nofollow noreferrer">Github</a></p>\n\n<pre><code>sudo gem install pelle-rspactor\n</code></pre>\n\n<p>While these don\'t speed up individual rspec tests, they feel much faster as they auto run the affected spec\'s within a second of you hitting save.</p>\n'}, {'answer_id': 70568, 'author': 'DEfusion', 'author_id': 6432, 'author_profile': 'https://Stackoverflow.com/users/6432', 'pm_score': 1, 'selected': False, 'text': '<p>If you are on a Windows environment then there is probably little you can do as Rails seems to startup really slowly under Windows. I had the same experience on Windows and had to move my setup to a Linux VM to make it really zippy (I was also using autotest).</p>\n'}, {'answer_id': 74278, 'author': 'Jean', 'author_id': 7898, 'author_profile': 'https://Stackoverflow.com/users/7898', 'pm_score': 2, 'selected': False, 'text': '<p><strong>Why is rspec so slow?</strong> because it loads all the environement, loads fixtures and all that jazz. </p>\n\n<p><strong>Is there a way to speed up Rails\' initial load</strong> you could try using mocks instead of relying on the database, this is actually correct for unit testing and will definitly speed up your unit tests. Additionnaly using the spec server as mentionned by @<a href="https://stackoverflow.com/users/10267/scott-matthewman">Scott Matthewman</a> can help, same with the autotest from zentest mentionned by @<a href="https://stackoverflow.com/users/8279/marc-andre-lafortune">Marc-Andre Lafortune</a> </p>\n\n<p><strong>Is there a way to single out the part of my Rails app I need (e.g. ActiveRecord stuff only) so it doesn\'t load absolutely everything to run a few tests?</strong> what about this </p>\n\n<pre><code>rake test:recent \n</code></pre>\n\n<p>I am not sure how the rspec task integrate with this but you could definitely use the test:recent task as a template to do the same with rspec tests if the.</p>\n\n<pre><code>rake test:rspec:recent\n</code></pre>\n\n<p>doesn\'t exist yet</p>\n'}, {'answer_id': 78867, 'author': 'Orion Edwards', 'author_id': 234, 'author_profile': 'https://Stackoverflow.com/users/234', 'pm_score': 2, 'selected': False, 'text': '<blockquote>\n  <p>because it loads all the environement, loads fixtures and all that jazz. </p>\n</blockquote>\n\n<p>The real culprit is if you run it using <code>rake spec</code>, <a href="http://github.com/dchelimsky/rspec-rails/tree/master/generators/rspec/templates/rspec.rake#L11" rel="nofollow noreferrer">it runs the <code>db:test:prepare</code> task</a>.</p>\n\n<p>This task drops your entire test database and re-creates it from scratch. This seems ridiculous to me, but that\'s what it does (the same thing happens when you run <code>rake:test:units</code> etc).</p>\n\n<p>You can easily work around this using the <code>spec</code> application which rspec installs as part of the rspec gem.</p>\n\n<p>Like this:</p>\n\n<pre><code>cd railsapp\nspec spec # run all specs without rebuilding the whole damn database\nspec spec/models # run model specs only\n\ncd spec\nspec controllers/user* # run specs for controllers that start with user\n</code></pre>\n'}, {'answer_id': 282606, 'author': 'Nappy', 'author_id': 34652, 'author_profile': 'https://Stackoverflow.com/users/34652', 'pm_score': 2, 'selected': False, 'text': '<p>I think the "zen" experience you\'re looking for is to run <code>spec_server</code> and <code>autospec</code> in the background, with the result being near-instant tests when you save a file.</p>\n\n<p>However, I\'m having problems getting these two programs to communicate.</p>\n\n<p>I found an explanation <a href="http://www.ruby-forum.com/topic/145820" rel="nofollow noreferrer">here</a>:</p>\n\n<blockquote>\n  <p>I\'ve noticed that autotest doesn\'t send commands to the spec_server.\n  Instead it reloads the entire Rails environment and your application\'s\n  plugins everytime it executes.  This causes autotest to run\n  significantly slower than script server, because when you run the\n  script/spec command the specs are sent to the spec_server which\n  already has your Rails environment fired up and ready to go.  If you\n  happen to install a new plugin or something like that, then you\'ll\n  have to restart the spec_server.</p>\n</blockquote>\n\n<p>But, how do we fix this issue? I\'m guessing it would involve downloading ZenTest and changing code for the <code>autotest</code> program, but don\'t have time to try it out right now.</p>\n'}, {'answer_id': 2284685, 'author': 'Scott Schulthess', 'author_id': 68393, 'author_profile': 'https://Stackoverflow.com/users/68393', 'pm_score': 2, 'selected': False, 'text': '<p>As of rspec-rails-1.2.7, spec_server is deprecated in favor of the spork gem.</p>\n'}, {'answer_id': 2968206, 'author': 'rogerdpack', 'author_id': 32453, 'author_profile': 'https://Stackoverflow.com/users/32453', 'pm_score': 2, 'selected': False, 'text': '<p>The main reason is that require takes <em>forever</em> on windows, for some reason.</p>\n\n<p>Tips for speedup:\nspork now works with windows, I believe.</p>\n\n<p>You can try "faster_require" which caches locations:</p>\n\n<p><a href="http://github.com/rdp/faster_require" rel="nofollow noreferrer">http://github.com/rdp/faster_require</a></p>\n\n<p>GL.\n-rp</p>\n'}, {'answer_id': 4037689, 'author': 'John Hinnegan', 'author_id': 461974, 'author_profile': 'https://Stackoverflow.com/users/461974', 'pm_score': 5, 'selected': False, 'text': '<p>I definitely suggest checking out spork.</p>\n\n<p><a href="http://spork.rubyforge.org/">http://spork.rubyforge.org/</a></p>\n\n<p>The railstutorial specifically addresses this, and gives a workaround to get spork running nicely in rails 3.0 (as of this moment, spork is not rails 3 ready out of the box).  Of course, if you\'re not on rails 3.0, then you should be good to go.</p>\n\n<p>The part of the tutorial showing how to get spork running in rails 3.0</p>\n\n<p><a href="http://railstutorial.org/chapters/static-pages#sec:spork">http://railstutorial.org/chapters/static-pages#sec:spork</a></p>\n\n<p>Checking when spork is rails 3.0 ready</p>\n\n<p><a href="http://www.railsplugins.org/plugins/440-spork">http://www.railsplugins.org/plugins/440-spork</a></p>\n'}]
['https://Stackoverflow.com/questions/66770', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/8344/']