Combining multiple TTest objects into a single test

To make it easier to manage your instances of TTest, use the TTestCollection derived classes, TTestSet and TTestSequence shown in Figure 7, to group related TTest classes into a single test. The resulting test passes only if all of its subtests pass. This approach allows you to use your TTest classes individually as well as grouped together.

Every test in a group's collection is owned by the group. That means when a group is destroyed, every test in the group's collection is also destroyed.


Creating an unordered group of tests

Derive from TTestSet when you need to create a group of tests in which the order you perform the tests does not matter. In addition, you can have the Test framework randomly reorder the tests each time you perform the tests.

To create a randomly ordered set of tests:

  1. Derive your class from TTestSet.
  2. Override the SetupSubtests member function.
    Use the AdoptTest member function for each test you need to add to the set. Tests will run in the order listed in this function unless you call RandomlyReorder in the Setup member function.
  3. Override the Setup member function.
    Call the parent class Setup member function to add the subtests. Call RandomlyReorder to randomly perform the tests in the set.
    1  void TSampleObjectTestSet::SetupSubtests()
    2  {
    3      TAllocationHeap heap(this);
    4      AdoptTest(new(heap) TSampleObjectSimpleTest);
    5      AdoptTest(new(heap) TSampleObjectFailTest);
    6      AdoptTest(new(heap) TSampleObjectBaseTest);
    7  }
    8  
    9  void TSampleObjectTestSet::Setup()
    10  {
    11      TTestSet::Setup();
    12      RandomlyReorder();
    13  }
Line numbers 4 through 6 add tests to the collection.

Line number 11 calls the base class Setup function to select the tests specified in the SetupSubtests member function.

Line number 12 uses the RandomlyReorder function to force the set of tests to perform randomly each time you call the test class.

Creating an ordered list of tests

Use a derived class of TTestSequence when you have a group of related tests that must be executed in a fixed order. The default behavior is to halt when any subtest fails.

To create an ordered set of tests:

  1. Derive your class from TTestSequence.
  2. Override the SetupSubtests member function.
    Instantiate each of your subtests and add them to the sequence in the correct order using the AdoptFirst and AdoptLast member functions. Do not delete your subtests after calling AdoptFirst or AdoptLast.
      void TSampleObjectTestSequence::SetupSubtests()
      {
          TAllocationHeap heap(this);
          AdoptFirstTest(new(heap) TSampleObjectSimpleTest);
          AdoptLastTest(new(heap) TSampleObjectBaseTest);
      }
When you derive your class from TTestSequence, you do not need to override the Test member function. The parent class automatically runs the set of tests.


[Contents] [Previous] [Next]
Click the icon to mail questions or corrections about this material to Taligent personnel.
Copyright©1995 Taligent,Inc. All rights reserved.

Generated with WebMaker