framework.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. // (C) Copyright Gennadiy Rozental 2001.
  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 Defines Unit Test Framework mono-state interfaces.
  9. //! The framework interfaces are based on Monostate design pattern.
  10. // ***************************************************************************
  11. #ifndef BOOST_TEST_FRAMEWORK_HPP_020805GER
  12. #define BOOST_TEST_FRAMEWORK_HPP_020805GER
  13. // Boost.Test
  14. #include <boost/test/detail/global_typedef.hpp>
  15. #include <boost/test/detail/fwd_decl.hpp>
  16. #include <boost/test/detail/throw_exception.hpp>
  17. #include <boost/test/detail/suppress_warnings.hpp>
  18. // STL
  19. #include <stdexcept>
  20. //____________________________________________________________________________//
  21. namespace boost {
  22. /// Main namespace for the Unit Test Framework interfaces and implementation
  23. namespace unit_test {
  24. // ************************************************************************** //
  25. // ************** init_unit_test_func ************** //
  26. // ************************************************************************** //
  27. /// Test module initialization routine signature
  28. /// Different depending on whether BOOST_TEST_ALTERNATIVE_INIT_API is defined or not
  29. #ifdef BOOST_TEST_ALTERNATIVE_INIT_API
  30. typedef bool (*init_unit_test_func)();
  31. #else
  32. typedef test_suite* (*init_unit_test_func)( int, char* [] );
  33. #endif
  34. // ************************************************************************** //
  35. // ************** framework ************** //
  36. // ************************************************************************** //
  37. /// Namespace of the Unit Test Framework mono-state
  38. namespace framework {
  39. /// @name Unit Test Framework initialization and shutdown
  40. /// @{
  41. /// @brief This function performs initialization of the framework mono-state.
  42. ///
  43. /// It needs to be called every time before the test is started.
  44. /// @param[in] init_func test module initialization routine
  45. /// @param[in] argc command line arguments collection
  46. /// @param[in] argv command line arguments collection
  47. BOOST_TEST_DECL void init( init_unit_test_func init_func, int argc, char* argv[] );
  48. /// This function applies all the decorators and figures out default run status. This argument facilitates an
  49. /// ability of the test cases to prepare some other test units (primarily used internally for self testing).
  50. /// @param[in] tu Optional id of the test unit representing root of test tree. If absent, master test suite is used
  51. BOOST_TEST_DECL void finalize_setup_phase( test_unit_id tu = INV_TEST_UNIT_ID);
  52. /// This function returns true when testing is in progress (setup is finished).
  53. BOOST_TEST_DECL bool test_in_progress();
  54. /// This function shuts down the framework and clears up its mono-state.
  55. ///
  56. /// It needs to be at the very end of test module execution
  57. BOOST_TEST_DECL void shutdown();
  58. /// @}
  59. /// @name Test unit registration
  60. /// @{
  61. /// Provides both read and write access to current "leaf" auto test suite during the test unit registration phase.
  62. ///
  63. /// During auto-registration phase the framework maintain a FIFO queue of test units being registered. New test units become children
  64. /// of the current "leaf" test suite and if this is test suite it is pushed back into queue and becomes a new leaf.
  65. /// When test suite registration is completed, a test suite is popped from the back of the queue. Only automatically registered test suites
  66. /// should be added to this queue. Master test suite is always a zero element in this queue, so if no other test suites are registered
  67. /// all test cases are added to master test suite.
  68. /// This function facilitates all three possible actions:
  69. /// - if no argument are provided it returns the current queue leaf test suite
  70. /// - if test suite is provided and no second argument are set, test suite is added to the queue
  71. /// - if no test suite are provided and last argument is false, the semantic of this function is similar to queue pop: last element is popped from the queue
  72. /// @param[in] ts test suite to push back to the queue
  73. /// @param[in] push_or_pop should we push ts to the queue or pop leaf test suite instead
  74. /// @returns a reference to the currently active/"leaf" test suite
  75. BOOST_TEST_DECL test_suite& current_auto_test_suite( test_suite* ts = 0, bool push_or_pop = true );
  76. /// This function add new test case into the global collection of test units the framework aware of.
  77. /// This function also assignes unique test unit id for every test case. Later on one can use this id to locate
  78. /// the test case if necessary. This is the way for the framework to maintain weak references between test units.
  79. /// @param[in] tc test case to register
  80. BOOST_TEST_DECL void register_test_unit( test_case* tc );
  81. /// This function add new test suite into the global collection of test units the framework aware of.
  82. /// This function also assignes unique test unit id for every test suite. Later on one can use this id to locate
  83. /// the test case if necessary. This is the way for the framework to maintain weak references between test units.
  84. /// @param[in] ts test suite to register
  85. BOOST_TEST_DECL void register_test_unit( test_suite* ts );
  86. /// This function removes the test unit from the collection of known test units and destroys the test unit object.
  87. /// This function also assigns unique test unit id for every test case. Later on one can use this id to located
  88. /// the test case if necessary. This is the way for the framework to maintain weak references between test units.
  89. /// @param[in] tu test unit to deregister
  90. BOOST_TEST_DECL void deregister_test_unit( test_unit* tu );
  91. // This function clears up the framework mono-state.
  92. /// After this call the framework can be reinitialized to perform a second test run during the same program lifetime.
  93. BOOST_TEST_DECL void clear();
  94. /// @}
  95. /// @name Test observer registration
  96. /// @{
  97. /// Adds new test execution observer object into the framework's list of test observers.
  98. /// Observer lifetime should exceed the the testing execution timeframe
  99. /// @param[in] to test observer object to add
  100. BOOST_TEST_DECL void register_observer( test_observer& to );
  101. /// Excludes the observer object form the framework's list of test observers
  102. /// @param[in] to test observer object to exclude
  103. BOOST_TEST_DECL void deregister_observer( test_observer& to );
  104. /// @}
  105. /// @name Global fixtures registration
  106. /// @{
  107. /// Adds a new global fixture to be setup before any other tests starts and tore down after
  108. /// any other tests finished.
  109. /// Test unit fixture lifetime should exceed the testing execution timeframe
  110. /// @param[in] tuf fixture to add
  111. BOOST_TEST_DECL void register_global_fixture( global_fixture& tuf );
  112. /// Removes a test global fixture from the framework
  113. ///
  114. /// Test unit fixture lifetime should exceed the testing execution timeframe
  115. /// @param[in] tuf fixture to remove
  116. BOOST_TEST_DECL void deregister_global_fixture( global_fixture& tuf );
  117. /// @}
  118. /// @name Assertion/uncaught exception context support
  119. /// @{
  120. /// Context accessor
  121. struct BOOST_TEST_DECL context_generator {
  122. context_generator() : m_curr_frame( 0 ) {}
  123. /// Is there any context?
  124. bool is_empty() const;
  125. /// Give me next frame; empty - last frame
  126. const_string next() const;
  127. private:
  128. // Data members
  129. mutable unsigned m_curr_frame;
  130. };
  131. /// Records context frame message.
  132. /// Some context frames are sticky - they can only explicitly cleared by specifying context id. Other (non sticky) context frames cleared after every assertion.
  133. /// @param[in] context_descr context frame message
  134. /// @param[in] sticky is this sticky frame or not
  135. /// @returns id of the newly created frame
  136. BOOST_TEST_DECL int add_context( lazy_ostream const& context_descr, bool sticky );
  137. /// Erases context frame (when test exits context scope)
  138. /// If context_id is passed clears that specific context frame identified by this id, otherwise clears all non sticky contexts.
  139. BOOST_TEST_DECL void clear_context( int context_id = -1 );
  140. /// Produces an instance of small "delegate" object, which facilitates access to collected context.
  141. BOOST_TEST_DECL context_generator get_context();
  142. /// @}
  143. /// @name Access to registered test units.
  144. /// @{
  145. /// This function provides access to the master test suite.
  146. /// There is only only master test suite per test module.
  147. /// @returns a reference the master test suite instance
  148. BOOST_TEST_DECL master_test_suite_t& master_test_suite();
  149. /// This function provides an access to the test unit currently being executed.
  150. /// The difference with current_test_case is about the time between a test-suite
  151. /// is being set up or torn down (fixtures) and when the test-cases of that suite start.
  152. /// This function is only valid during test execution phase.
  153. /// @see current_test_case_id, current_test_case
  154. BOOST_TEST_DECL test_unit const& current_test_unit();
  155. /// This function provides an access to the test case currently being executed.
  156. /// This function is only valid during test execution phase.
  157. /// @see current_test_case_id
  158. BOOST_TEST_DECL test_case const& current_test_case();
  159. /// This function provides an access to an id of the test case currently being executed.
  160. /// This function safer than current_test_case, cause if wont throw if no test case is being executed.
  161. /// @see current_test_case
  162. BOOST_TEST_DECL test_unit_id current_test_case_id(); /* safe version of above */
  163. /// This function provides access to a test unit by id and type combination. It will throw if no test unit located.
  164. /// @param[in] tu_id id of a test unit to locate
  165. /// @param[in] tu_type type of a test unit to locate
  166. /// @returns located test unit
  167. BOOST_TEST_DECL test_unit& get( test_unit_id tu_id, test_unit_type tu_type );
  168. /// This function template provides access to a typed test unit by id
  169. /// It will throw if you specify incorrect test unit type
  170. /// @tparam UnitType compile time type of test unit to get (test_suite or test_case)
  171. /// @param id id of test unit to get
  172. template<typename UnitType>
  173. inline UnitType& get( test_unit_id id )
  174. {
  175. return static_cast<UnitType&>( get( id, static_cast<test_unit_type>(UnitType::type) ) );
  176. }
  177. ///@}
  178. /// @name Test initiation interface
  179. /// @{
  180. /// Initiates test execution
  181. /// This function is used to start the test execution from a specific "root" test unit.
  182. /// If no root provided, test is started from master test suite. This second argument facilitates an ability of the test cases to
  183. /// start some other test units (primarily used internally for self testing).
  184. /// @param[in] tu Optional id of the test unit or test unit itself from which the test is started. If absent, master test suite is used
  185. /// @param[in] continue_test true == continue test if it was already started, false == restart the test from scratch regardless
  186. BOOST_TEST_DECL void run( test_unit_id tu = INV_TEST_UNIT_ID, bool continue_test = true );
  187. /// Initiates test execution. Same as other overload
  188. BOOST_TEST_DECL void run( test_unit const* tu, bool continue_test = true );
  189. /// @}
  190. /// @name Test events dispatchers
  191. /// @{
  192. /// Reports results of assertion to all test observers
  193. BOOST_TEST_DECL void assertion_result( unit_test::assertion_result ar );
  194. /// Reports uncaught exception to all test observers
  195. BOOST_TEST_DECL void exception_caught( execution_exception const& );
  196. /// Reports aborted test unit to all test observers
  197. BOOST_TEST_DECL void test_unit_aborted( test_unit const& );
  198. /// Reports aborted test module to all test observers
  199. BOOST_TEST_DECL void test_aborted( );
  200. /// @}
  201. namespace impl {
  202. // exclusively for self test
  203. BOOST_TEST_DECL void setup_for_execution( test_unit const& );
  204. BOOST_TEST_DECL void setup_loggers( );
  205. // Helper for setting the name of the master test suite globally
  206. struct BOOST_TEST_DECL master_test_suite_name_setter {
  207. master_test_suite_name_setter( const_string name );
  208. };
  209. } // namespace impl
  210. // ************************************************************************** //
  211. // ************** framework errors ************** //
  212. // ************************************************************************** //
  213. /// This exception type is used to report internal Boost.Test framework errors.
  214. struct BOOST_TEST_DECL internal_error : public std::runtime_error {
  215. internal_error( const_string m ) : std::runtime_error( std::string( m.begin(), m.size() ) ) {}
  216. };
  217. //____________________________________________________________________________//
  218. /// This exception type is used to report test module setup errors.
  219. struct BOOST_TEST_DECL setup_error : public std::runtime_error {
  220. setup_error( const_string m ) : std::runtime_error( std::string( m.begin(), m.size() ) ) {}
  221. };
  222. #define BOOST_TEST_SETUP_ASSERT( cond, msg ) BOOST_TEST_I_ASSRT( cond, unit_test::framework::setup_error( msg ) )
  223. //____________________________________________________________________________//
  224. struct nothing_to_test {
  225. explicit nothing_to_test( int rc ) : m_result_code( rc ) {}
  226. int m_result_code;
  227. };
  228. //____________________________________________________________________________//
  229. } // namespace framework
  230. } // unit_test
  231. } // namespace boost
  232. #include <boost/test/detail/enable_warnings.hpp>
  233. #endif // BOOST_TEST_FRAMEWORK_HPP_020805GER