CppUnit Starting Out


I've decided to try and keep a bit of a blog by writing it in Emacs on my laptop and then copying the resulting text over to the actual online Blog.

This evening I've finished going through the first part of the CppUnit simple tutorial, but I still need to get a better idea of what is entailed in using it, so I'll have to try working through the Cookbook even though it starts with other Test versions than the one in the Tutorial.

Maybe I should work through the cookbook and then write my own tutorial, using the development of my canvas class as a result then use the resulting tutorial as part of my write up for the final project. Plus I need to get CppUnit working on Windows so that I can do all of my testing there.

The basic tutorial covers setting up a Test Suite and demonstrates the use of CPPUNIT_TEST and CPPUNIT_TEST_EXCEPTION. But in the actual test section it only uses CPPUNIT_ASSERT.

These seem to work as follows, we have files ClassName.h,
ClassName.cpp, ClassNameTest.h, ClassNameTest.cpp and ClassNameApp.cpp (plus I guess any user defined classes we need for the tests).

In the "Private" section of the class in the ClassNameTest.h file we put:

[code]
    CPPUNIT_TEST_SUITE( ClassNameTest );
    CPPUNIT_TEST( testFunctionName1 );
    CPPUNIT_TEST( testFunctionName2 );
    CPPUNIT_TEST_EXCEPTION( testFunctionName3, someException );
    CPPUNIT_TEST_SUITE_END();
[/code]

someException is an exception which inherits from runtime_error defined in . Then in the "Public" section of the class we declare:

[code]
    void testFunctionname1();
    void testFunctionname2();
[/code]

We then go to the ClassNameTest.cpp file and define each of these two functions, eg:

[code]
    void ClassNameTest::testFunctionName1()
    {
        Set up any variables
        Do whatever needs doing
        CPPUNIT_ASSERT( Some boolean test )
    }

    void ClassNameTest::testFunctionName2()
    {
        Set up any variables
        Do whatever needs doing
        CPPUNIT_ASSERT( Some boolean test )
    }

    void ClassNameTest::testFunctionNam31()
    {
        Set up any variables
        Do whatever needs doing which should trigger the
        exception defined above otherwise the test suite will fail due
        to an expected exception not being thrown.
    }
[/code]

We either define the exception ourselves or use a runtime_error which is predefined.

Setting up the project requires a few steps.

Create a configure.in file containing:

[code]
    dnl Process this file with autoconf to produce a configure script
    AC_INIT(Makefile.am)
    AM_INIT_AUTOMAKE(*classname?*,0.1)
    AM_PATH_CPPUNIT(1.9.6)
    AC_PROG_CXX
    AC_PROG_CC
    AC_PROG_INSTALL
    AC_OUTPUT(Makefile)
[/code]

and a Makefile.am containing:

[code]
    # Rules for the test code (use 'make check' to execute)
    TESTS = ExecutableName
    check_PROGRAMS = $(TESTS)
    ExecutableName_SOURCES = List of all of the source files to be compiled in
    ExecutableName_CXXFLAGS = $(CPPUNIT_CFLAGS)
    ExecutableName_LDFLAGS = $(CPPUNIT_LIBS) -ldl
[/code]

We need a ClassNameApp.cpp file containing:

[code]
    #include
    #include
    #include

    int main(int argc, char* argv[])
    {
        // Get the top level suite from the registry
        CppUnit::Test *suite = CppUnit::TestFactoryRegistry::getRegistry().makeTest();
        CppUnit::TextUi::TestRunner runner;
        runner.addTest( suite );

        runner.setOutputter(new CppUnit::CompilerOutputter( &runner.result(), std::cerr) );
        bool wasSuccessful = runner.run();

        return wasSuccessful ? 0 : 1;
    }
[/code]

We need to do the following at the command line too:

[code]
    $ touch Names of any source files we haven't created yet.
    $ aclocal -I /usr/local/share/aclocal
    $ autoconf
    $ automake -a
    $ touch NEWS README AUTHORS ChangeLog (this stops automake complaining)
    $ automake
    $ ./configure
    $ make check
[/code]

make check builds and runs the test application for us, and this is what we have to run each time we wish to run the tests. Most of the above is based on the Simple Tutorial for CPPUNIT available at:

http//cppunit.sourceforge.net/doc/1.11.6/money_example.html

Bed now.