An Hello, world program for CppUnit

This follows a discussion on the Test Driven Developement group. The first program one usually write when beginning with a new programming language is the infamous "Hello, world!". Unfortunatly this is not easy to program Test-First. The idea is not to test what is effectively printed out in the console, but to make sure the framework is running decently.

Hence the suggestion to start with a test: when you have the green bar (meaning that the test passes), you're in the same position you are when you get the famous "Hello, world!" message printed out: you are able to compile and run a program.

Since such a beast doesn't seem to exist online and that I've been told it may be hepful, here it one for CppUnit.

//--- Hello, World! for CppUnit

#include <iostream>

#include <cppunit/TestRunner.h>
#include <cppunit/TestResult.h>
#include <cppunit/TestResultCollector.h>
#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/BriefTestProgressListener.h>
#include <cppunit/extensions/TestFactoryRegistry.h>

class Test : public CPPUNIT_NS::TestCase

  void setUp(void) {}
  void tearDown(void) {} 

  void testHelloWorld(void) { std::cout << "Hello, world!" << std::endl; }


int main( int ac, char **av )
  //--- Create the event manager and test controller
  CPPUNIT_NS::TestResult controller;

  //--- Add a listener that colllects test result
  CPPUNIT_NS::TestResultCollector result;
  controller.addListener( &result );        

  //--- Add a listener that print dots as test run.
  CPPUNIT_NS::BriefTestProgressListener progress;
  controller.addListener( &progress );      

  //--- Add the top suite to the test runner
  CPPUNIT_NS::TestRunner runner;
  runner.addTest( CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest() );
  runner.run( controller );

  return result.wasSuccessful() ? 0 : 1;

Compiling with CppUnit

To compile you have to have your include path containing the directory where the cppunit includes are.
Obviously, you shoud link you executable against the cppunit library.

Last modified: May 12