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

Setting up a test case and running it with TestRunner - Bridgette Wiley

One of the most important things to do when creating code for your program is testing it. If you’ve used Java before, you know that you can use the JUnit framework to make test cases to test methods you have written. Here, we will build a basic test case for a method for our supply chain.

Understanding the problem:
We own a store, say, Walmart that just ordered 10,000 copies of Windows Vista from Microsoft. In our store, we must have an inventory to add the items to. So we have an Inventory class, to which we want to test the method addItem(). In our Store class, we have an ‘inventory’ instance variable. In our Inventory class, we have an OrderedCollection of items which is an instance variable named ‘items’.

First things first, create a new class with the identical name of the existing class to which the method belongs. Append the word “Test” to the end. Make sure you are adding the class in your current package.Make sure that the new test class you created extends the SUnit TestCase class (see window entries in screenshot below).
External Image

Great! Now you have your class to contain the methods you want to test. First we need to initialize some values to add to the inventory. We will need a Store and a couple items, i1 and i2 to test our method. Right now the values are red because they are not declared. We want them as our class instance variables, so when you right-click and select “Accept”, choose “Instance Variable” for each. Now the variables should be blue. If they are purple, you selected them as temporary variables. Delete them and start over.

!!If you don’t see color at all, you don’t have the Color Code Highlighting Package uploaded. You can find it at: Main VisualWorks Window -> Parcel Manager -> Environment Enhancement -> RBCodeHighlighting. Right-Click and select “unload”.

External Image

Now we need to add a setUp() method. This method automatically gets called before your test cases. Any values that you need to use should be initialized here.
Right-click -> Accept to save the method.
External Image

Then you’re ready to write your test case for your addItem() method. The name of your method must be preceded by “test”. This allows it to be seen as a test case by the TestRunner when you run the code. Here we call it “testInventoryAddItem”. The simplest way to test your method is to write a series of “assert” and/or “deny” statements. Think about what the pre- and post-conditions are when calling this method. The assert message is formatted like this
self assert: [boolean expression].
Likewise with deny
self deny: [boolean expression].

It is a literal interpretation of the name of the method. Check the expression to see if the right and left sides of the ‘=’ sign match. If true, the test passed. If not, the test failed. Now we will check our conditions for adding an item to the inventory. The following explains the lines of code:
[1] Assure that the list of items in your inventory is empty.
[2] Calls the method you wrote
[3] We need to make sure SOMETHING was added. So ensure the size is 1.
[4] Now we need to make sure that what was added was really an item.
[5] Lets add another to make sure it wasn’t a fluke.
[6] Now, the inventory must have 2 things in it.
[7-8] Not only must the inventory have i1, but it must also have i2. These lines
make sure no item got deleted or went MIA somehow. We’ve all had our
experiences with that.

Remember, check everything you can think of. You never know what might happen when you’ve written a bunch of classes and the methods start misbehaving. Nothing is never “too trivial” to test. Right-click -> Accept to save the method.
External Image

Add as many tests as necessary for each class in your package following the steps above. Now you’re ready to test using TestRunner. Go to: Main VisualWorks window -> Tools -> Workspace.
Type “TestRunner open.”
External Image

Find your class with the test cases you wish to run from the list.
External Image

Click Run. If all your test cases pass, the window will be green. Congratulations!
External Image

If your test cases fail, the window will be red. It will give information on how many of your cases fail.
External Image

In the bottom selection box, you can select which failed case you want to debug. The following debug window should automatically appear. If not, press the “Debug” button. Regardless, press the “Debug” button on the pop-up error window.
External Image

When the debug window opens, you can trace the commands that were called to find where it crashed. If you scroll down the list, you’ll eventually find which line of code failed. Below, we find that it crashed on our first line of code. Note why it failed: we were trying to test to see if an empty inventory size is 1…..Clearly that is a logical error.
External Image

YAY! Now you know how to make and run a test case using TestRunner.

You can look up other methods for testing your cases within the SUnit Package in your system browser.
External Image

Links to this Page