Autotest for Rails

The zentest module for Ruby has five different pieces, but I’m specifically interested in autotest and Test::Rails. You start up autotest and it watches your files to see what changes. When a file is saved, autotest notices and kicks off unit and functional tests that were affected, so it’s speedy. It’s instant feedback!

I keep putting off trying to do test driven development (TDD) because, well, it’s not how I’ve done it before. The idea of writing tests hasn’t been my idea of a good time. However, I’m getting motivated to try it out now, partly because of autotest. Instead of writing larger chunks of code, then going back and adding tests for that section (or not adding tests) I’m going to try writing tests first.

Now that I think of it, I do spend a lot of time just waiting for a page to refresh, then it’s not quite right and I debug it, and so on. Or, I think “Oh, maybe that should be green” and I play with the styles or something, instead of keeping in flow of application development.

Some quotes about the whole test-driven development/zentest/autotest idea:

It makes it hard NOT to do test-driven development. You see the screen sitting there like a hungry fire waiting for another test. Like a fire, it also has an infinite hunger, so you will never beat it. — topfunky

Improves feedback by running tests continuously.
Continually runs tests based on files you’ve changed.
Get feedback as soon as you save. Keeps you in your editor allowing you to get stuff done faster.
Focuses on running previous failures until you’ve fixed them.
zentest page

So, I’m convinced. Well, I’m convinced to try it. Maybe it’ll just suck, but maybe I’ll bring something away from it. Let’s get down to it.

Here are some of the questions I had when getting started using autotest for Rails development:

Does it run on Windows?
Yes.
What environment does it run in? Production or test?
Test. In fact, you have to prepare the test environment (via a rake task) before you get started. See below.

I started by installing the ZenTest gem:

C:railsmyapp>gem install ZenTest
Attempting local installation of 'ZenTest'
Local gem file not found: ZenTest*.gem
Attempting remote installation of 'ZenTest'
Updating Gem source index for: http://gems.rubyforge.org
Successfully installed ZenTest-3.3.0
Installing RDoc documentation for ZenTest-3.3.0...

Note that I’m using version 3.3.0, so if you’re reading this and you’ve got a earlier or later version, read on with that understanding.

Get your test database ready with:

C:railsmyapp>rake db:test:prepare

On Windows, you need to add an environment variable HOME because autotest looks for a .autotest file there.

C:railsmyapp>set HOME=C:railsmyapp

Start up autotest.

C:railsmyapp>autotest -rails

I actually get an error when first starting up, but I think that’s because there’s not a previous test result file to compare to. So, change a file and watch autotest run the relevant test.

Started
F
Finished in 0.301 seconds.

1) Failure:
test_truth(MyControllerTest) [./test/functional/my_controller_test.rb:16]:
false is not true.

1 tests, 1 assertions, 1 failures, 0 errors 

Next:
RTFM. Seriously. It’s in the lib\ruby\gems\1.8\doc\ directory and it tells you a few things about how to change your tests to be better. Make sure you read the Test::Rails module documentation. There are at least three suggestions:

You will need to make three small changes to test/test_helper.rb to set up Test::Rails:

First, add the following to ‘test/test_helper.rb’ before you require test_help:

require 'test/rails'

Next, change the class from “Unit” to “Rails” right after you require test_help.

Your ‘test/test_helper.rb’ will end up looking like this:

ENV["RAILS_ENV"] = "test"
require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
require 'test/rails'
require 'test_help'

class Test::Rails::TestCase
...

Finally, you need to add the extra rake tasks Test::Rails provides. Add the following line to your Rakefile after you require ‘tasks/rails’:

require 'test/rails/rake_tasks'

There’s more, but you really need to read the docs in order to find out how to use view tests and how controller tests are changed.

Now, lay out your application and write the test. Type them in. Watch them fail until you comment them out to start afresh.
Uncomment a test. Fail. Write code. Yes! Repeat.

I’m going to be most interested in seeing how writing the tests affects my application layout thinking. Will it force me to think earlier about what logic goes where? I’m not sure yet.

Snippets from the autotest documentation:

If you want Autotest to start over from the top, hit ^C once. If you want Autotest to quit, hit ^C twice.

Plugins are available by creating a .autotest file either in your project root or in your home directory. You can then write event handlers in the form of:

Autotest.add_hook hook_name { |autotest| ... }

The available hooks are: run, interrupt, quit, ran_command, red,

green, all_good, and reset.

See example_dot_autotest.rb for more details.

Other resources:

3 Comments »

  1. Ron Lambkin Said,

    September 6, 2006 @ 8:25 am

    Some of your links are to your local file system.

  2. Wes Said,

    September 6, 2006 @ 9:38 am

    Thanks a bunch, Ron. I’d just pulled that straight from the documentation.

  3. Nick Said,

    November 1, 2006 @ 9:29 pm

    I realize I’m really late on this, but I just have to thank you. I’d about given up on using autotest (couldn’t find the HOME environment…) before I found this. Thanks.

RSS feed for comments on this post · TrackBack URI

Leave a Comment

Comments are moderated.