Combining operations in a single test class

Your test classes can become very large and complex if you try to test many member functions in the Test member. A simpler way to test multiple member functions inside a test class is to create a derived class of TTestMultiplexer. This class allows you to write smaller tests that share code.

When you write a test class derived from TTestMultiplexer, each decision function is a separate function, not part of the test member function. You can use the TTestMultiplexer protocol to run all or some of the test class decision functions. This means you can write fewer TTest derived classes if you consolidate several tests inside a single TTestMultiplexer derived class by placing the behavior of each test inside a member function, called a decision function. The decision functions can be executed by name using a text input to select the decision function to be executed in Test.

To make your tests easier to understand, use the same name for the decision functions and the respective member functions. However, there is not always a one-to-one correspondence between decision functions and member functions.

Each decision function decides whether the associated member function in the target class works correctly. If some of the member functions in the target class are overloaded, you must differentiate the associated decision functions in the test class by giving them slightly different names.

To create a test class that performs several tests:

  1. Derive your class from TTestMultiplexer.
  2. Write the decision functions for your test.
    Decision functions are member functions that correspond to the TTest::Test member function. They are different from the Test member function in that they return a Boolean result rather than calling TTest::SetSuccess. A True return value indicates success.
    Each decision function has an associated text key. This key is used to select the decision function at runtime.
    One key comes predefined: kAllDecisions. This key has the effect of running all decision functions and doing a logical AND of the result. If any decision function fails, the entire test is considered to have failed.
    This is an example of one of the decision functions used in the TSampleObjectMultiplexerTest class:
      bool TSampleObjectMultiplexerTest::AlphaFunction()
      {
      unsigned longmaxlength ;
      int             correctlength = 256;
      
      TSampleObject testLength(correctlength);
      maxlength = testLength.GetMaxLength();
      OutputTextStream() << "\nThis is Alpha.\n";
      
      if (maxlength != correctlength)  
          {OutputTextStream() << "Length does not match\n";
          return false; 
      } 
      
      return true;
      }
Note that the code is similar to a function you would write for a class derived from TTest but this code returns either True or False instead of calling SetSuccess.

  1. Override the LoadDecisions member function to call the inherited implementation and add each key-decision pair to a collection.
    Keys are TText objects with all characteristics significant, including capitalization and spacing.
    This is an example of the LoadDecisions function that registers each key-decision pair.
    1  void TSampleObjectMultiplexerTest::LoadDecisions(TDictionaryOf<TText,TDecisionFnWrapper>& keyDecisionPairs) const
    2  {
    3      const TStandardText kAlpha = ("Alpha");
    4      const TStandardText kBeta  = ("Beta");
    5      const TStandardText kChi   = ("Chi");
    6      const TStandardText kDelta = ("Delta");
    7      const TStandardText kEpsilon = ("Epsilon");
    8  
    9      TTestMultiplexer::LoadDecisions(keyDecisionPairs);
    10  
    11      AddDecision(keyDecisionPairs, kAlpha, TTestDecisionFn(&TSampleObjectMultiplexerTest::AlphaFunction));
    12      AddDecision(keyDecisionPairs, kBeta, TTestDecisionFn(&TSampleObjectMultiplexerTest::BetaFunction));
    13      AddDecision(keyDecisionPairs, kChi, TTestDecisionFn(&TSampleObjectMultiplexerTest::ChiFunction));
    14      AddDecision(keyDecisionPairs, kDelta, TTestDecisionFn(&TSampleObjectMultiplexerTest::DeltaFunction));
    15      AddDecision(keyDecisionPairs, kEpsilon, TTestDecisionFn(&TSampleObjectMultiplexerTest::EpsilonFunction));
    16  }
Line numbers 3 through 7 define the keys for the functions.

Line number 9 calls the inherited LoadDecisions function.

Line numbers 11 through 15 register each key-decision pair by calling AddDecision, passing in the keyDecisionPairs identifier, the key, and the function name.


[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