Unit Testing C++ Programs using CppUnit in Eclipse IDE on Windows
CppUnit is the most popular unit testing framework available for the C++ language today. So integrating it with one of the most popular IDEs available for the C++ language should be very appealing indeed. If you have not yet configured CppUnit library to work with the Eclipse IDE yet, here is a step-by-step procedure to do the same. By the end of it you will be able to create C++ classes and functions, write unit tests for them, run them and see the results, all from with-in the Eclipse IDE. This is for the Windows users; can be adapted for GNU/Linux users but there are shorter procedures for them.
You can either use Eclipse Europa for C++ as the IDE or other IDEs based on it like EasyEclipse, Wascana etc. And of course MinGW or someother C++ toolchain must already be installed and configured with your Eclipse IDE; if not, read the MinGW and EasyEclipse configuration post for more information; it’s for older version of the Eclipse and configuration is much simpler for Eclipse Europa.
Downloading and Preparing the CppUnit Package
You can skip this section and the next one if you already have CppUnit library built/installed on your system and you know where its include and library files reside. Jump over to the last section in that case.

Extract it to a directory of your choice. I will assume that it is extracted to ‘c:\cpp\cppunit’. This directory should look like this:

(If the downloaded archive file gets extracted to a directory named cppunit-1.12.0 then just rename it to cppunit.) What we downloaded in the above step is the CppUnit source code. We need to build this source code to create a library file which we can then use in the C++ programs. CppUnit can be built from sources either from the command line or from the Eclipse IDE itself. To build it from the Eclipse IDE, we still need to generate at least one file from the MSYS command line. Go to your MSYS installation directory and click on msys.bat file in it. In the MSYS console window, change to the CppUnit directory(c:\cpp\cppunit in our example) and run the ./configure command.

This will create the cppunit/config-auto.h file that we need. Close the MSYS command window.
Building CppUnit Package from the Source Code in Eclipse IDE

You can also choose to select “Shared Library” if you want to build CppUnit as a shared library. Similary, select “Cygwin GCC” if that is the toolchain you prefer.
Click Finish when done.
Now we need to import the entire CppUnit source code into this project. Right-click on the newly created project(CppUnitBuild) and select the “Import” menu item. In the “Import” dialog box, expand the “General” tab, select “File System” and click the Next button. Click “Browse” and browse to the c:\cppunit\src\cppunit directory and click the “Select All” button and click Finish.
This will select and import all the source(.cpp) and header(.h) files of the CppUnit package into the CppUnitBuild project.
Next step is to add the CppUnit include directory to the compiler’s include path. Right-click on the project(CppUnitBuild), select “Properties” and go to C/C++ Build -> Settings node. In the right pane, go to Tool Settings -> GCC C++ Compiler -> Directories. Click the “Add” button located near the “Include Paths (-I)” text box(see the screenshot below), click “File System…” button and browse to c:\cpp\cppunit\include directory.
At the end of the build process, a static CppUnit library(libCppUnitBuild.a) should be built in the Debug subdirectory of the project directory in your Eclipse workspace(If you build using the Release configuration, the library will be generated in Release subdirectory instead). If you had opted for a shared library earlier, then [1] suggests that you define CPPUNIT_DLL_BUILD variable by going to Project -> Properties -> C/C++ Build -> Settings -> GCC C++ Compiler -> Preprocessor -> Define Symbols (-D). You will see a file named libCppUnitBuild.dll generated in this case.
Let us test it with a sample C++ project.
Unit Testing C++ Programs using CppUnit in Eclipse IDE
Right-click on the project name, select Properties, select C/C++ Build -> Settings node, select the Tools Settings tab, select MinGW C++ Linker -> Libraries node, click the Add button near “Libraries (-l)” text box and enter CppUnitBuild in the popped-up dialog box. Similary, click the Add button near “Library search path (-L)” text box, click “File System…” button and browse to the path where the CppUnit library file was generated([your-eclipse-workspace]\CppUnitBuild\Debug\; The generated file might be in the Release sub-directory if you had chosen a Release build configuration to build the CppUnitBuild project earlier).

If you had built CppUnit as a shared library earlier, then you need to define CPPUNIT_DLL at this stage; see Step 6 for more details.
Right-click on the project name and select “Build Project” to build the project and right-click on the project name and select Run As -> Local C/C++ Application, select ‘gdb debugger’ and click OK to run the project. You should see two dots in the output to represent that the two unit tests present in the sample files ran successfully. You can now create your C++ classes, write unit tests for them and then run the tests. Need help in getting started with writing unit tests using CppUnit? Here’s a CppUnit cookbook for you and you can also download the CppUnit documentation.Next up is how to integrate CppUnit Qt GUI test runner in Eclipse IDE. Then I will write about how to integrate CxxTest unit testing framework with the Eclipse IDE. Doxygen integration may follow, so hang tight ;)
[1] http://cppunit.sourceforge.net/cppunit-wiki/CppUnitWithEclipse – the sample zip file and other help for creating this tutorial were taken from this wiki page.
13 comments
Thanks for a great article. This article is very similar to the one one cppunit website but this has screenshots which were quite helpful. I just wish that someone get to the task of building cppunit integration for Eclipse. Visual Studio 2008 ships with its own Unit test feature built in and fully integrated.
this is a horribly useless article, and has little to nothing to do with eclipse in reality. Creating a seperate project to do the unit testing in defeats the whole point of it.. This article only would have been worthwhile if it covered the usage / setup that integrated c/cpp unit into eclipse. otherwise theres nothing special that wouldn’t be better described generically for the command line…
Thanks for the great article.
I don’t have MSYS and I don’t find it on the net (maybe there is a more advanced replacement?). I don’t run Vista but Windows 2000 (my other system, at home, is ubuntu Hardy :-> ). So I could not generate config-auto.h file and this probably is why the build step fails with 100 errors.
It must be another way of getting config-auto.h…?
Thanks so much.
JK
@joe I don’t understand what you are saying but note that you need to complete instructions in “Building CppUnit Package from the Source Code in Eclipse IDE” section only once, not for every C++ project you create in Eclipse.
@Joshua http://www.mingw.org/wiki/msys :)
I tried with the same. CppUnitBuild library is made successfully. But when I tried to link this library with one example itäs giving error.
Like I have taken one example present in CPPUnitTese source.Have kept in some other path. And done the same as mentioned, like set include and library path as described. When I tried to build, it gives error , like present in CPPUnitBuild source code as shown below.
C:/CPP/cppunit/include/cppunit/portability/Stream.h: undefined reference to `(anonymous namespace)::allocator::~allocator()’ C:/CPP/cppunit/include/cppunit/portability/Stream.h: undefined reference to `(anonymous namespace)::allocator::allocator()’
C:/Documents and Settings/satapal/workspace/CppUnitBuild/Debug/../Asserter.cpp undefined reference to `(anonymous namespace)::allocator::~allocator()’
C:/Documents and Settings/satapal/workspace/CppUnitBuild/Debug/../Asserter.cpp undefined reference to `(anonymous namespace)::allocator::allocator()’
Pleaze help me regarding this.
JK – I was having the same problem!! I found that MSYS is part of the minGW suite, but is a separate install – http://www.mingw.org/wiki/MSYS just run the MSYS-1.0.10.exe and you will have it on your system – then follow the instructions in step 2. Very Good “How To”, much better than the one on the CPPUnit site!!!
hey thanks for the info.. incredibly it seems the re is no other better source on the net to start doing cppunit with eclipse.
but 2 observations:
- link to the example zip file is broken
- is this is the only way to run cppunit in eclipse, then netbeans is clear winner.
in netbeans the test files can be generated automatically and you have just to deploy your testcases,
more on that, there is a graphical output by running tests.
I feel really disappointed with eclipse now, as i don’t really want to use netbeans just for cppunit but it looks there is no other way…
I tried compile but i have this problem:
g++ -IH:\cpp\cppunit\include -O0 -g3 -Wall -c -fmessage-length=0 -oTestPath.o ..\TestPath.cpp
In file included from ..\TestPath.cpp:1:
H:/cpp/cppunit/include/cppunit/Portability.h:18:37: cppunit/config-auto.h: No such file or directory
Build error occurred, build is stopped
Time consumed: 437 ms.
can you help me plz?
regards
Nestor:
I tried compile but i have this problem:
g++ -IH:\cpp\cppunit\include -O0 -g3 -Wall -c -fmessage-length=0 -oTestPath.o ..\TestPath.cpp
In file included from ..\TestPath.cpp:1:
H:/cpp/cppunit/include/cppunit/Portability.h:18:37: cppunit/config-auto.h: No such file or directory
Build error occurred, build is stopped
Time consumed: 437 ms.
can you help me plz?
regards
It is help me with same error
http://sourceforge.net/apps/mediawiki/cppunit/index.php?title=CppUnitWithEclipse
Thanks a lot, great article. Only question is how would one create a makefile project using
cppunit instead of a executable?