CppUnit Tests with C++BuilderX

By: Charlie Calvert

Abstract: A simple example of how to install CppUnit and create unit tests using C++BuilderX.

by Charlie Calvert

This article describes how to build and use the unit testing framework CppUnit with C++BuilderX. A very similar process, not detailed in this article, will work in C++Builder6.

The opening sections of this article will describe how to build the CppUnit binaries. The last section of this article will describe how to create a test and run it in C++BuilderX.

The first step is to download the CppUnit source files from http://sourceforge.net/projects/cppunit. Now decompress them, and follow the instructions outlined below to build the CppUnit binaries.

I installed the CppUnit source into D:\src\srcc\cppunit-1.10.2\, henceforth called $CUROOT. Let me be as clear as possible. In the rest of this article, if you see a path such as $CUROOT\bcc, then you should translate it into something like D:\src\srcc\cppunit-1.10.2\bcc, though of course the first part of the path will reflect where you installed the CppUnit source files that you downloaded from SourceForge.

Build C++Unit in C++BuilderX, the Short Version

Unzip the contents of the $CUROOT\contrib\bc5\bcc-makefile.zip archive into $CUROOT\bcc.

First create a small console app and build the simple executable called mksrclist.exe. Base your console app on $CUROOT\bcc\mksrclist.cpp.

Select File | New | Project | Project from Makefile in the C++BuilderX IDE. Base your project on $CUROOT\bcc\makefile. Build the project and you are done.

Note: These make files were aimed at BC5, so there is one change you need to apply after you build mksrclist.exe. The file $CUROOT\bcc\cppunit.mak twice uses the obsolete -N option. Remove both instances.

Build CppUnit in C++BuilderX, the Long Version

Here is the longer version of the CppUnit build process.

  • There is a special zip file in the $CUROOT\contrib\bc5\ directory of your decompressed version of CppUnit that contains make files for use with Borland's tools. Being careful to preserve the directories, unzip the contents of that file into the root of your CppUnit directory, $CUROOT. The decompressed zip file should create a directory called bcc that contains several files, including one named $CUROOT\bcc\makefile.
  • If you are working in C++BuilderX rather than CBuilder6, you will need to first create a file called mksrclist.exe. To do this, choose File | New | Project | New Console. In the third step of the New Console wizard, add mksrclist.cpp to the Imported files window. Close the wizard, and optionally set the output directory for the executable to be $CUROOT\bcc, and build. When you are done, you should have a file called $CUROOT\bcc\mksrclist.exe. (If you did not set the output directory, then move mksrclist.exe file from $CUROOT\bcc\windows\Debug_Build to $CUROOT\bcc.)
  • Choose File | New Project | Project from MakeFile from the C++BuilderX menu system. Set the project name to CppUnitX and set the Makefile field in the wizard to $CUROOT\bcc\makefile.
  • The cppunit-mak file will have two instances of the -N switch, which is now obsolete. Remove these two characters from line 29, which begins CXXFLAGS_DEBUG_static, and line 33, which begins CXXFLAGS_DEBUG_dll. For instance, change -v -N -x -xp to -v -x -xp
  • Build the project. You should end up with a bunch of lib and dll files in $CUROOT\bcc. These are the files you set out to create.

If all else fails, you can download my debug version of these files.

Create a Simple Test Program

Start a new project and add cppunit_bc5_dll.lib to your project. This file was created in the first half of this article. Also place the cppunit_bc5_dll.dll file in the same directory with your project's binary output. For instance, if you are using C++BuilderX, it might go in a directory with a name like d:\src\MyProject\windows\Debug_Build\.

Choose Project | Build Options Explorer from the C++BuilderX menu. In the BOE, select the BCC32 tool in the top half of the dialog. In the Paths and defines section in the lower half of the dialog add the path $CUROOT\include to your include search path.

The source code for the project is shown below in listings 1 through 3. You can download a version of this project from my web site. In the downloaded version you may need to fix at least the path to the include files by bringing up the Build Options Explorer from the C++BuilderX Project menu.

Listing 1: The header for the unit test should be saved to disk as SimpleTest.h

#ifndef CPP_UNIT_SIMPLE_TEST_H
#define CPP_UNIT_SIMPLE_TEST_H

#include <cppunit/extensions/HelperMacros.h>

class SimpleTest : public CPPUNIT_NS::TestFixture
{
	CPPUNIT_TEST_SUITE( SimpleTest );
	CPPUNIT_TEST( runTest );
	CPPUNIT_TEST_SUITE_END();
public:
	void runTest();
};

#endif

Listing 2: The cpp source file for the unit test should be saved to disk as SimpleTest.cpp.

#include <iostream>

#include "SimpleTest.h"


// CPPUNIT_TEST_SUITE_REGISTRATION( SimpleTest );

	void SimpleTest::runTest()
	{
		//std::cout << "RunTest" << std::endl;
		CPPUNIT_ASSERT(1 != 1);
		CPPUNIT_ASSERT(1 != 2);
	}

Listing 3: The program entry point for a CppUnit test.

#include <cppunit/TextTestRunner.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/ui/text/TestRunner.h>
#include <cppunit/TestResult.h>
#include "SimpleTest.h"


#ifdef __BORLANDC__
	#pragma argsused
#endif
int main( int argc, char * argv[] )
{
	CppUnit::TextUi::TestRunner runner;
	runner.addTest( SimpleTest::suite() );
	runner.run();

	return 0;
}

There are many different ways to structure a CppUnit test. The code shown in listings 1-3 is simply one of the quickest and easiest ways to get up and running. Read the CppUnit documentation for more details.

Summary

In this paper you learned how to create a simple unit test in C++BuilderX, using the Borland tools. The majority of the article focused on creating the libraries and dlls that form the basis of CppUnit. Once you had those binaries in hand, the last part of the article showed how to write a simple CppUnit test.

If you are new to this subject, you might might want more information. One way to get started would be to follow some of the links on the junit site.


Server Response from: ETNASC03