Workspace 6.21.5
Classes | Public Member Functions | List of all members
TestSuite Class Reference

Class for implementing a suite of related tests. More...

#include <Workspace/testcommon.h>

Classes

class  TestCase
 Nested class template defining a test case for test TestNum. More...
 

Public Member Functions

 TestSuite (const std::string &name)
 
 ~TestSuite ()
 
void addTest (TestHarness *t)
 
template<int NumTests>
void addTestCases ()
 
template<>
void addTestCases ()
 
const std::string & getName () const
 
int run (bool captureOutput=true) const
 
void setName (const std::string &name)
 
int skipAll (const std::string &msg="") const
 

Detailed Description

To use this class, you must add tests to the test suite and then call run(). A class template called TestCase is provided to simplify writing your own test cases. Just create an explicit specialization of runTest() for each test number T and add that test to the test suite. For example:

{
// Do testing stuff here for case 1....
}
// Test numbers don't have to be sequential
{
// Do testing stuff here for case 3....
}
void runTest() override
Definition: testcommon.h:1006

Then in your main function, add each test to the test suite and run the whole test suite. It is not necessary to make the test numbers used for the template parameter be the same as the test number for it in the suite itself. The test suite assigns its own name and number to each test case when run() is called. The tests can be added one at a time like this:

int main(int argc, char* argv[])
{
TestSuite suite("My test suite");
suite.addTest(new TestSuite::TestCase<1>);
//suite.addTest(new TestSuite::TestCase<2>); // See below
suite.addTest(new TestSuite::TestCase<3>);
suite.addTest(new TestSuite::TestCase<4>); // See below
return suite.run();
}
Nested class template defining a test case for test TestNum.
Definition: testcommon.h:1001
Class for implementing a suite of related tests.
Definition: testcommon.h:851
int main(int argc, char **argv)
Definition: wsscheduler_mongodb.cpp:81

Or you can add the whole set of test cases in one go using the addTestCases() function:

int main(int argc, char* argv[])
{
TestSuite suite("My test suite");
suite.addTestCases<4>();
return suite.run();
}

Any test case for which a specialized runTest() is not provided will be treated as skipped, so for TestCase<4> in the first example above, that particular case will get treated as skipped. In the second example, cases 2 and 4 are both skipped as well but if a runTest() specialization is ever provided for that case, it will automatically be included.

As a general guide, unless you have more than 17 test cases for the test suite, prefer to use addTestCases() because it is easier to maintain.

Constructor & Destructor Documentation

◆ TestSuite()

TestSuite ( const std::string &  name)
inline
Parameters
nameThe name of the test suite.

◆ ~TestSuite()

~TestSuite ( )
inline

Deletes all tests that were added to the test suite using addTest().

Member Function Documentation

◆ addTest()

void addTest ( TestHarness t)
inline
Parameters
tThe test to add to this test suite. The test suite will take ownership of t and will delete it when the test suite is deleted.
See also
addTestCases()

◆ addTestCases() [1/2]

void addTestCases ( )
inline
Template Parameters
NumTestsThe highest test case number whose runTest() function has a specialization defined.

This function template instantiates itself recursively to add each test case to the test suite. Test case zero is treated specially and is used purely to mark the end of the cases to add (ie there can never be a test case zero).

Note
Some compilers may not cope with a large number of tests added to a test suite with this funciton. Specifically, the C++ standard only requires a compiler to support a minimum template nesting level of 17. In practice, it would appear that mainstream compilers support more than that, but if you are concerned about this issue, avoid using addTestCases() with a NumTests value higher than 17.
See also
addTest()

◆ addTestCases() [2/2]

void addTestCases ( )
inline

◆ getName()

const std::string & getName ( ) const
inline
Returns
The name assigned to the test suite.

◆ run()

int run ( bool  captureOutput = true) const
inline

◆ setName()

void setName ( const std::string &  name)
inline
Parameters
nameThe name to assign to the test suite.

◆ skipAll()

int skipAll ( const std::string &  msg = "") const
inline
Parameters
msgSome information about why the test suite was skipped.

Outputs an appropriate SKIPPED message for this test suite and then returns the code the application should exit with.

Returns
Always the code for a SKIPPED test (currently 1). This should be the exit code of the application.