| CppUnit | |
|---|---|
| Stable release | 1.15.1 (LibreOffice version)[1] (13 April 2017; 8 years ago (2017-04-13)) [±] |
| Written in | C++ |
| Type | Unit testing tool |
| License | LGPL |
| Website | freedesktop |
| Repository | |
CppUnit is aunit testing framework module for theC++ programming language. It allows unit-testing ofC sources as well as C++ with minimal source modification. It was started around 2000 by Michael Feathers as a C++port ofJUnit for Windows and ported to Unix by Jerome Lacoste.[2] The library is released under theGNU Lesser General Public License. However, unlike JUnit, CppUnit does not rely onannotations (as annotations were not added to C++ untilC++26), and rather creates tests withpreprocessor 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 withcontinuous integration reporting systems.[3]
The project has beenforked several times.[4][5] Thefreedesktop.org version atGitHub, maintained by Markus Mohrhard of theLibreOffice project (which uses CppUnit heavily), was actively maintained until 2020, and is used inLinux distributions such asDebian,Ubuntu,Gentoo andArch.[6]
Some libraries, such asPOCO C++ Libraries, provide their own APIs to consume the CppUnit library.
Consider the following classInteger:
module;exportmoduleorg.wikipedia.examples.Integer;exportnamespaceorg::wikipedia::examples{classInteger{private:intx=0;public:Integer(intx):x{x}{}[[nodiscard]]intadd(inty)noexcept{x+=y;returnx;}[[nodiscard]]intsubtract(inty)noexcept{x-=y;returnx;}}}
It can be tested like so:
module;exportmoduleorg.wikipedia.examples.tests.IntegerTest;import<cppunit/extensions/HelperMacros.h>;importorg.wikipedia.examples.Integer;usingCppUnit::TestFixture;usingorg::wikipedia::examples::Integer;exportnamespaceorg::wikipedia::examples::tests{classIntegerTest:publicTestFixture{private:CPPUNIT_TEST_SUITE(IntegerTest);CPPUNIT_TEST(testAdd);CPPUNIT_TEST(testSubtract);CPPUNIT_TEST_SUITE_END();Integer*i;protected:voidtestAdd(){CPPUNIT_ASSERT_EQUAL(8,i->add(8));// 5 + 3 = 8}voidtestSubtract(){CPPUNIT_ASSERT_EQUAL(3,i->subtract(2));// 5 - 2 = 3}public:voidsetUp()override{i=newInteger(5);}voidtearDown()override{deletei;}}
Then, it can be tested inmain():
import<cppunit/ui/text/TestRunner.h>importorg.wikipedia.examples.tests.IntegerTest;usingCppUnit::TextUi::TestRunner;usingorg::wikipedia::examples::tests::IntegerTest;intmain(intargc,char*argv[]){TestRunnertestRunner;runner.addTest(IntegerTest::suite());runner.run();}