autotest Jan 13 2010

I’ve just recently started using autotest again, and it’s making development so much easier, and more enjoyable. If you do any TDD (test driven development) with Ruby, you need autotest running, as it really improves the experience massively.

With autotest, and growl, you can plod away writing failing tests, implementing code so that they pass, and then rinse and repeat - all the way getting messages flash up telling you when tests are broken, and when tests are passing again. It’s a great way to iterate through writing code to meet user requirements, and incredibly satisfying as you turn failing tests into passing ones, with working code.

So what do you need to do to get it setup? You’ll need to install ZenTest, which contains autotest amongst some other tools:

sudo gem install ZenTest

You can then install the following gems too if you don’t have them already:

sudo gem install autotest-growl
sudo gem install autotest-rails

Finally, you’ll need to setup a small config file, called .autotest in your home directory (~/.autotest):

require "autotest/growl"

That’s all you need in that file, now just hop into a Rails project directory, and run autotest:

cd /path/to/project

You’ll then get output from the initial test run, and from now on while autotest is running, when you edit a file, it will re-run any tests it deems to have been impacted by a change to that file. It will pop up test failures and test successes as growl notifications too. To force it to re-run the entire test suite, press ctrl+c once. To end autotest completely, use ctrl+c twice in quick succession. Bear in mind for things like database schema changes, you’ll need to do the following to get the test database schema up to date and inline with the development database, otherwise your tests when being run under autotest will fail:

rake db:test:prepare

Now get cracking with those user stories, get those tests written, and then turn them from red to green!