Thinking in C++ Vol 2 - Practical Programming |
Prev |
Home |
Next |
Real projects usually contain many classes, so you need a
way to group tests so that you can just push a single button to test the entire
project. The Suite
class collects tests into a functional unit. You add Test objects to a Suite
with the addTest( ) member function, or you can include an entire
existing suite with addSuite( ). To illustrate, the following
example collects the programs in Chapter 3 that use the Test class into
a single suite. Note that this file will appear in the Chapter 3 subdirectory:
//: C03:StringSuite.cpp
//{L} ../TestSuite/Test
../TestSuite/Suite
//{L} TrimTest
// Illustrates a test suite
for code from Chapter 3
#include <iostream>
#include
"../TestSuite/Suite.h"
#include
"StringStorage.h"
#include "Sieve.h"
#include "Find.h"
#include "Rparse.h"
#include
"TrimTest.h"
#include "CompStr.h"
using namespace std;
using namespace TestSuite;
int main() {
Suite suite("String
Tests");
suite.addTest(new
StringStorageTest);
suite.addTest(new
SieveTest);
suite.addTest(new
FindTest);
suite.addTest(new
RparseTest);
suite.addTest(new TrimTest);
suite.addTest(new
CompStrTest);
suite.run();
long nFail =
suite.report();
suite.free();
return nFail;
}
/* Output:
s1 = 62345
s2 = 12345
Suite "String Tests"
====================
Test
"StringStorageTest":
Passed: 2 Failed: 0
Test "SieveTest":
Passed: 50 Failed: 0
Test "FindTest":
Passed: 9 Failed: 0
Test "RparseTest":
Passed: 8 Failed: 0
Test "TrimTest":
Passed: 11 Failed: 0
Test "CompStrTest":
Passed: 8 Failed: 0
*/ ///:~
Five of the above tests are completely contained in header
files. TrimTest is not, because it contains static data that must be defined
in an implementation file. The two first two output lines are trace lines from
the StringStorage test. You must give the suite a name as a constructor
argument. The Suite::run( ) member function calls Test::run( )
for each of its contained tests. Much the same thing happens for Suite::report( ),
except that you can send the individual test reports to a different destination
stream than that of the suite report. If the test passed to addSuite( )
already has a stream pointer assigned, it keeps it. Otherwise, it gets its
stream from the Suite object. (As with Test, there is an optional
second argument to the suite constructor that defaults to std::cout.)
The destructor for Suite does not automatically delete the contained Test
pointers because they don t need to reside on the heap; that s the job of Suite::free( ).
Thinking in C++ Vol 2 - Practical Programming |
Prev |
Home |
Next |