CppTest
Version 2.0.0
|
CppTest is a portable and powerful, yet simple, unit testing framework for handling automated tests in C++. The focus lies on usability and extendability. Several output formats are supported and new ones are easily added.
This tutorial is intended to quickly get you started.
CppTest is released under the GNU Lesser General Public License.
Each test must be created and run within a test suite (see Test suites below), which must be derived from Test::Suite. Tests must be registered with the TEST_ADD(func) macro inorder to be executed. Note that a test function must return void
and take no parameters. For example:
Asserts are used to ensure that the tests are correct. If not, one or more asserts will be generated. There exist several types of asserts, see Available asserts for a complete list.
For example, the functions declared within ExampleTestSuite
may be declared as:
An output handler takes care of all assert messages and generates some output as result. There exist several output handlers that all fit different needs. Currently, the following output handlers exist:
New output handlers should be derived from Test::Output.
The result from the different output handlers is shown below:
The tests within a test suite are all executed when Test::Suite::run() is called. This method returns a boolean value that only returns true if all tests were successful. This value may be used to return a suiteable value from the program. For example, the following is required to execute our tests within ExampleTestSuite
:
Note that a single test normally continues after an assert. However, this behavior may be changed with an optional parameter to Test::Suite::run(). For example:
Test suites may also be added other test suites using Test::Suite::add(). This way, you may develop logically separated test suites and run all tests at once. For example:
Test cases in the same test suite often share the same requirements, they all require some known set of objects or resources. Instead of repeating all initialization code for each test it may be moved to the Test::Suite::setup() and Test::Suite::tear_down() functions. The setup function is called before each test function is called, and the tear down function is called after each test function has been called. For example: