I'm beginning the planning phase of creating a testing suite for my rails 3.0.8 application. I'm trying to decide on which testing framework/gems to use. Normally I prefer to stick to Rails convention as much as possible. However, this means using TestUnit. There are many competing test frameworks to choose from that were created as an alternative to TestUnit. Has TestUnit gotten better over the years, or is it not a very good contender?
I've also heard of a lot of good things about rspec. Are rspec and TestUnit close in terms of functionality, or does rspec blow TestUnit out of the water?
Whatever framework I choose, I'd prefer it to have a good support base(lots of users and documentation), ease of use/simplicity, and a lasting future.
Both Test::Unit and Rspec are very nice solutions. As for me I prefer to use RSpec in my Rails project - because it has many automatic solutions like autotest, highlighting syntax, shortcut commands like rake spec
and other stuff that make using test easer. But in my plain ruby scripts I use Test::Unit - it is very lightweight and staightforward when you don't need complex testing (integrational and others)
The answer is matter of taste, use both in one project and find yourself what do you prefer. (I do Test::Unit, RSpec has to much sugar for me).
Update 2014: As Ruby 2.x has MiniTest built-in I would suggest to check MiniTest::Spec eventually which is simple BDD extension. You can mix "describe" and "it" blocks to nicely structure test contexts with. It also provides "must_be.." matchers if you want to more than assert
.
I prefer and use these gems for testing
- Rspec
- Faker (for creating random and proper data for database)
- Fabrication (for simple creating instances)
To start with rspec take a look at this presentation. It contains all the necessary examples:
http://kerryb.github.com/iprug-rspec-presentation
Also read through these articles on rspec:
http://relishapp.com/rspec/rspec-rails
This Ruby Inside article, especially if you follow the links and read the comments, provides quite a thorough discussion of the relative merits of Test::Unit and RSpec. A key differentiator seems to be that:
- people who have non-technical clients are more likely to view RSpec and Cucumber more favourably;
- others are more likely to prefer more direct systems like Test::Unit.
Also see this part of Michael Hartl's Rails Tutorial for a comparison of RSpec and Cucumber.
You could use rspec
with cucumber
and webrat
gems which makes a great combination, if you have a large website. It will be cumbersome at first but later on it will be a huge benefit.
You could watch the following railscasts episodes to have more info about those gems:
155 Beginning with Cucumber
156 Webrat
157 RSpec Matchers & Macros
159 More on Cucumber
There are also some other gems like capybara
which is relatively new and promising, but honestly I didnt try it.
If you're beginning your journey with testing Rails application I would recommend you start doing it with TestUnit. You can find very nice course about best testing rails app and best practices on http://www.codeschool.com/courses/rails-testing-for-zombies
If you're more experienced when it comes to testing you can try RSpec.