1. Home
  2. Tutorials
  3. C/C++
  4. CppUnit
Yolinux.com Tutorial

CppUnit: C++ unit test framework

C++ class unit test framework examples and integration techniques for Jenkins.

CppUnit Description:

The CppUnit test framework is for unit test of C++ class functions. It relies on the hierarchy of a test suite comprising of unit test cases which test class functions. The test begins with setUp() followed by the test and ending with tearDown().

Each unit test employs the use of C++ assert() to test the function results.

C++ assert prototype: void assert (int expression);

If this expression evaluates to 0, this causes an assertion failure that terminates the program.

The assert function will abort the application if false.

Turn assert off: #define NDEBUG
at the beginning of its code, before the inclusion of assert.h

Also see the YoLinux Tutorial on Google C++ unit test.

Man page:
  • assert - abort the program if assertion is false


Available as Linux RPMs:
  • cppunit-1.12.0-3.el5.rf
  • cppunit-devel-1.12.0-3.el5.rf
Docs installed to: /usr/share/doc/doc/cppunit-devel-1.12.0/index.html

Debian/Ubuntu: apt-get install libcppunit-doc libcppunit-dev

CppUnit Home Page

CppUnit Example:

C++ class to unit test:

File: CBasicMath.hpp
#ifndef BASIC_MATH_HPP__
#define BASIC_MATH_HPP__

class CBasicMath
   int Addition(int x, int y);
   int Multiply(int x, int y);


File: CBasicMath.cpp
#include "CBasicMath.hpp"

int CBasicMath::Addition(int x, int y)
   return (x + y);

int CBasicMath::Multiply(int x, int y)
   return (x * y);

CppUnit test driver program:

File: TestBasicMath.cpp
#include <iostream>
#include <string>
#include <list>
#include <cppunit/TestCase.h>
#include <cppunit/TestFixture.h>
#include <cppunit/ui/text/TextTestRunner.h>
#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/TestResult.h>
#include <cppunit/TestResultCollector.h>
#include <cppunit/TestRunner.h>
#include <cppunit/BriefTestProgressListener.h>
#include <cppunit/CompilerOutputter.h>
#include <cppunit/XmlOutputter.h>
#include <netinet/in.h>

#include "CBasicMath.hpp"

using namespace CppUnit;
using namespace std;


class TestBasicMath : public CppUnit::TestFixture

    void setUp(void);
    void tearDown(void);

    void testAddition(void);
    void testMultiply(void);


    CBasicMath *mTestObj;


    CPPUNIT_ASSERT(5 == mTestObj->Addition(2,3));

    CPPUNIT_ASSERT(6 == mTestObj->Multiply(2,3));

void TestBasicMath::setUp(void)
    mTestObj = new CBasicMath();

void TestBasicMath::tearDown(void)
    delete mTestObj;



int main(int argc, char* argv[])
    // informs test-listener about testresults
    CPPUNIT_NS::TestResult testresult;

    // register listener for collecting the test-results
    CPPUNIT_NS::TestResultCollector collectedresults;
    testresult.addListener (&collectedresults);

    // register listener for per-test progress output
    CPPUNIT_NS::BriefTestProgressListener progress;
    testresult.addListener (&progress);

    // insert test-suite at test-runner by registry
    CPPUNIT_NS::TestRunner testrunner;
    testrunner.addTest (CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest ());

    // output results in compiler-format
    CPPUNIT_NS::CompilerOutputter compileroutputter(&collectedresults, std::cerr);
    compileroutputter.write ();

    // Output XML for Jenkins CPPunit plugin
    ofstream xmlFileOut("cppTestBasicMathResults.xml");
    XmlOutputter xmlOut(&collectedresults, xmlFileOut);

    // return 0 if tests were successful
    return collectedresults.wasSuccessful() ? 0 : 1;
Note the use of XmlOutputter for XML output of results

Compile: g++ -o testBasicMath CBasicMath.cpp TestBasicMath.cpp -lcppunit

File: Makefile
CXX = g++
SRCM= ../CBasicMath.cpp
OBJM = $(SRCM:.cpp=.o)
LINKFLAGS= -lcppunit

testbasicmath: TestBasicMath.cpp $(OBJM)
	$(CXX) $(CXXFLAGS) -o $@ TestBasicMath.cpp $(OBJM) $(LINKFLAGS) $(LINKFLAGSLOG4) $(LIBLOG)

# Default compile

	$(CXX) $(CXXFLAGS) -c $< -o $@

Run test: ./testBasicMath

TestBasicMath::testAddition : OK
TestBasicMath::testMultiply : OK

This generates the XML file: cppTestBasicMathResults.xml
<?xml version="1.0" encoding='ISO-8859-1' standalone='yes' ?>
    <Test id="1">
    <Test id="2">

Integration with Jenkins:

Note that I had no luck with the CppUnit plugin for Jenkins and integration required explicit translation of XML unit test results to JUnit format for use by Jenkins.

Ant script to execute Makefile:

File: build.xml
  <property name="make.cmd" value="/usr/bin/make"/>


  <target name="testBasicMathCompile"
        description="cppunit test compile" >
    <exec dir="${build.native}/Test" executable="${make.cmd}" failonerror="true">

    <arg value="testbasicmath"/>

  <target name="testbasicmath" 
        description="cppunit test run" >
    <exec dir="${build.native}/Test" executable="./testbasicmath" failonerror="false">


The XML results of CppUnit require translation to JUnit style XML for interpretation by Jenkins.

Requires XML conversion from CPP Unit to JUnit using XSLT: cpp2junit.xslt

File: cpp2junit.xslt
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>
    <xsl:template match="/">
            <xsl:attribute name="errors"><xsl:value-of select="TestRun/Statistics/Errors"/></xsl:attribute>
            <xsl:attribute name="failures">
                <xsl:value-of select="TestRun/Statistics/Failures"/>
            <xsl:attribute name="tests">
                <xsl:value-of select="TestRun/Statistics/Tests"/>
            <xsl:attribute name="name">from cppunit</xsl:attribute>
    <xsl:template match="/TestRun/SuccessfulTests/Test">
            <xsl:attribute name="classname" ><xsl:value-of select="substring-before(Name, '::')"/></xsl:attribute>
            <xsl:attribute name="name"><xsl:value-of select="substring-after(Name, '::')"/></xsl:attribute>
    <xsl:template match="/TestRun/FailedTests/FailedTest">
            <xsl:attribute name="classname" ><xsl:value-of select="substring-before(Name, '::')"/></xsl:attribute>
            <xsl:attribute name="name"><xsl:value-of select="substring-after(Name, '::')"/></xsl:attribute>
                <xsl:attribute name="message">
                    <xsl:value-of select=" normalize-space(Message)"/>
                <xsl:attribute name="type">
                    <xsl:value-of select="FailureType"/>
                <xsl:value-of select="Message"/>
                File:<xsl:value-of select="Location/File"/>
                Line:<xsl:value-of select="Location/Line"/>
    <xsl:template match="text()|@*"/>

Use the Gnome program "xsltproc" to apply XSLT to the CppUnit output XML.

Command: xsltproc -o junitTestBasicMathResults.xml cpp2junit.xslt cppTestBasicMathResults.xml

Translated results: junitTestBasicMathResults.xml
<?xml version="1.0"?>
<testsuite errors="0" failures="0" tests="2" name="from cppunit">
  <testcase classname="TestBasicMath" name="testAddition"/>
  <testcase classname="TestBasicMath" name="testMultiply"/>

Ant script for results translation:

File: build.xml
  <property name="xsltproc.cmd" value="/usr/bin/xsltproc"/>


  <target name="testdbCpp2junit" 
        description="Convert cppunit test results to junit test results" >
    <echo>Tests run from  directory: ${build.native}/Test</echo>
    <exec dir="${build.native}/Test" executable="${xsltproc.cmd}" failonerror="true">
    <arg value="-o"/>

    <arg value="test/data/junitTestBasicMathResults.xml"/>
    <arg value="cpp2junit.xslt"/>
    <arg value="cppTestBasicMathResults.xml"/>

xsltproc man page - command line XSLT processor

Jenkins configuration to display JUnit results:
Jenkins/Hudson JUnit configuration

For more, see the YoLinux.com Jenkins tutorial


C++ How to Program
by Harvey M. Deitel, Paul J. Deitel
ISBN #0131857576, Prentice Hall

Fifth edition. The first edition of this book (and Professor Sheely at UTA) taught me to program C++. It is complete and covers all the nuances of the C++ language. It also has good code examples. Good for both learning and reference.

Exceptional C++: 47 Engineering Puzzles, Programming Problems and Solutions
by Herb Sutter
ISBN #0201615622, Addison-Wesley Professional

Advanced C++ features and STL.

More Exceptional C++
by Herb Sutter
ISBN #020170434X, Addison-Wesley Professional

Effective C++: 50 Specific Ways to Improve Your Programs and Design (2nd Edition)
by Scott Meyers
ISBN #0201924889, Addison-Wesley Professional

More Effective C++: 35 New Ways to improve your Programs and Designs
by Scott Meyers
ISBN #020163371X, Addison-Wesley Professional