Test::Unit gotchas

January 13, 2009 by alex

I’ve run across a few things worth remembering when writing unit tests for Ruby using Test::Unit. Might be obvious to some, but they tripped me up once or twice.

“`ruby class SomeTests < Test::Unit::TestCase def testsomethinghappened puts ‘A’ end

def testsomethinghappened puts ‘B’ end end ”`

Because you’re allowed to modify methods at runtime, you get ‘B’ in your test output, and the first test_something_happened is never run. Shouldn’t usually be a problem, but this is one of those times I wish you could make Ruby return an error, or at least a warning, when methods are redefined. If 2 tests get the same name, you could have a broken test with no warnings.

ruby def test_load!_throws_exception assert_raises SomeException do Record.load!( invalid_record ) end end

Apparently, you can name a method ‘load!’, but you can’t include ‘!’ in the name of a test method. test_load!_throws_exception is never run, and no errors are generated. When I rename the method to test_load_bang_throws_exception, then the test actually gets run.

Running your test case in this way : bash $ ruby -w2 test/unit/some_test.rb will at least get you a warning, but it’s not the default. It would be nice if the Rails rake tasks enabled warnings like that when running ‘rake test’.

I don’t like things like this because it makes it unclear what it means when your test suite completes with no errors. Does it mean all your tests passed? When tests can be silently skipped, you can’t be sure, and you have to constantly watch for things like this. I wonder what else gets silently ignored?

☙ ☙ ☙