Tu banner alternativo

CppUnit

In this article, we will thoroughly explore CppUnit, a topic that has captured the attention and interest of many people in recent times. CppUnit is a topic that has generated debate and discussion in different areas, and it is important to understand its relevance and implications in today's society. Throughout this article, we will examine different perspectives on CppUnit, addressing its most relevant aspects and analyzing its impact today. In addition, we will delve into its history, its evolution and its influence on various aspects of daily life. We hope that this article will provide a complete and enriching overview of CppUnit, inviting readers to deepen their knowledge and understanding of this very relevant topic.

Tu banner alternativo
CppUnit
Stable release1.15.1 (LibreOffice version)[1] (13 April 2017 (2017-04-13)) [±]
Repository
Written inC++
TypeUnit testing tool
LicenseLGPL
Websitefreedesktop.org/wiki/Software/cppunit

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.[2] 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.[3]

The project has been forked several times.[4][5] 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.[6]


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

Example

Consider the following class Integer:

module;

export module org.wikipedia.examples.Integer;

export namespace org::wikipedia::examples {

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

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

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

}

It can be tested like so:

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():

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();
}

See also

Further reading

  • Madden, Blake (6 April 2006). "1.7: Using CPPUnit to implement unit testing". In Dickheiser, Mike (ed.). Game Programming Gems 6. Charles River Media. ISBN 1-58450-450-1.

References

  1. ^ "Commit description".
  2. ^ Mohrhard, Markus. "CppUnit Documentation". freedesktop.org.
  3. ^ Jenkins plug-in for CppUnit and other Unit Test tools
  4. ^ freedesktop.org fork presented as CppUnit v1.13
  5. ^ fork presented as CppUnit2; not modified since 2009
  6. ^ Mohrhard, Markus (22 October 2013). "cppunit framework". LibreOffice mailing list. Retrieved 20 March 2014.