The Artima Developer Community
Sponsored Link

Artima SuiteRunner Tutorial
Building Conformance and Unit Tests with Artima SuiteRunner
by Bill Venners
February 10, 2003

<<  Page 3 of 8  >>


Creating a Suite

To create a test suite, you simply subclass Suite and define test methods. For example, for the account exampleI CTK I created class AccountSuite and gave it three test methods. As shown in Figure 1, AccountSuite is a subclass of Suite. Its three test methods are testConstructor, testDeposit, and testWithdraw.

Figure 1. AccountSuite extends Suite and defines test methods.

If your are familiar with JUnit, you can think of class Suite as the JUnit types TestCase, TestSuite, Assert, and Test all collapsed into one type. Whereas JUnit uses the composite pattern to combine test cases into test suites, Artima SuiteRunner uses plain old composition. Any Artima SuiteRunner Suite can hold references to other Suites.

Creating a Fixture

A fixture consists of objects and anything else needed to perform a test. In general, all test methods in a Suite share the same fixture, which is usually composed of objects stored in private instance variables of the Suite. You can create fixtures in either of two ways, via the constructor of your Suite subclass or via Suite's setupFixture method.

Suite.executeTestMethods calls setupFixture before invoking each test method, and cleanupFixture after each test method completes. These methods can be used to create a fresh fixture before each test method, and destroy it afterwards. The setupFixture and cleanupFixture methods are useful when your test methods destroy or change the fixture. If you are certain your test methods won't destroy the fixture, and you don't need to perform any fixture cleanup, you can simply initialize the private variables that represent your fixture in a constructor of your Suite subclass.

Class Suite's setupFixture and cleanupFixture methods are similar to setup and teardown methods of JUnit's class TestCase. Unlike JUnit, however, Artima SuiteRunner's Suite.executeTestMethods invokes all test methods on the same Suite instance. (JUnit generally creates a different TestCase instance for each test method.) If any of your test methods destroy its fixture such that the fixture can't be used by sibling test methods invoked later on the same Suite object, you should use setupFixture to create the fixture. In setupFixture, you create the necessary objects and perform any other tasks to ready the fixture, such as opening a file or socket. In cleanupFixture, you can release the objects to the whims of the garbage collector and perform any other necessary cleanup, such as closing the file or socket.

<<  Page 3 of 8  >>

Sponsored Links

Copyright © 1996-2018 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use