View this PageEdit this PageAttachments to this PageHistory of this PageHomeRecent ChangesSearch the SwikiHelp Guide
Hotspots: Admin Pages | Turn-in Site |
Current Links: Cases Final Project Summer 2007

Guide to SUnit Testing

It took me a little bit to actually figure out the whole SUnit testing in VisualWorks. So here's a quick guide to how to get it all set up and making it work.

If you open up visual works and go to the system browser, you should find a package called "SUnit". If not, you can download it from the Cincom Repository. Just connect to the repository and then open up the published items window. Search for SUnit and load the package.

Now, go to whichever package you want to use the SUnit test on and create a new class. I'll call mine SUnitTesting. Make sure the superclass is set to #{XProgramming.SUnit.TestCase} that way you have access to all of the methods from the class TestCase inside the SUnit package. Now you've set up a class which will be shown in the dropdown when you run the TestRunner.

To actually set up the SUnit testing, create an instance group of "initialize-release" and create the method "intialize" with the variables that you will be using. Obviously this can be changed when needed. So just to get the hang of it, try starting with just one variable and setting up a simple assert or deny case.

For a simple example, I'll just set up a variable called 'theTestVar'
My initialize method looks like this:
initialize
theTestVar := nil.
^self

Now create a new instance group called "Testing" or something similar. This group will contain all of the tests that you will create for the package. Each test should be targeted towards making sure one specific class in the package works correctly. I will use the SUnit testing that I did for the CRC Card Maker as an example of how to test the core domain objects of the package.

In my CRC Card Maker package, we had about 7 core classes so I tested each one individually. One of core classes was "Attribute". So I created a class inside the SUnitTesting class and named it something like "testAttr". Inside this method is where all of the testing for Attribute will take place. There can be several different levels of testing, but this will count as one single test when running.

So, I take a look in my Attribute class to see how I would initialize a new attribute and what other methods are available to the Attribute class. Since an attribute will essentially an OrderedCollection of strings (attributes). So, inside the testAttr class, I initialize theTestVar to be a new Attribute. Then I try using the methods inside the Attribute class to add and remove attributes.
Here's some basic code inside testAttr:
testAttr
theTestVar := Attribute new.
theTestVar addAttr: 'Testing'.
theTestVar addAttr: 'Number 2'.

Now, there should be 2 elements inside the OrderedCollection inside testAtrr. But how do we really know if it's there? I guess, we could just keep doing Transcript show's but that could take a lot of time if this example was a lot more complex. So, we use SUnit testing. We can simple use the assert/deny test which will show us whether or not the add (and remove) attribute methods are working correctly or not.
Here's adding the assert/deny:
testAttr
theTestVar := Attribute new.
theTestVar addAttr: 'Testing'.
theTestVar addAttr: 'Number 2'.
self assert: theTestVar attributes size = 2.

Running the TestRunner with this piece of code should result in 1 passed test if everything is working correctly inside the Attribute class.

testAttr
theTestVar := Attribute new.
theTestVar addAttr: 'Testing'.
theTestVar addAttr: 'Number 2'.
theTestVar removeAttr: 'Testing'.
self assert: theTestVar attributes size = 2.

Now, running this should result in a failed test inside Testrunner. If you changed self assert to self deny, then it should work.


Running the TestRunner
It's simple, just open up any workspace and type "TestRunner open" and it should bring up a small box with a dropdown which contains all of the SUnit tests it could possibly run. Choose the one you want to run, in this case "testAttribute" and it change the color of the box underneath to green, red, or yellow if it is in the process of running. Obviously green means that all of the tests have passed. And red means that at least one of the tests failed. You can figure out exactly how many tests are failing because it will say at the bottom of the screen. Then, you can click debug and figure out exactly what went wrong.

There are other types of "tests" that you could run besides assert/deny. There is should raise or shouldnt raise for exception handling. Just take a look through the TestCase class inside the SUnit package. There are also some SUnit testing examples in the repository if you are still confused.

Good luck to you all!

Links to this Page