This post originated from an RSS feed registered with Agile Buzz
by James Robertson.
Original Post: A Unit Test Pattern
Feed Title: Travis Griggs - Blog
Feed URL: http://www.cincomsmalltalk.com/rssBlog/travis-rss.xml
Feed Description: This TAG Line is Extra
There's a pattern that I've begun using with some UnitTests and I kinda like it. Often, I end up with a TestCase that has a single inst var. The setUp method sets that to some default instance of the object being tested. And, if there's an external part (things like files, or windows opened, etc), I use my tearDown to clean it all up. Invariably, I'll end up with one or two tests amongst many that needs TWO instances of the class of interest. Maybe I'm testing comparison, or copying between, or something. So I can wrap the entire test:
[blah
blah
blah] ensure: [make sure you do the equivalent of tearDown for the alternate thing]
That's ugly. And funky to debug. Or, I can add another ivar to setUp and tearDown, which seems, I dunno, bloated.
Lately, I've experimented with having a single collection object that "registers" these things. So end up with methods that look something like:
setUp
theNormalTestThing := self newThingToTest
tearDown
self testThings do: [:each | "do the tear down per thing"]
testThings
^testThings ifNil: [testThings := Set new]
Then for tests that need a second thing to test, they just call the newTestThing, and everything's still cleaned up hunky dorey. There's gotta be a pattern there. I've always wanted to put my name on a pattern.