CppUnit

Unit testing framework module


title: "CppUnit" type: doc version: 1 created: 2026-02-28 author: "Wikipedia contributors" status: active scope: public tags: ["c++-libraries", "extreme-programming", "freedesktop.org", "unit-testing-frameworks"] description: "Unit testing framework module" topic_path: "general/c-libraries" source: "https://en.wikipedia.org/wiki/CppUnit" license: "CC BY-SA 4.0" wikipedia_page_id: 0 wikipedia_revision_id: 0

::summary Unit testing framework module ::

::data[format=table title="Infobox software"]

FieldValue
nameCppUnit
programming languageC++
genreUnit testing tool
licenseLGPL
website
::

| name = CppUnit | programming language = C++ | genre = Unit testing tool | license = LGPL | website = CppUnit is a unit testing framework module for the C++ programming language. It allows unit-testing of C sources as well as C++ with minimal source modification. It was started around 2000 by Michael Feathers as a C++ port of JUnit for Windows and ported to Unix by Jerome Lacoste. The library is released under the GNU Lesser General Public License. However, unlike JUnit, CppUnit does not rely on annotations (as annotations were not added to C++ until C++26), and rather creates tests with preprocessor macros.

The framework runs tests in suites. Test result output is sent to a filter, the most basic being a simple pass or fail count printed out, or more advanced filters allowing XML output compatible with continuous integration reporting systems.

The project has been forked several times. The freedesktop.org version at GitHub, maintained by Markus Mohrhard of the LibreOffice project (which uses CppUnit heavily), was actively maintained until 2020, and is used in Linux distributions such as Debian, Ubuntu, Gentoo and Arch. NOTOC

Some libraries, such as POCO C++ Libraries, provide their own APIs to consume the CppUnit library.

Example

Consider the following class Integer: ::code[lang=cpp] module;

export module org.wikipedia.examples.Integer;

export namespace org::wikipedia::examples {

class Integer { private: int x = 0; public: Integer(int x): x{x} {}

[[nodiscard]]
int add(int y) noexcept {
    x += y;
    return x;
}

[[nodiscard]]
int subtract(int y) noexcept {
    x -= y;
    return x;
}

}

} ::

It can be tested like so: ::code[lang=cpp] module;

export module org.wikipedia.examples.tests.IntegerTest;

import <cppunit/extensions/HelperMacros.h>; import org.wikipedia.examples.Integer;

using CppUnit::TestFixture; using org::wikipedia::examples::Integer;

export namespace org::wikipedia::examples::tests {

class IntegerTest: public TestFixture { private: CPPUNIT_TEST_SUITE(IntegerTest);

CPPUNIT_TEST(testAdd);
CPPUNIT_TEST(testSubtract);

CPPUNIT_TEST_SUITE_END();

Integer* i;

protected: void testAdd() { CPPUNIT_ASSERT_EQUAL(8, i->add(8)); // 5 + 3 = 8 }

void testSubtract() {
    CPPUNIT_ASSERT_EQUAL(3, i->subtract(2)); // 5 - 2 = 3
}

public: void setUp() override { i = new Integer(5); }

void tearDown() override {
    delete i;
}

} ::

Then, it can be tested in main(): ::code[lang=cpp] import <cppunit/ui/text/TestRunner.h> import org.wikipedia.examples.tests.IntegerTest;

using CppUnit::TextUi::TestRunner; using org::wikipedia::examples::tests::IntegerTest;

int main(int argc, char* argv[]) { TestRunner testRunner; runner.addTest(IntegerTest::suite()); runner.run(); } ::

References

References

  1. Mohrhard, Markus. "CppUnit Documentation". freedesktop.org.
  2. [[Jenkins (software). Jenkins]] [https://wiki.jenkins-ci.org/display/JENKINS/xUnit+Plugin plug-in] for CppUnit and other Unit Test tools
  3. [http://www.freedesktop.org/wiki/Software/cppunit freedesktop.org fork] presented as CppUnit v1.13
  4. [https://sourceforge.net/apps/trac/cppunit2/wiki/WikiStart fork] presented as CppUnit2; not modified since 2009
  5. Mohrhard, Markus. (22 October 2013). "cppunit framework". LibreOffice mailing list.

::callout[type=info title="Wikipedia Source"] This article was imported from Wikipedia and is available under the Creative Commons Attribution-ShareAlike 4.0 License. Content has been adapted to SurfDoc format. Original contributors can be found on the article history page. ::

c++-librariesextreme-programmingfreedesktop.orgunit-testing-frameworks