Appendix A. Simple C++ Unit Test Framework

This appendix contains the C++ version of the simple unit test framework example from Chapter 2. The software architecture of the C++ version is identical to that of the Java version. The only variations are those dictated by the differences in language syntax.

The full description of the example is found in Chapter 2. This appendix simply describes how to build and run the C++ version. It assumes that you are using the GNU g++ compiler. Most other C++ compilers should work as well, but the compilation commands may vary from what is shown here.

Example 1: Create a Book

The first example creates the class Book. En route, the unit test framework is built and the first unit test written.

Step 0: Set Up the Unit Test Framework

The framework initially is built on a single class, UnitTest , shown in Figure A-1. The source code for UnitTest includes a header file, UnitTest.h, and an implementation file, UnitTest.cpp, as shown in Example A-1.

The class UnitTest
Figure A-1. The class UnitTest
Example A-1. The class UnitTest
UnitTest.h
#define UT_ASSERT( condition ) \
        assertTrue(condition,__FILE__,__LINE_  _,#condition)

class UnitTest {

 public:

   virtual ~UnitTest( ) {}
   virtual void runTest( ) = 0;

 protected:

   void assertTrue(bool condition, const char *file, 
                  int line, const char *msg);
   static int num_test_success; }; UnitTest.cpp #include <stdio.h> #include <stdlib.h> #include ...

Get Unit Test Frameworks now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.