Using autotest when developing Rails plugins

I am a great fan of ZenTest’s ‘autotest’ tool and use it continuously whilst developing Rails apps. Up until now I’d not found a way to use autotest while developing plugins.
h3. The problem
After some digging around in the ZenTest source, it turns out that autotest looks for files that are prefixed with test_ however the standard convention in Rails is to use a suffix e.g. base_test.rb. You could simply prefix your tests instead of using suffixes and modify the test file pattern in the Rakefile, however this feels wrong.
I noticed that autotest looks for a .autotest file in the current directory before looking in the home directory meaning that getting autotest to run for plugins is surprisingly simple.
h3. The solution
Create an .autotest file in the root of the plugin directory containing the following:

global_autotest_file = File.expand_path('~/.autotest')
load(global_autotest_file) if File.exists?(global_autotest_file)
class Autotest
 def tests_for_file(filename)
 Dir['test/**/*_test.rb']
 end
end

You can now cd into the plugin dir, run autotest and all works as it does in a Rails app[1]. Also as this file loads the .autotest file in the home directory, any Growl notifiers etc. setup there will all work fine.
fn1. The only difference is that all tests are re-loaded when a test file is saved. This is not an issue for the majority of plugins, if it is, then it’s probably a sign that the tests need optimising.

Leave a comment

Your e-mail address will not be published. Required fields are marked *