1 // (C) Copyright Raffi Enficiaud 2016.
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)
6 // See http://www.boost.org/libs/test for the library home page.
8 // tests Unit Test Framework logging facilities against
10 // ***************************************************************************
13 #define BOOST_TEST_MAIN
14 #include <boost/test/unit_test.hpp>
15 #include <boost/test/unit_test_log.hpp>
16 #include <boost/test/unit_test_suite.hpp>
17 #include <boost/test/framework.hpp>
18 #include <boost/test/unit_test_parameters.hpp>
19 #include <boost/test/utils/nullstream.hpp>
20 #include <boost/test/execution_monitor.hpp>
22 typedef boost::onullstream onullstream_type
;
25 #include <boost/lexical_cast.hpp>
27 // our special logger for tests
28 #include "logger-for-tests.hpp"
34 using boost::test_tools::output_test_stream
;
35 using namespace boost::unit_test
;
39 //____________________________________________________________________________//
41 // this one should generate a message as it does not execute any assertion
44 void almost_good_foo()
46 BOOST_TEST_WARN( 2>3 );
52 BOOST_TEST_MESSAGE("this is a message");
55 BOOST_TEST_INFO("Context value=something");
56 BOOST_TEST_INFO("Context value2=something different");
57 BOOST_ERROR( "with some message" );
59 BOOST_CHECK_MESSAGE( 1 == 2.3, "non sense" );
63 BOOST_TEST_CONTEXT("some context") {
64 BOOST_FAIL( "very_bad_foo is fatal" );
68 struct local_exception
{};
70 void very_bad_exception() {
71 BOOST_TEST_INFO("Context value=something");
72 BOOST_TEST_INFO("Context value2=something different");
73 BOOST_ERROR( "with some message" );
75 BOOST_TEST_INFO("exception context should be shown");
76 throw local_exception();
79 void bad_foo2() { bad_foo(); } // tests with clashing names
83 using boost::execution_exception
;
84 execution_exception::location dummy
;
85 throw execution_exception(
86 execution_exception::timeout_error
,
91 //____________________________________________________________________________//
93 void check( output_test_stream
& output
,
94 output_format log_format
,
96 log_level ll
= log_successful_tests
)
98 boost::unit_test::unit_test_log
.set_format(log_format
);
99 boost::unit_test::unit_test_log
.set_stream(output
);
100 boost::unit_test::unit_test_log
.set_threshold_level(ll
);
102 output
<< "* " << log_format
<< "-format *******************************************************************";
104 framework::finalize_setup_phase( id
);
105 framework::run( id
, false ); // do not continue the test tree to have the test_log_start/end
108 boost::unit_test::unit_test_log
.set_format(OF_CLF
);
109 boost::unit_test::unit_test_log
.set_stream(std::cout
);
111 BOOST_TEST( output
.match_pattern(true) ); // flushes the stream at the end of the comparison.
114 //____________________________________________________________________________//
116 void check( output_test_stream
& output
, test_suite
* ts
)
118 ts
->p_default_status
.value
= test_unit::RS_ENABLED
;
120 check( output
, OF_CLF
, ts
->p_id
);
121 check( output
, OF_XML
, ts
->p_id
);
122 check( output
, OF_JUNIT
, ts
->p_id
, log_successful_tests
);
123 check( output
, OF_JUNIT
, ts
->p_id
, log_cpp_exception_errors
); // should branch to the log log_all_errors
126 //____________________________________________________________________________//
131 boost::unit_test::unit_test_log
.set_format( runtime_config::get
<output_format
>( runtime_config::btrt_log_format
) );
132 boost::unit_test::unit_test_log
.set_stream( std::cout
);
136 //____________________________________________________________________________//
139 BOOST_AUTO_TEST_CASE( test_logs
)
142 boost::ignore_unused( G
);
144 #define PATTERN_FILE_NAME "log-formatter-test.pattern"
146 std::string
pattern_file_name(
147 framework::master_test_suite().argc
<= 1
148 ? (runtime_config::save_pattern() ? PATTERN_FILE_NAME
: "./baseline-outputs/" PATTERN_FILE_NAME
)
149 : framework::master_test_suite().argv
[1] );
151 output_test_stream_for_loggers
test_output( pattern_file_name
,
152 !runtime_config::save_pattern(),
157 test_suite
* ts_0
= BOOST_TEST_SUITE( "0 test cases inside" );
159 test_suite
* ts_1
= BOOST_TEST_SUITE( "1 test cases inside" );
160 ts_1
->add( BOOST_TEST_CASE( good_foo
) );
162 test_suite
* ts_1b
= BOOST_TEST_SUITE( "1 bad test case inside" );
163 ts_1b
->add( BOOST_TEST_CASE( bad_foo
), 1 );
165 test_suite
* ts_1c
= BOOST_TEST_SUITE( "1 almost good test case inside" );
166 ts_1c
->add( BOOST_TEST_CASE( almost_good_foo
) );
168 test_suite
* ts_2
= BOOST_TEST_SUITE( "2 test cases inside" );
169 ts_2
->add( BOOST_TEST_CASE( good_foo
) );
170 ts_2
->add( BOOST_TEST_CASE( bad_foo
), 1 );
172 test_suite
* ts_3
= BOOST_TEST_SUITE( "3 test cases inside" );
173 ts_3
->add( BOOST_TEST_CASE( bad_foo
) );
174 test_case
* tc1
= BOOST_TEST_CASE( very_bad_foo
);
176 test_case
* tc2
= BOOST_TEST_CASE( bad_foo2
);
178 tc2
->depends_on( tc1
);
180 test_suite
* ts_4
= BOOST_TEST_SUITE( "4 test cases inside" );
181 ts_4
->add( BOOST_TEST_CASE( bad_foo
) );
182 ts_4
->add( BOOST_TEST_CASE( very_bad_foo
) );
183 ts_4
->add( BOOST_TEST_CASE( very_bad_exception
) );
184 ts_4
->add( BOOST_TEST_CASE( bad_foo2
) );
186 test_suite
* ts_main
= BOOST_TEST_SUITE( "Fake Test Suite Hierarchy" );
187 ts_main
->add( ts_0
);
188 ts_main
->add( ts_1
);
189 ts_main
->add( ts_2
);
190 ts_main
->add( ts_3
);
191 ts_main
->add( ts_4
);
193 test_suite
* ts_timeout
= BOOST_TEST_SUITE( "Timeout" );
194 ts_timeout
->add( BOOST_TEST_CASE( good_foo
) );
195 test_case
* tc_timeout
= BOOST_TEST_CASE( timeout_foo
);
196 ts_timeout
->add( tc_timeout
);
198 test_suite
* ts_timeout_nested
= BOOST_TEST_SUITE( "Timeout-nested" );
199 ts_timeout_nested
->add( BOOST_TEST_CASE( good_foo
) );
200 test_suite
* ts_timeout_internal
= BOOST_TEST_SUITE( "Timeout" );
201 ts_timeout_internal
->add( BOOST_TEST_CASE( good_foo
) );
202 test_case
* tc_timeout_internal
= BOOST_TEST_CASE( timeout_foo
);
203 ts_timeout_internal
->add( tc_timeout_internal
);
204 ts_timeout_nested
->add( ts_timeout_internal
);
205 ts_timeout_nested
->add( BOOST_TEST_CASE_NAME( good_foo
, "good_foo2" ) );
207 check( test_output
, ts_1
);
209 check( test_output
, ts_1b
);
211 check( test_output
, ts_1c
);
213 check( test_output
, ts_2
);
215 check( test_output
, ts_3
);
217 check( test_output
, ts_4
);
219 ts_1
->add( BOOST_TEST_CASE( bad_foo
) );
220 ts_3
->depends_on( ts_1
);
222 check( test_output
, ts_main
);
224 check( test_output
, ts_timeout
);
226 check( test_output
, ts_timeout_nested
);
229 //____________________________________________________________________________//
231 BOOST_AUTO_TEST_CASE( test_logs_junit_info_closing_tags
)
234 boost::ignore_unused( G
);
236 #define PATTERN_FILE_NAME_JUNIT "log-formatter-test.pattern.junit"
238 std::string
pattern_file_name(
239 framework::master_test_suite().argc
<= 2
240 ? (runtime_config::save_pattern() ? PATTERN_FILE_NAME_JUNIT
: "./baseline-outputs/" PATTERN_FILE_NAME_JUNIT
)
241 : framework::master_test_suite().argv
[2] );
243 output_test_stream_for_loggers
test_output( pattern_file_name
,
244 !runtime_config::save_pattern(),
249 test_suite
* ts_main
= BOOST_TEST_SUITE( "1 test cases inside" );
250 ts_main
->add( BOOST_TEST_CASE( almost_good_foo
) );
252 ts_main
->p_default_status
.value
= test_unit::RS_ENABLED
;
253 char const* argv
[] = { "a.exe", "--run_test=*", "--build_info" };
254 int argc
= sizeof(argv
)/sizeof(argv
[0]);
255 boost::unit_test::runtime_config::init( argc
, (char**)argv
);
256 boost::unit_test::framework::impl::setup_for_execution( *ts_main
);
258 check( test_output
, OF_JUNIT
, ts_main
->p_id
, log_successful_tests
);
260 test_suite
* ts_timeout
= BOOST_TEST_SUITE( "Timeout" );
261 ts_timeout
->add( BOOST_TEST_CASE( good_foo
) );
262 test_case
* tc_timeout
= BOOST_TEST_CASE( timeout_foo
);
263 ts_timeout
->add( tc_timeout
);
265 check( test_output
, OF_JUNIT
, ts_timeout
->p_id
, log_successful_tests
);
268 test_suite
* ts_account_failures
= BOOST_TEST_SUITE( "1 junit failure is not error" );
269 ts_account_failures
->add( BOOST_TEST_CASE( bad_foo
) );
270 ts_account_failures
->add( BOOST_TEST_CASE( very_bad_foo
) );
271 ts_account_failures
->add( BOOST_TEST_CASE( good_foo
) );
272 ts_account_failures
->add( BOOST_TEST_CASE( bad_foo2
) );
274 char const* argv2
[] = { "a.exe", "--run_test=*", "--build_info=false" };
275 int argc2
= sizeof(argv2
)/sizeof(argv2
[0]);
276 boost::unit_test::runtime_config::init( argc2
, (char**)argv2
);
277 boost::unit_test::framework::impl::setup_for_execution( *ts_account_failures
);
279 check( test_output
, OF_JUNIT
, ts_account_failures
->p_id
, log_messages
);