Archive for September, 2010

set up CppUnit Eclipse plugin (ECUT) for C++ on Ubuntu

September 24th, 2010

Here I will show how to use CppUnit to do Unit Testing for C++ within Eclipse with the plugin ECUT.

NB: the OS here is Ubuntu 10.4. As for other OS, you have to figure out how to install CppUnit by yourself, and change the build path accordingly in configuration.

* First, install CppUnit. On Ubuntu, we can install from the repository:

sudo apt-get install libcppunit-1.12-1 libcppunit-dev

* Get the ECUT plugin from http://sourceforge.net/projects/ecut/ and install it in Eclipse with the software installer in the Help menu.

* Configur the connector in Eclipse in Window -> Preferences -> C/C++ -> ECUT -> Configure (Add “Library location: /usr/lib/libcppunit.a” and “Include location: /usr/include/cppunit”).

Now the configuration is done. Now let’s see a sample project. I’ll use the official ECUT Demo: http://sourceforge.net/projects/ecut/files/ECUT%20Documentation/R1.0-M3/EcutDemo_M3.zip/download.

* Create a empty C++ project in Eclipse called: EcutTest.

* Create 2 source folders within the project: src & test. src will be place for application application source files, while test is for test cases.

* Copy the 3 source files from EcutDemo_M3/src into the project src folder.

* Build a Debug version, and run it. This is just to test whether the project works. You will find the output: “result: 13″ if it build the project right.

Now it’s the time to connect CppTest with the project.

* Right click on the project name “EcutTest”, and choose ECUT -> Apply Connector. Select the one and only Connect “CppUnit Connector”, and “Create build configurations” as well.
– Note: ignore the warning in the image below, because I have applied the connect before.
– If you need a configuration: Library location is: “/usr/lib/libcppunit.a” and Include location is: “/usr/include/cppunit”.

* Create a Test case. Right click on module1.c -> New -> ECUT Test Case. Note: ignore the error message in the image below, because I have already created the Test Case.

* edit the automatically created source file test/module1Test.cpp a little bit to correct the Include Path.

change the source:

#include “module1.c”

into

#include “../src/module1.c”

* Create a run configuration to run the Test Case: Run -> Run Configurations -> Right click on ECUT -> New:

ECUT Test Case Run Configuration

* Run the test by clicking on Run as shown in the above image. Both of the 2 test cases should fail, because in the test method body, they return false directly.

* OK, now everything is done. It’s the time to do some real work. Change the function module1Test::testadd as bleow:

void module1Test::testadd() {
CPPUNIT_ASSERT(24 == add(12, 12));
}

And run the Test again, you will find 1 case is passed, and 1 case fails.

Details of assertions can be found here: http://cppunit.sourceforge.net/doc/1.11.6/group___assertions.html.