This post originated from an RSS feed registered with Ruby Buzz
by Daniel Berger.
Original Post: Declarative Tests
Feed Title: Testing 1,2,3...
Feed URL: http://djberg96.livejournal.com/data/rss
Feed Description: A blog on Ruby and other stuff.
As a result of the current RoR gig I've become addicted to the declarative syntax for test-unit:
class TC_Foo < Test::Unit::TestCase
test "foo should return the string 'hello'" do
assert_equal('hello', @obj.foo)
end
# vs.
def test_foo_should_return_hello
assert_equal('hello', @obj.foo)
end
end
When you first look at that you'll probably think it's just a silly cosmetic change. But, after using for a while I have to admit that it's had two effects on my testing, both positive.
First, and foremost, I now write more descriptive test descriptions. With the old "def test_something" I found that I tended to be overly terse. With the declarative syntax I tend to be more descriptive. That's a good thing, because it's easier for me (or someone else) to look at that test later and more easily determine just what the purpose of that test is.
Second, I tend to break out groups of assertions that I had previously lumped together into separate tests. This is a function of the first change, i.e. by writing more descriptive tests I limit the assertions to those that actually match my description. So, if an assertion is actually testing something *slightly* different, I feel more comfortable (for lack of a better word) writing it out into a separate test because I'm writing a more verbose description.
I'm not necessarily down to one assertion per test (as some people advocate), but my average number of assertions per test definitely dropped. :)
BTW, the declarative syntax has been added to test-unit 2 and will be part of the 2.0.6 release!