Short testing feedback in rails 3

Good TDD requires Fast Feedback

When practicing TDD, testing feedback time should be super fast. You cannot sit and wait for your tests to run minutes before getting your red / green notification.

Tests should not be IO bound (IO == slow), you should use in memory db / preloaded data.

Another factor is the environment start-up time.
In rails 3 environment, sometimes for applications with heavy gem dependencies, loading time can be very long. Even up to 1 minute in some cases. We cannot accept that when it comes to test that depends on rails environment to be up. What can we do?

Spork & Guard to the rescue

Spork is using fork of rails environment in the background. You can configure your RSpec / Test::Unit to use the preloaded environment using drb (ruby rpc).

But what if you change initialization files? -changes will only affect next rails start-up

For this task guard, which uses fs events to monitor changes in files can be configured to inflict spork reload of rails environment.

The following post describes well how to install and configure Spork & Guard with RSpec

Next step: autotest

Now that the environment is preloaded we are nearly done.

But… why should we run rspec manually each time we wish to run test? – if we are practicing TDD well we probably wish to run the tests VERY often.

‘autotest’ uses fs-event to listen to file modification within our rails directory tree and to run the tests each time we SAVE a pre-configured file (e.g. spec files)

growl is a nice tool which shows a visual notification of the testing status green / red with number of failures and total number of test run.

We find this setting to work fine:
In your Gemfile (test group)

gem 'webrat'
gem 'ZenTest'
gem 'autotest-rails'
gem 'autotest-growl'
gem 'autotest-fsevent'

and in your .autotest file (rails root):

require "ZenTest"
require "autotest/rails"
require "autotest/growl"
require "autotest/fsevent"
Autotest.add_hook :initialize do |autotest|
  %w{.git .svn .hg .DS_Store ._* vendor tmp log doc config .rvmrc Gemfile .autotest README Rakefile}.each do |exception|

Side note: Continuous testing vs. Compilation

When you are getting use to test drive you code, it starts feeling similar to compiling your code (if you are using a language that require compilation ;-))

Auto-testing inline notification is pretty much like modern IDEs notifying syntax errors and compilation errors before you run your build.
The major difference is that compilation only tells you the code can run, whereas testing provides evidence that it actually work!!


, ,

  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: