A quick testing tip

March 5, 2008 – 9:09 am

Generally, when you run your tests in a Rails app you get something like this:

Started
.................E...................
 
1) Error:
test_editors_for_returns_none_for_selection_if_no_editors(PagesHelperTest):
ActiveRecord::StatementInvalid: Mysql::Error: Unknown column 'images.user_id' in 'where clause': SELECT * FROM `images` WHERE (`images`.user_id = 1) LIMIT 1
test/unit/pages_helper_test.rb:20
all_tests.rb:1

You may have noticed that tests are run in alphabetical order (so AccountsControllerTest#test_get_should_render comes before AccountsControllerTest#test_update_should_succeed, which in turn comes before PostsController#test_show_should_set_monkey) - but there’s no indication of any of that in the result string displayed while the tests are running. For that, you have to wait for the report at the end. Unfortunately, though, sometimes you need the feedback during the tests themselves - if, for instance, something is breaking your test suite at a more basic level, such that the end report is never even generated.

Started
.................E................/Users/bscofield/testapp/vendor/rails/ \
activerecord/lib/../../activesupport/lib/active_support/callbacks.rb:90: \
[BUG] rb_gc_mark(): unknown data type 0×0(0×297ce48) non object

Never fear! Try adding the following to your test_helper:

class Test::Unit::TestCase
# ...
 
@@named = {}
 
def setup_with_naming
unless @@named[self.class.name]
puts “\n#{self.class.name} ”
@@named[self.class.name] = true
end
setup_without_naming
end
alias_method_chain :setup, :naming unless defined? @@aliased
@@aliased = true
end

And you’ll get live feedback on which test cases are running:

AccountTest
........
AccountsControllerTest
...........
ApplicationHelperTest
...........
CategoriesControllerTest
.............
CategoryTest
...../Users/bscofield/testapp/vendor/rails/blah blah blah

  1. One Response to “A quick testing tip”

  2. A true life-saver. Thanks!
    Now my hair might have a chance to grow back?

    By Thom Parkin on Mar 5, 2008

Post a Comment