PackageDescription: SUnit-SimpleResources


S Unit - Simple Resources

Last published: November 17, 2008 by 'mkobetic'

Defines 3 Classes
Extends 3 Classes


The core SUnit package provides support for shared test resources via the TestResource class. A TestCase that wants to use TestResources is expected to list all the resource classes in its class side #resources method. Individual test case methods then access the resources via the resource classes, usually as default, singleton instances. That provides potentially interesting levels of flexibility, however the access to the resources themselves is not exactly convenient. In my experience the vast majority of cases involving TestResources either keep repeating the 'self resources first default blah' incantation over and over again, where blah is the name of the real resource the case cares about which is being managed in a blah instance variable of the corresponding TestResource subclass. A more palatable way is adding the same instance variables to the TestCase subclass as well and copying the resource pieces there in the TestCase>>setUp method. Then you can access the resources directly as instance variables in your test case methods. So the test methods are clean again, but wouldn't it be nice if we didn't have to go through the
1) create a TestResource subclass
2) add it to the TestCase class>>resources method
3) add the same set of instance variables to the TestCase subclass
4) copy the contents of the TestResource default instance to the TestCase instance variables in TestCase>>setUp method
over and over again. That's what this package is about.

The ResourcefulTestCase is an abstract test case class with simplified version of shared test resource support. Instead of separate classes of test resources, it makes sure that if there are class side #setUp and #tearDown methods defined on the class, they run before and after the test suite that gets built out of this test class. This allows one to initialize and store shared resources in class side variables in the #setUp method. It's probably easiest to use shared class variables for easy access from the instance side test case methods. I also like that the capitalized first letter nicely highlights in the test code the difference between the shared resources and the private stuff in case instance variables. Obviously the resources need to be torn down in the class side #tearDown method. It is usually also important to nil out the variables as well so that the class doesn't hand on to too much garbage. The nilling out could be done automatically with a bit of meta-programming, but since it's usually also necessary to finalize/close/release the resources properly as well, I figured it's better to force the user to deal with that, rather than facilitate potentially serious leakage with more automated magic. It's probably also a good idea to call super implementations of the setUp/tearDown methods so that case hierarchies work well. So here's a quick summary of how to create a test case with resource:

1) Create a subclass of ResourcefulTestCase:

XProgramming.SUnit defineClass: #MyTest
superclass: #{XProgramming.SUnit.ResourcefulTestCase}
indexedType: #none
private: false
instanceVariableNames: ''
classInstanceVariableNames: ''
imports: ''
category: 'My Tests'

2) Add class side setUp method and initialize the test resources there

MyTest class>>setUp

A := Object new.
Client := HttpClient new connect: 'testserver'

3) Add class side tearDown method releasing the resources

MyTest class>>setUp

A := nil.
Client close.
Client := nil.

And that's it. Now you can simply use the resources in your test methods:

MyTest>>testResources

self assert: A class == Object.
self assert: (Client get: 'index.html') isSuccess