Monday, 20 June 2011

autotest, zentest, autotest-rails, autotest-rails-pure: which gems to choose?

It was a little bit confusing for me which "autotest like" gems to choose when I want to autotest my rails application.
Well... There are some alternatives depending which testing framework I choose rspec or Test::Unit, also there are alternatives if I want to generate the tests for my controller actions automatically with zentest or I just want to write them in some other way.



Zentest was the first tool in this domain with a lot of features. Then autotest-standalone was factored out from it. There is also an empty gem called Autotest which is dependent on Zentest.


Because the main idea is to simply map the lib and test directories, in order to test rails application we need to tell the rails specific mappings. This is done by the autotest-rails and rspec-rails and autotest-rails-pure gems. So let's walk through each of the alternatives


Test::Unit


In this case the rails specific mappings are done by the autotest-rails-pure or autotest-rails gems.
The code excerpt which makes the mapping for the controllers is:


add_mapping %r%^app/controllers/application_controller\.rb$% do |_, m|
files_matching %r%^test/(controllers|views|functional)/.*_test\.rb$%
end


For the core autotest we can use the autotest-standalone or the feature rich old Zentest.



Rspec


The rails specific mapping is done by the rspec-rails in case we opt for using rspec



Here is an excerpt wich does the mapping for the controllers:

    add_mapping(%r%^app/controllers/(.*)\.rb$%) { |_, m|
if m[1] == "application"
files_matching %r%^spec/controllers/.*_spec\.rb$%
else
["spec/controllers/#{m[1]}_spec.rb"]
end
}



So there are a plenty of alternatives we can choose from and it is good to know which opportunities are available. We have all we need :)