Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  1. Include a dedicated header file in the source file that contains the CppUnit main function:

    Code Block
    #include "cpptest/extensions/cppunit/results_listener.h"
    #include "cpptest/extensions/cppunit/coverage_annotator.h"
  2. Install the results listener. The installation details will depend on the TestRunner class of the CppUnit framework you use to execute unit tests.
    For simple setups, your modified main function may resemble the following:

    Code Block
         /* required header files */
    
         int main()
         {
             CPPUNIT_NS::Test *suite = CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest();
             CPPUNIT_NS::TextUi::TestRunner runner;
             runner.addTest( suite );
    
             CppTest_CppUnitResultsOutputterCppUnitResultsListener cpptestResListener;
             CppTest_CppUnitCoverageAnnotator cpptestCovAnnotator;
    
             runner.eventManager().addListener( &cpptestResListener );
             runner.eventManager().addListener( &cpptestCovAnnotator );
    
             runner.setOutputter( new CPPUNIT_NS::CompilerOutputter( &runner.result(), std::cout));
             bool wasSucessful = runner.run();
             return wasSucessful ? 0 : 1;

    Depending on the class that was used, installation of the modifications may differ. The examples below show installation for two classes of the CppUnit framework.
    Installation for the CppUnit::TestRunner class: 

    Code Block
         CppUnit::TestResult controller;
         CppTest_CppUnitResultsListener cpptestResListener;
         CppTest_CppUnitCoverageAnnotator cpptestCovAnnotator;
         controller.addListener( &cpptestResListener );      
         controller.addListener( &cpptestCovAnnotator );      
    
         CppUnit::TestRunner runner;
         runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest() );   
         runner.run( controller, testPath );

    Installation for the CppUnit::TextTestRunner class:

    Code Block
     CppUnit::TextTestRunner runner;
         CppTest_CppUnitResultsListener cpptestResListener;
         CppTest_CppUnitCoverageAnnotator cpptestCovAnnotator;
         runner.eventManager().addListener(&cpptestResListener);
         runner.eventManager().addListener(&cpptestCovAnnotator);
  3. Modify your build system configuration to specify the results_listener.h header file location with the -I option:

    Code Block
    -I<C++test Installation Directory>/runtime/include 

...