Shoulda You abandon Test::Unit?

I was not very keen on RSpec. Well to be honest I had some problems with writing tests at all. Fortunately old and bad times are gone. I mean writing test is now much more natural and has become second nature. That way I don’t need to force myself to write them. You know, attacking new ideas is much more sexier than boring (most of time) tests ;)

As freelancer I do often jump into some established project, with long history and different tools used. This has advantage I can watch other developers how theirs code looks, and learn from it how things can be done (there are always exceptions).

In one of such projects I’ve found Shoulda plugin. This plugin adds to Test::Unit bunch of nifty helpers, making live much easier.

First, stop writing those damn underscores and make tests much more readable:

context "Facebook app" do
    context "when removed " do
      setup do
         #some init code
      should "accept only HTTP post" do
          #request and assertions
      should "clear FB bindings" do
         #request and assertions

context allow to group tests and provide some common setup phase. Naming tests is much more easier and allow DRY, making test titles much more compact. However when running tests it will output names of tests (when some errors/failures) perpending them with all context names.

"Facebook app when removed should accept only HTTP post"
"Facebook app when removed should clear FB bindings"

More readable? You bet! But that is not all! Helpers to make even more less typing, this time with assertions. Just check docs for this plugin (note there is also non-rails version, Shoulda gem, which provides cut-down features, but is not coupled with Rails, so can be used in other projects using Test::Unit).

For me, RSpec is still not test framework of choice. Maybe the other day I will drink RSpec’s Kool-Aid, but for now I prefer Test::Unit + Shoulda plugin.

If there are some RSpec zealots, what is reason for You to choose it over Test::Unit?

Join the Conversation


  1. I use RSpec, vanilla Test::Unit, little to zero shoulda – I have never stumbled upon shoulda when searching thru the codes of other people I work with. The biggest difference is not in the syntax. It’s in the name of the framework. Test::Unit implies testing – something that should be done after programming. On the other hand we have RSpec – specs, something that must exist before we write antyhing.

    After using Test::Unit I knew that it’s good. Be I didn’t write too many tests then. After I started using RSpec (maybe two months had to pass) I understand and it became my second nature to write specs before coding.

    Now when I have to write some tests (in Test::Unit), it’s not a problem at all. It’s just a different syntax.

  2. Mnie te? kiedy? ilo???RSpecowej magii odstraszy?a od test?w, ale jak zapozna?em si? z prostot? i wygod? Shoulda, to zacz??em je pisa? na powa?nie.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.