XmlOutputterHook.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #ifndef CPPUNIT_XMLOUTPUTTERHOOK_H
  2. #define CPPUNIT_XMLOUTPUTTERHOOK_H
  3. #include <cppunit/Portability.h>
  4. CPPUNIT_NS_BEGIN
  5. class Test;
  6. class TestFailure;
  7. class XmlDocument;
  8. class XmlElement;
  9. /*! \brief Hook to customize Xml output.
  10. *
  11. * XmlOutputterHook can be passed to XmlOutputter to customize the XmlDocument.
  12. *
  13. * Common customizations are:
  14. * - adding some datas to successfull or failed test with
  15. * failTestAdded() and successfulTestAdded(),
  16. * - adding some statistics with statisticsAdded(),
  17. * - adding other datas with beginDocument() or endDocument().
  18. *
  19. * See examples/ClockerPlugIn which makes use of most the hook.
  20. *
  21. * Another simple example of an outputter hook is shown below. It may be
  22. * used to add some meta information to your result files. In the example,
  23. * the author name as well as the project name and test creation date is
  24. * added to the head of the xml file.
  25. *
  26. * In order to make this information stored within the xml file, the virtual
  27. * member function beginDocument() is overriden where a new
  28. * XmlElement object is created.
  29. *
  30. * This element is simply added to the root node of the document which
  31. * makes the information automatically being stored when the xml file
  32. * is written.
  33. *
  34. * \code
  35. * #include <cppunit/XmlOutputterHook.h>
  36. * #include <cppunit/XmlElement.h>
  37. * #include <cppunit/tools/StringTools.h>
  38. *
  39. * ...
  40. *
  41. * class MyXmlOutputterHook : public CppUnit::XmlOutputterHook
  42. * {
  43. * public:
  44. * MyXmlOutputterHook(const std::string projectName,
  45. * const std::string author)
  46. * {
  47. * m_projectName = projectName;
  48. * m_author = author;
  49. * };
  50. *
  51. * virtual ~MyXmlOutputterHook()
  52. * {
  53. * };
  54. *
  55. * void beginDocument(CppUnit::XmlDocument* document)
  56. * {
  57. * if (!document)
  58. * return;
  59. *
  60. * // dump current time
  61. * std::string szDate = CppUnit::StringTools::toString( (int)time(0) );
  62. * CppUnit::XmlElement* metaEl = new CppUnit::XmlElement("SuiteInfo",
  63. * "");
  64. *
  65. * metaEl->addElement( new CppUnit::XmlElement("Author", m_author) );
  66. * metaEl->addElement( new CppUnit::XmlElement("Project", m_projectName) );
  67. * metaEl->addElement( new CppUnit::XmlElement("Date", szDate ) );
  68. *
  69. * document->rootElement().addElement(metaEl);
  70. * };
  71. * private:
  72. * std::string m_projectName;
  73. * std::string m_author;
  74. * };
  75. * \endcode
  76. *
  77. * Within your application's main code, you need to snap the hook
  78. * object into your xml outputter object like shown below:
  79. *
  80. * \code
  81. * CppUnit::TextUi::TestRunner runner;
  82. * std::ofstream outputFile("testResults.xml");
  83. *
  84. * CppUnit::XmlOutputter* outputter = new CppUnit::XmlOutputter( &runner.result(),
  85. * outputFile );
  86. * MyXmlOutputterHook hook("myProject", "meAuthor");
  87. * outputter->addHook(&hook);
  88. * runner.setOutputter(outputter);
  89. * runner.addTest( VectorFixture::suite() );
  90. * runner.run();
  91. * outputFile.close();
  92. * \endcode
  93. *
  94. * This results into the following output:
  95. *
  96. * \code
  97. * <TestRun>
  98. * <suiteInfo>
  99. * <author>meAuthor</author>
  100. * <project>myProject</project>
  101. * <date>1028143912</date>
  102. * </suiteInfo>
  103. * <FailedTests>
  104. * ...
  105. * \endcode
  106. *
  107. * \see XmlOutputter, CppUnitTestPlugIn.
  108. */
  109. class CPPUNIT_API XmlOutputterHook
  110. {
  111. public:
  112. /*! Called before any elements is added to the root element.
  113. * \param document XML Document being created.
  114. */
  115. virtual void beginDocument( XmlDocument *document );
  116. /*! Called after adding all elements to the root element.
  117. * \param document XML Document being created.
  118. */
  119. virtual void endDocument( XmlDocument *document );
  120. /*! Called after adding a fail test element.
  121. * \param document XML Document being created.
  122. * \param testElement \<FailedTest\> element.
  123. * \param test Test that failed.
  124. * \param failure Test failure data.
  125. */
  126. virtual void failTestAdded( XmlDocument *document,
  127. XmlElement *testElement,
  128. Test *test,
  129. TestFailure *failure );
  130. /*! Called after adding a successful test element.
  131. * \param document XML Document being created.
  132. * \param testElement \<Test\> element.
  133. * \param test Test that was successful.
  134. */
  135. virtual void successfulTestAdded( XmlDocument *document,
  136. XmlElement *testElement,
  137. Test *test );
  138. /*! Called after adding the statistic element.
  139. * \param document XML Document being created.
  140. * \param statisticsElement \<Statistics\> element.
  141. */
  142. virtual void statisticsAdded( XmlDocument *document,
  143. XmlElement *statisticsElement );
  144. virtual ~XmlOutputterHook() {}
  145. };
  146. CPPUNIT_NS_END
  147. #endif // CPPUNIT_XMLOUTPUTTERHOOK_H