This is homepage of unit++, a C++ unit testing framework similar to junit, yet intended to be more C++ like than CppUnit (the C++ port of junit).
There are at least one other thing in the project that I am proud of; a C++ replacement for getopt that uses a command pattern to directly execute some code when the option is present, rather than just return the option.
The test system actually works now, that is it can test itself, so the code is actually ready for use. There is a configure installation, so that part should be fairly easy.
In general I have nothing to say about how and why to test, that has not already been said by Erich Gamma and Kent Beck, look at The junit homepage.
However the original reason for unit++ is that I did not find that the C++ port of junit was very C++ like. This has to do with garbage collection, templates, reflection, and other features that are C++ or Java features only.
In order to start testing with unit++ you need some test cases. These are normally grouped into test suites, to impose some order. You can actually get away with just that, a class that derives from the test class and implements the function call operator. However, the suite approach is usualy done by making a class that derives from suite, make the individual tests as methods in the suite, and make a constructor of the suite class that adds the test methods as individual tests in the suite. This could look something like
namespace { #includeThe anonymous namespace allows all my test suites the use the same class name which I have found relieves my creativity from much anguish.class Test : public suite { void test1() { assert_true("Compiler sanity", true); } public: Test() : suite("The Demo Suite") { // make the test1 method a test in this suite add("test1", testcase(this, "Demo test", &Test::test1)); // add this suite to the main suite suite::main().add("demo", this); } }; Test* theTest = new Test(); }
The add(id, testcase) method of suite adds a testcase to the suite. A testcase is a wrapper around a test* that ensures that the pointer is deleted when everybody has finished with it. The used constructor generates a test from a member function.
The
Test* theTest = new Test();is a global variable that ensures that one instance of the Test class is generated before main() is entered. It needs to be a new Test, since a suite assumes ownership of the pointer it receives.
In order to get an executable test we need a main method, and an instance of the test. The main method is actually part of the unit++ library, and the above code exactly adds an instance of the suite to the main suite, and hence the above code put in a file called demo_test.cc could actually be compikled with the following:
g++ -o test++ demo_test.cc -lunit++given that the headers and the library are installed in accessable places.
Once that is done, simply invoke the test `test++'.
The generated documentation is available here, so take a look at the API documentation or risk a download.