CppUnit project page | FAQ | CppUnit home page

- Main Page
- Modules
- Namespaces
- Classes
- Files
- Related Pages

- Alphabetical List
- Class List
- Class Hierarchy
- Class Members

# XmlOutputterHook Class Reference

Hook to customize Xml output. More...

`#include <XmlOutputterHook.h>`

List of all members.

## Public Member Functions
virtual void | beginDocument (XmlDocument *document)
virtual void | endDocument (XmlDocument *document)
virtual void | failTestAdded (XmlDocument *document, XmlElement *testElement, Test *test, TestFailure *failure)
virtual void | successfulTestAdded (XmlDocument *document, XmlElement *testElement, Test *test)
virtual void | statisticsAdded (XmlDocument *document, XmlElement *statisticsElement)
virtual | ~XmlOutputterHook ()

## Detailed Description

Hook to customize Xml output.

XmlOutputterHook can be passed to XmlOutputter to customize the XmlDocument.

Common customizations are:

- adding some datas to successfull or failed test with failTestAdded() and successfulTestAdded(),
- adding some statistics with statisticsAdded(),
- adding other datas with beginDocument() or endDocument().

See examples/ClockerPlugIn which makes use of most the hook.

Another simple example of an outputter hook is shown below. It may be used to add some meta information to your result files. In the example, the author name as well as the project name and test creation date is added to the head of the xml file.

In order to make this information stored within the xml file, the virtual member function beginDocument() is overriden where a new XmlElement object is created.

This element is simply added to the root node of the document which makes the information automatically being stored when the xml file is written.

     #include <cppunit/XmlOutputterHook.h>
     #include <cppunit/XmlElement.h>
     #include <cppunit/tools/StringTools.h>

     ...

     class MyXmlOutputterHook : public CppUnit::XmlOutputterHook
     {
     public:
       MyXmlOutputterHook(const std::string projectName,
                          const std::string author)
       {
          m_projectName = projectName;
          m_author      = author;
       };

       virtual ~MyXmlOutputterHook()
       {
       };

       void beginDocument(CppUnit::XmlDocument* document)
       {
         if (!document)
           return;

         // dump current time
         std::string szDate          = CppUnit::StringTools::toString( (int)time(0) );
         CppUnit::XmlElement* metaEl = new CppUnit::XmlElement("SuiteInfo",
                                                               "");

         metaEl->addElement( new CppUnit::XmlElement("Author", m_author) );
         metaEl->addElement( new CppUnit::XmlElement("Project", m_projectName) );
         metaEl->addElement( new CppUnit::XmlElement("Date", szDate ) );

         document->rootElement().addElement(metaEl);
       };
     private:
       std::string m_projectName;
       std::string m_author;
     };

Within your application's main code, you need to snap the hook object into your xml outputter object like shown below:

     CppUnit::TextUi::TestRunner runner;
     std::ofstream outputFile("testResults.xml");

     CppUnit::XmlOutputter* outputter = new CppUnit::XmlOutputter( &runner.result(),
                                                                   outputFile );
     MyXmlOutputterHook hook("myProject", "meAuthor");
     outputter->addHook(&hook);
     runner.setOutputter(outputter);
     runner.addTest( VectorFixture::suite() );
     runner.run();
     outputFile.close();

This results into the following output:

     <TestRun>
       <suiteInfo>
         <author>meAuthor</author>
         <project>myProject</project>
         <date>1028143912</date>
       </suiteInfo>
       <FailedTests>
        ...

See also:
  XmlOutputter, CppUnitTestPlugIn.

## Constructor & Destructor Documentation

virtual XmlOutputterHook::~XmlOutputterHook | ( |  | ) | `[inline, virtual]`

## Member Function Documentation

CPPUNIT_NS_BEGIN void XmlOutputterHook::beginDocument | ( | XmlDocument * | document | ) | `[virtual]`

Called before any elements is added to the root element.

Parameters:

  | document | XML Document being created.

void XmlOutputterHook::endDocument | ( | XmlDocument * | document | ) | `[virtual]`

Called after adding all elements to the root element.

Parameters:

  | document | XML Document being created.

void XmlOutputterHook::failTestAdded | ( | XmlDocument * | document,
|  | XmlElement * | testElement,
|  | Test * | test,
|  | TestFailure * | failure |
| ) |  |  | `[virtual]`

Called after adding a fail test element.

Parameters:

  | document | XML Document being created.
  | testElement | <FailedTest> element.
  | test | Test that failed.
  | failure | Test failure data.

void XmlOutputterHook::successfulTestAdded | ( | XmlDocument * | document,
|  | XmlElement * | testElement,
|  | Test * | test |
| ) |  |  | `[virtual]`

Called after adding a successful test element.

Parameters:

  | document | XML Document being created.
  | testElement | <Test> element.
  | test | Test that was successful.

void XmlOutputterHook::statisticsAdded | ( | XmlDocument * | document,
|  | XmlElement * | statisticsElement |
| ) |  |  | `[virtual]`

Called after adding the statistic element.

Parameters:

  | document | XML Document being created.
  | statisticsElement | <Statistics> element.

The documentation for this class was generated from the following files:

- XmlOutputterHook.h
- XmlOutputterHook.cpp

| hosts this site. |  | Send comments to: CppUnit Developers