CppUnit

From Wikipedia, the free encyclopedia
Jump to navigation Jump to search
CppUnit
Stable release1.15.1 (LibreOffice version)[1] (13 April 2017; 9 years ago (2017-04-13)) [±]
Repository
  • {{URL|example.com|optional display text}}Lua error in Module:EditAtWikidata at line 29: attempt to index field 'wikibase' (a nil value).
Written inC++
Engine
    Lua error in Module:EditAtWikidata at line 29: attempt to index field 'wikibase' (a nil value).
    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

    [edit | edit source]

    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} {}
    
        [[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:

    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

    [edit | edit source]

    Lua error in mw.title.lua at line 392: bad argument #2 to 'title.new' (unrecognized namespace name 'Portal').

    Further reading

    [edit | edit source]
    • Lua error in Module:Citation/CS1/Configuration at line 2172: attempt to index field '?' (a nil value).

    References

    [edit | edit source]
    1. ^ Lua error in Module:Citation/CS1/Configuration at line 2172: attempt to index field '?' (a nil value).
    2. ^ Lua error in Module:Citation/CS1/Configuration at line 2172: attempt to index field '?' (a nil value).
    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. ^ Lua error in Module:Citation/CS1/Configuration at line 2172: attempt to index field '?' (a nil value).
    [edit | edit source]
    • Lua error in Module:Official_website at line 94: attempt to index field 'wikibase' (a nil value). (freedesktop.org version)