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

September 24th, 2010 by xrigher Leave a reply »

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.

Advertisement

14 comments

  1. Monica G says:

    Hi,
    Thanks for the straight-forward instructions.
    I’m getting an “Internal error” trying to create a testcase via the wizard:
    org.eclipse.core.runtime.CoreException: Internal error
    at net.sf.ecut.cppunit.internal.generator.TestFileGenerationStrategy.createFile(TestFileGenerationStrategy.java:163)
    at net.sf.ecut.cppunit.internal.generator.HeaderAndSourceTestFileGenerationStragey.generateTestCppFile(HeaderAndSourceTestFileGenerationStragey.java:109)
    at net.sf.ecut.cppunit.internal.generator.CppUnitTestCaseGenerator.generateTestCases(CppUnitTestCaseGenerator.java:139)
    at net.sf.ecut.EcutCore.createTestCases(EcutCore.java:131)…
    —–
    eclipse.buildId=
    java.version=1.6.0_18
    java.vendor=Sun Microsystems Inc.
    BootLoader constants: OS=linux, ARCH=x86, WS=gtk, NL=en_US
    Framework arguments: -product org.eclipse.epp.package.cpp.product
    Command-line arguments: -os linux -ws gtk -arch x86 -product org.eclipse.epp.package.cpp.product

    Could you help with a suggestion?
    Thank you

  2. xrigher says:

    Didn’t it create a file, but without any content? If so you may check whether the folder, in which you tried to create the test case, is a source folder. You can create a source folder like this: Right click on the project name -> New -> Source Folder.

  3. Monica G says:

    Thanks a lot, it worked!

    I have another question: Can you run a single test case at a time (select a testcase at random, in the test suite)? (for example, assuming the test case failed, and you want to re-run it)
    Appreciate your feedback.

  4. aqiruse says:

    Omg, xrigher…. thank you sooo much for that solution. I have been trying for hours to get this thing to work and putting the tests into a source folder solved the problem. Thanks.

  5. xrigher says:

    The way to run a single Test Case is to choose “run a single ECUT test” as shown in the image “ECUT Test Case Run Configuration” in the article.

  6. mia9 says:

    I managed to install ECUT and to Run the tests as advised, but I cannot see the results of running them !Are they written on the Console?

  7. Calvin says:

    Thanks for you post. By the way I came across Mockito Mock Forum and found it is exclusively for Unit Testing, Automated testing and Testing frameworks. This wil be of great help to people around here!!

  8. Frank says:

    This is either not working, or obsolete. There is no working repository at the sourceforge location for the project (at least, that’s the message in eclipse when trying to add it).

  9. Madhu says:

    Hi,
    Thanks for the detailed steps.

    Can we generate the test report file in XML or HTML format ?
    If yes, how ?

  10. mailoszyarr says:

    Hi,
    I did everything you write until the moment when I have to create New ECUT Test Case. When I choose “Source file under test” Browse button, window with “Source file selection – No entries available” appear. Project has been specified, like in your instruction. Until this problem Project was been able to compiling. Could you help me?

  11. ccortiz says:

    The same issue for me. I can not select “Source file under test”.

    My eclipse is Juno.
    Thanks

Leave a Reply