By: Charlie Calvert
Abstract: A short article describing the steps necessary to get Boost unit testing up and running under C++BuilderX
by Charlie Calvert
This article describes a simple technique for writing unit tests using the C++ Boost library. The Boost library is highly lauded set of tools for cross platform C++ development. It is likely that parts of the Boost library will be adopted in future C++ standards documents.
Boost ships with C++BuilderX, but it is not included with C++Builder6. If you are using CB6, or if you want to work with the most recent version of Boost, then you can read this article to learn how to proceed.
Boost has its own build technology, similar to make, called Boost Jam. You can download it from the boost libraries page on SourceForge. The description found here is based on my experience when running Windows XP, service pack 2.
You may need to build bjam.exe. I installed the boost jam files in the following directory:
I went into this directory, and found a file called build.bat. Assuming that bcc32.exe from BC6 or BCX is on your path, this will build jam. I had a little trouble, however, because I run 4DOS. There were commands in this batch file that did not work properly under 4DOS. So I loaded Microsoft's command tool:
Then I was able to run build.bat to build four executables, which were placed in a directory called bin.ntx86. I copied these executables from bin.ntx86 to a place on my system that is always on my path. If you want, you could copy them to c:\windows\system32, which will always be on your path. But I have another place on my system that is less crowded and which is always on my path.
One of the four executables created is called bjam.exe. This is the file you need to build the boost libraries.
To build the libraries, enter the directory where you installed boost (in my case: D:\src\srcc\boost_1_31_0), and type the following:
bjam "-sTOOLS=borland" install
This will build and install the boost libraries, which by default are copied to c:\boost.
When running the bjam command in the boost install directory, you will likely be notified of a problem with the Python libraries. Though I am big Python fan, nevertheless, for our purposes, it is okay to skip the Python part of the build process.
Once you have the libraries and include files installed, then you should start a new console project in C++BuilderX or CBuilder6. Select Project | Build Options Explorer and set the Toolset to run GNU Minimalist tools (GNU C++ Linker). You will have no main in this project. Instead, paste in the code shown in listing 1 right over whatever default code was included in your project.
Listing 1: A simple test case to run under the boost library.
using namespace boost::unit_test_framework;
BOOST_CHECK_EQUAL( 1, 3 );
test_suite* init_unit_test_suite(int argc, char* argv)
test_suite* test= BOOST_TEST_SUITE( "const_string test" );
test->add( BOOST_TEST_CASE( &foo ) );
The init_unit_test_suite function will be called automatically by Boost. In effect, it is the new entry point for your program.
Now you should use the Build Options Explorer (BOE) to add two items to your include files list two items. The first is the include section of the boost libary, and second is the root directory of your boost source installation. To do this, choose Project | Build Options Explorer from the C++BuilderX menu. Set the Toolset to Minimalist GNU for Windows. In the Tools section select GNU C++ Linker and set the lib path to c:\Boost\lib. Now select MinGW GNU C++ Compiler add the following two files to your include path:
The first of these entries is part of the include section created when I ran bjam.exe on the boost installation. The second is a directory into which I installed Boost right after downloading it. It makes sense to paste in the include directory, but why reference the install directory for boost? It turns out that the missing "main" section that you need, is in the lib directory of the boost installation directory, in a file called libs\test\src\unit_test_main.cpp. When you added the unit_test_framework.hpp include file to your project, you brought in that cpp file under the cover of night, as it were, and thereby added it to your project.
You should also add the boost lib directory to your project:
Now you can compile your project. An executable will be created with your project name and a ".exe" extension. Run this file to execute your tests. At this time, you have only one test, implemented above in the routine called foo().
This artcle describes the simple steps necessary to get up and running writing tests using the tools found in the Boost Library. You learned how to first build your own copy of the Boost build tool, called BJam. The second step described how to create a simple test case and run it.
If you are new to the subject of writing test suites, you might might want more information. One way to get started would be to follow some of the links on the junit site.
Try C++Builder XE4 free for 30 days
Click to download now!
More social media choices:
C++Builder on Google+
@RADtools on Twitter
Server Response from: ETNASC03