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 typedef boost::onullstream onullstream_type
;
23 #include <boost/lexical_cast.hpp>
25 // our special logger for tests
26 #include "logger-for-tests.hpp"
32 using boost::test_tools::output_test_stream
;
33 using namespace boost::unit_test
;
34 namespace utf
= boost::unit_test
;
37 //____________________________________________________________________________//
39 // this one should generate a message as it does not execute any assertion
42 void almost_good_foo()
44 BOOST_TEST_WARN( 2>3 );
50 BOOST_TEST_MESSAGE("this is a message");
53 BOOST_TEST_INFO("Context value=something");
54 BOOST_TEST_INFO("Context value2=something different");
55 BOOST_ERROR( "with some message" );
57 BOOST_CHECK_MESSAGE( 1 == 2.3, "non sense" );
61 BOOST_TEST_CONTEXT("some context") {
62 BOOST_FAIL( "very_bad_foo is fatal" );
66 struct local_exception
{};
68 void very_bad_exception() {
69 BOOST_TEST_INFO("Context value=something");
70 BOOST_TEST_INFO("Context value2=something different");
71 BOOST_ERROR( "with some message" );
73 BOOST_TEST_INFO("exception context should be shown");
74 throw local_exception();
78 //____________________________________________________________________________//
80 void check( output_test_stream
& output
,
81 output_format log_format
,
83 log_level ll
= log_successful_tests
)
85 boost::unit_test::unit_test_log
.set_format(log_format
);
86 boost::unit_test::unit_test_log
.set_stream(output
);
87 boost::unit_test::unit_test_log
.set_threshold_level(ll
);
89 output
<< "* " << log_format
<< "-format *******************************************************************";
91 framework::finalize_setup_phase( id
);
92 framework::run( id
, false ); // do not continue the test tree to have the test_log_start/end
95 boost::unit_test::unit_test_log
.set_format(OF_CLF
);
96 boost::unit_test::unit_test_log
.set_stream(std::cout
);
98 BOOST_TEST( output
.match_pattern(true) ); // flushes the stream at the end of the comparison.
101 //____________________________________________________________________________//
103 void check( output_test_stream
& output
, test_suite
* ts
)
105 ts
->p_default_status
.value
= test_unit::RS_ENABLED
;
107 check( output
, OF_CLF
, ts
->p_id
);
108 check( output
, OF_XML
, ts
->p_id
);
109 check( output
, OF_JUNIT
, ts
->p_id
, log_successful_tests
);
110 check( output
, OF_JUNIT
, ts
->p_id
, log_cpp_exception_errors
); // should branch to the log log_all_errors
113 //____________________________________________________________________________//
118 boost::unit_test::unit_test_log
.set_format( runtime_config::get
<output_format
>( runtime_config::btrt_log_format
) );
119 boost::unit_test::unit_test_log
.set_stream( std::cout
);
123 //____________________________________________________________________________//
126 BOOST_AUTO_TEST_CASE( test_result_reports
)
129 ut_detail::ignore_unused_variable_warning( G
);
131 #define PATTERN_FILE_NAME "log-formatter-test.pattern"
133 std::string
pattern_file_name(
134 framework::master_test_suite().argc
== 1
135 ? (runtime_config::save_pattern() ? PATTERN_FILE_NAME
: "./baseline-outputs/" PATTERN_FILE_NAME
)
136 : framework::master_test_suite().argv
[1] );
138 output_test_stream_for_loggers
test_output( pattern_file_name
,
139 !runtime_config::save_pattern(),
144 test_suite
* ts_0
= BOOST_TEST_SUITE( "0 test cases inside" );
146 test_suite
* ts_1
= BOOST_TEST_SUITE( "1 test cases inside" );
147 ts_1
->add( BOOST_TEST_CASE( good_foo
) );
149 test_suite
* ts_1b
= BOOST_TEST_SUITE( "1 bad test case inside" );
150 ts_1b
->add( BOOST_TEST_CASE( bad_foo
), 1 );
152 test_suite
* ts_1c
= BOOST_TEST_SUITE( "1 almost good test case inside" );
153 ts_1c
->add( BOOST_TEST_CASE( almost_good_foo
) );
155 test_suite
* ts_2
= BOOST_TEST_SUITE( "2 test cases inside" );
156 ts_2
->add( BOOST_TEST_CASE( good_foo
) );
157 ts_2
->add( BOOST_TEST_CASE( bad_foo
), 1 );
159 test_suite
* ts_3
= BOOST_TEST_SUITE( "3 test cases inside" );
160 ts_3
->add( BOOST_TEST_CASE( bad_foo
) );
161 test_case
* tc1
= BOOST_TEST_CASE( very_bad_foo
);
163 test_case
* tc2
= BOOST_TEST_CASE( bad_foo
);
165 tc2
->depends_on( tc1
);
167 test_suite
* ts_4
= BOOST_TEST_SUITE( "4 test cases inside" );
168 ts_4
->add( BOOST_TEST_CASE( bad_foo
) );
169 ts_4
->add( BOOST_TEST_CASE( very_bad_foo
) );
170 ts_4
->add( BOOST_TEST_CASE( very_bad_exception
) );
171 ts_4
->add( BOOST_TEST_CASE( bad_foo
) );
173 test_suite
* ts_main
= BOOST_TEST_SUITE( "Fake Test Suite Hierarchy" );
174 ts_main
->add( ts_0
);
175 ts_main
->add( ts_1
);
176 ts_main
->add( ts_2
);
177 ts_main
->add( ts_3
);
178 ts_main
->add( ts_4
);
180 check( test_output
, ts_1
);
182 check( test_output
, ts_1b
);
184 check( test_output
, ts_1c
);
186 check( test_output
, ts_2
);
188 check( test_output
, ts_3
);
190 check( test_output
, ts_4
);
192 ts_1
->add( BOOST_TEST_CASE( bad_foo
) );
193 ts_3
->depends_on( ts_1
);
195 check( test_output
, ts_main
);
198 //____________________________________________________________________________//