]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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) | |
5 | ||
6 | // See http://www.boost.org/libs/test for the library home page. | |
7 | // | |
8 | // tests Unit Test Framework logging facilities against | |
9 | // pattern file | |
10 | // *************************************************************************** | |
11 | ||
12 | // Boost.Test | |
13 | #define BOOST_TEST_MAIN | |
b32b8144 | 14 | #include <boost/test/unit_test.hpp> |
7c673cae | 15 | #include <boost/test/unit_test_log.hpp> |
7c673cae FG |
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> | |
92f5a8d4 TL |
20 | #include <boost/test/execution_monitor.hpp> |
21 | ||
7c673cae | 22 | typedef boost::onullstream onullstream_type; |
7c673cae FG |
23 | |
24 | // BOOST | |
25 | #include <boost/lexical_cast.hpp> | |
26 | ||
b32b8144 FG |
27 | // our special logger for tests |
28 | #include "logger-for-tests.hpp" | |
29 | ||
7c673cae FG |
30 | // STL |
31 | #include <iostream> | |
32 | #include <ios> | |
33 | ||
34 | using boost::test_tools::output_test_stream; | |
35 | using namespace boost::unit_test; | |
11fdf7f2 | 36 | |
7c673cae FG |
37 | |
38 | ||
39 | //____________________________________________________________________________// | |
40 | ||
41 | // this one should generate a message as it does not execute any assertion | |
42 | void good_foo() {} | |
43 | ||
44 | void almost_good_foo() | |
45 | { | |
46 | BOOST_TEST_WARN( 2>3 ); | |
47 | } | |
48 | ||
49 | void bad_foo() { | |
50 | BOOST_ERROR( "" ); | |
51 | ||
52 | BOOST_TEST_MESSAGE("this is a message"); | |
53 | BOOST_CHECK(true); | |
54 | ||
55 | BOOST_TEST_INFO("Context value=something"); | |
56 | BOOST_TEST_INFO("Context value2=something different"); | |
57 | BOOST_ERROR( "with some message" ); | |
58 | ||
59 | BOOST_CHECK_MESSAGE( 1 == 2.3, "non sense" ); | |
60 | } | |
61 | ||
7c673cae FG |
62 | void very_bad_foo() { |
63 | BOOST_TEST_CONTEXT("some context") { | |
64 | BOOST_FAIL( "very_bad_foo is fatal" ); | |
65 | } | |
66 | } | |
67 | ||
68 | struct local_exception {}; | |
69 | ||
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" ); | |
74 | ||
75 | BOOST_TEST_INFO("exception context should be shown"); | |
76 | throw local_exception(); | |
77 | } | |
78 | ||
11fdf7f2 | 79 | void bad_foo2() { bad_foo(); } // tests with clashing names |
7c673cae | 80 | |
92f5a8d4 TL |
81 | void timeout_foo() |
82 | { | |
83 | using boost::execution_exception; | |
84 | execution_exception::location dummy; | |
85 | throw execution_exception( | |
86 | execution_exception::timeout_error, | |
87 | "fake timeout", | |
88 | dummy); | |
89 | } | |
90 | ||
7c673cae FG |
91 | //____________________________________________________________________________// |
92 | ||
b32b8144 FG |
93 | void check( output_test_stream& output, |
94 | output_format log_format, | |
95 | test_unit_id id, | |
96 | log_level ll = log_successful_tests ) | |
7c673cae FG |
97 | { |
98 | boost::unit_test::unit_test_log.set_format(log_format); | |
99 | boost::unit_test::unit_test_log.set_stream(output); | |
b32b8144 | 100 | boost::unit_test::unit_test_log.set_threshold_level(ll); |
7c673cae FG |
101 | |
102 | output << "* " << log_format << "-format *******************************************************************"; | |
103 | output << std::endl; | |
104 | framework::finalize_setup_phase( id ); | |
105 | framework::run( id, false ); // do not continue the test tree to have the test_log_start/end | |
106 | output << std::endl; | |
107 | ||
108 | boost::unit_test::unit_test_log.set_format(OF_CLF); | |
109 | boost::unit_test::unit_test_log.set_stream(std::cout); | |
110 | ||
111 | BOOST_TEST( output.match_pattern(true) ); // flushes the stream at the end of the comparison. | |
112 | } | |
113 | ||
114 | //____________________________________________________________________________// | |
115 | ||
116 | void check( output_test_stream& output, test_suite* ts ) | |
117 | { | |
118 | ts->p_default_status.value = test_unit::RS_ENABLED; | |
119 | ||
120 | check( output, OF_CLF, ts->p_id ); | |
121 | check( output, OF_XML, ts->p_id ); | |
b32b8144 FG |
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 | |
7c673cae FG |
124 | } |
125 | ||
126 | //____________________________________________________________________________// | |
127 | ||
128 | struct guard { | |
129 | ~guard() | |
130 | { | |
b32b8144 | 131 | boost::unit_test::unit_test_log.set_format( runtime_config::get<output_format>( runtime_config::btrt_log_format ) ); |
7c673cae FG |
132 | boost::unit_test::unit_test_log.set_stream( std::cout ); |
133 | } | |
134 | }; | |
135 | ||
7c673cae FG |
136 | //____________________________________________________________________________// |
137 | ||
138 | ||
11fdf7f2 | 139 | BOOST_AUTO_TEST_CASE( test_logs ) |
7c673cae FG |
140 | { |
141 | guard G; | |
92f5a8d4 | 142 | boost::ignore_unused( G ); |
7c673cae FG |
143 | |
144 | #define PATTERN_FILE_NAME "log-formatter-test.pattern" | |
145 | ||
146 | std::string pattern_file_name( | |
11fdf7f2 | 147 | framework::master_test_suite().argc <= 1 |
7c673cae FG |
148 | ? (runtime_config::save_pattern() ? PATTERN_FILE_NAME : "./baseline-outputs/" PATTERN_FILE_NAME ) |
149 | : framework::master_test_suite().argv[1] ); | |
150 | ||
b32b8144 FG |
151 | output_test_stream_for_loggers test_output( pattern_file_name, |
152 | !runtime_config::save_pattern(), | |
153 | true, | |
154 | __FILE__ ); | |
7c673cae FG |
155 | |
156 | #line 207 | |
157 | test_suite* ts_0 = BOOST_TEST_SUITE( "0 test cases inside" ); | |
158 | ||
159 | test_suite* ts_1 = BOOST_TEST_SUITE( "1 test cases inside" ); | |
160 | ts_1->add( BOOST_TEST_CASE( good_foo ) ); | |
161 | ||
162 | test_suite* ts_1b = BOOST_TEST_SUITE( "1 bad test case inside" ); | |
163 | ts_1b->add( BOOST_TEST_CASE( bad_foo ), 1 ); | |
164 | ||
165 | test_suite* ts_1c = BOOST_TEST_SUITE( "1 almost good test case inside" ); | |
166 | ts_1c->add( BOOST_TEST_CASE( almost_good_foo ) ); | |
167 | ||
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 ); | |
171 | ||
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 ); | |
175 | ts_3->add( tc1 ); | |
11fdf7f2 | 176 | test_case* tc2 = BOOST_TEST_CASE( bad_foo2 ); |
7c673cae FG |
177 | ts_3->add( tc2 ); |
178 | tc2->depends_on( tc1 ); | |
179 | ||
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 ) ); | |
11fdf7f2 | 184 | ts_4->add( BOOST_TEST_CASE( bad_foo2 ) ); |
7c673cae FG |
185 | |
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 ); | |
192 | ||
92f5a8d4 TL |
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 ); | |
197 | ||
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" ) ); | |
206 | ||
7c673cae FG |
207 | check( test_output, ts_1 ); |
208 | ||
209 | check( test_output, ts_1b ); | |
210 | ||
211 | check( test_output, ts_1c ); | |
212 | ||
213 | check( test_output, ts_2 ); | |
214 | ||
215 | check( test_output, ts_3 ); | |
216 | ||
217 | check( test_output, ts_4 ); | |
218 | ||
219 | ts_1->add( BOOST_TEST_CASE( bad_foo ) ); | |
220 | ts_3->depends_on( ts_1 ); | |
221 | ||
222 | check( test_output, ts_main ); | |
92f5a8d4 TL |
223 | |
224 | check( test_output, ts_timeout ); | |
225 | ||
226 | check( test_output, ts_timeout_nested ); | |
7c673cae FG |
227 | } |
228 | ||
229 | //____________________________________________________________________________// | |
230 | ||
11fdf7f2 TL |
231 | BOOST_AUTO_TEST_CASE( test_logs_junit_info_closing_tags ) |
232 | { | |
233 | guard G; | |
92f5a8d4 | 234 | boost::ignore_unused( G ); |
11fdf7f2 TL |
235 | |
236 | #define PATTERN_FILE_NAME_JUNIT "log-formatter-test.pattern.junit" | |
237 | ||
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] ); | |
242 | ||
243 | output_test_stream_for_loggers test_output( pattern_file_name, | |
244 | !runtime_config::save_pattern(), | |
245 | true, | |
246 | __FILE__ ); | |
247 | ||
248 | #line 218 | |
249 | test_suite* ts_main = BOOST_TEST_SUITE( "1 test cases inside" ); | |
250 | ts_main->add( BOOST_TEST_CASE( almost_good_foo ) ); | |
251 | ||
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 ); | |
257 | ||
258 | check( test_output, OF_JUNIT, ts_main->p_id, log_successful_tests ); | |
92f5a8d4 TL |
259 | |
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 ); | |
264 | ||
265 | check( test_output, OF_JUNIT, ts_timeout->p_id, log_successful_tests ); | |
266 | ||
267 | ||
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 ) ); | |
273 | ||
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 ); | |
278 | ||
279 | check( test_output, OF_JUNIT, ts_account_failures->p_id, log_messages ); | |
11fdf7f2 TL |
280 | } |
281 | ||
7c673cae | 282 | // EOF |