junit_log_formatter.hpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // (C) Copyright 2016 Raffi Enficiaud.
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // See http://www.boost.org/libs/test for the library home page.
  6. //
  7. ///@file
  8. ///@brief Contains the definition of the Junit log formatter (OF_JUNIT)
  9. // ***************************************************************************
  10. #ifndef BOOST_TEST_JUNIT_LOG_FORMATTER__
  11. #define BOOST_TEST_JUNIT_LOG_FORMATTER__
  12. // Boost.Test
  13. #include <boost/test/detail/global_typedef.hpp>
  14. #include <boost/test/unit_test_log_formatter.hpp>
  15. #include <boost/test/tree/test_unit.hpp>
  16. //#include <boost/test/results_collector.hpp>
  17. // STL
  18. #include <cstddef> // std::size_t
  19. #include <map>
  20. #include <list>
  21. #include <boost/test/detail/suppress_warnings.hpp>
  22. //____________________________________________________________________________//
  23. namespace boost {
  24. namespace unit_test {
  25. namespace output {
  26. namespace junit_impl {
  27. // helper for the JUnit logger
  28. struct junit_log_helper
  29. {
  30. struct assertion_entry {
  31. enum log_entry_t {
  32. log_entry_info,
  33. log_entry_error,
  34. log_entry_failure
  35. };
  36. assertion_entry() : sealed(false)
  37. {}
  38. std::string logentry_message; // the message associated to the JUnit error/entry
  39. std::string logentry_type; // the one that will get expanded in the final junit (failure, error)
  40. std::string output; // additional information/message generated by the assertion
  41. log_entry_t log_entry; // the type associated to the assertion (or error)
  42. bool sealed; // indicates if the entry can accept additional information
  43. };
  44. std::list<std::string> system_out; // sysout: additional information
  45. std::list<std::string> system_err; // syserr: additional information
  46. std::string skipping_reason;
  47. // list of failure, errors and messages (assertions message and the full log)
  48. std::vector< assertion_entry > assertion_entries;
  49. bool skipping;
  50. junit_log_helper(): skipping(false)
  51. {}
  52. void clear() {
  53. assertion_entries.clear();
  54. system_out.clear();
  55. system_err.clear();
  56. skipping_reason.clear();
  57. skipping = false;
  58. }
  59. };
  60. }
  61. // ************************************************************************** //
  62. // ************** junit_log_formatter ************** //
  63. // ************************************************************************** //
  64. /// JUnit logger class
  65. class junit_log_formatter : public unit_test_log_formatter {
  66. public:
  67. junit_log_formatter() : m_display_build_info(false)
  68. {
  69. // we log everything from the logger singleton point of view
  70. // because we need to know about all the messages/commands going to the logger
  71. // we decide what we put inside the logs internally
  72. this->m_log_level = log_successful_tests;
  73. m_log_level_internal = log_messages;
  74. }
  75. // Formatter interface
  76. void log_start( std::ostream&, counter_t test_cases_amount );
  77. void log_finish( std::ostream& );
  78. void log_build_info( std::ostream&, bool );
  79. void test_unit_start( std::ostream&, test_unit const& tu );
  80. void test_unit_finish( std::ostream&, test_unit const& tu, unsigned long elapsed );
  81. void test_unit_skipped( std::ostream&, test_unit const& tu, const_string reason );
  82. void test_unit_aborted( std::ostream& os, test_unit const& tu );
  83. void test_unit_timed_out( std::ostream& os, test_unit const& tu);
  84. void log_exception_start( std::ostream&, log_checkpoint_data const&, execution_exception const& ex );
  85. void log_exception_finish( std::ostream& );
  86. void log_entry_start( std::ostream&, log_entry_data const&, log_entry_types let );
  87. using unit_test_log_formatter::log_entry_value; // bring base class functions into overload set
  88. void log_entry_value( std::ostream&, const_string value );
  89. void log_entry_finish( std::ostream& );
  90. void entry_context_start( std::ostream&, log_level );
  91. void log_entry_context( std::ostream&, log_level, const_string );
  92. void entry_context_finish( std::ostream&, log_level );
  93. //! Discards changes in the log level
  94. virtual void set_log_level(log_level ll)
  95. {
  96. if(ll > log_successful_tests && ll < log_messages)
  97. ll = log_successful_tests;
  98. else if (ll > log_all_errors)
  99. ll = log_all_errors;
  100. this->m_log_level_internal = ll;
  101. }
  102. //! Instead of a regular stream, returns a file name corresponding to
  103. //! the current master test suite. If the file already exists, adds an index
  104. //! to it.
  105. virtual std::string get_default_stream_description() const;
  106. private:
  107. typedef std::map<test_unit_id, junit_impl::junit_log_helper> map_trace_t;
  108. map_trace_t map_tests;
  109. junit_impl::junit_log_helper runner_log_entry;
  110. junit_impl::junit_log_helper& get_current_log_entry() {
  111. if(list_path_to_root.empty())
  112. return runner_log_entry;
  113. map_trace_t::iterator it = map_tests.find(list_path_to_root.back());
  114. return (it == map_tests.end() ? runner_log_entry : it->second);
  115. }
  116. std::list<test_unit_id> list_path_to_root;
  117. bool m_display_build_info;
  118. bool m_is_last_assertion_or_error; // true if failure, false if error
  119. log_level m_log_level_internal;
  120. friend class junit_result_helper;
  121. };
  122. } // namespace output
  123. } // namespace unit_test
  124. } // namespace boost
  125. #include <boost/test/detail/enable_warnings.hpp>
  126. #endif // BOOST_TEST_JUNIT_LOG_FORMATTER__