1 // (C) Copyright Raffi Enficiaud 2019.
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 // ***************************************************************************
12 #define BOOST_TEST_MODULE junit count skipped tests
13 #include <boost/test/included/unit_test.hpp>
15 // our special logger for tests
16 #include "logger-for-tests.hpp"
22 using boost::test_tools::output_test_stream;
23 using namespace boost::unit_test;
24 namespace utf = boost::unit_test;
25 namespace tt = boost::test_tools;
28 struct skip_with_message
30 static bool default_enabled; // to control the status from outside
33 skip_with_message(std::string m)
36 tt::assertion_result operator()(utf::test_unit_id)
38 tt::assertion_result ans(skip_with_message::default_enabled);
39 ans.message() << "test is skipped because " << message;
43 bool skip_with_message::default_enabled = false;
47 BOOST_CHECK_EQUAL( 2 + 2, 4 );
52 BOOST_CHECK_EQUAL( 0, 1 );
57 BOOST_CHECK_EQUAL( 0, 1 );
60 void check( output_test_stream& output,
61 output_format log_format,
63 log_level ll = log_successful_tests )
65 boost::unit_test::unit_test_log.set_format(log_format);
66 boost::unit_test::unit_test_log.set_stream(output);
67 boost::unit_test::unit_test_log.set_threshold_level(ll);
69 class reset_status : public test_tree_visitor {
71 virtual bool visit( test_unit const& tu )
73 const_cast<test_unit&>(tu).p_default_status.value = test_case::RS_INHERIT;
74 const_cast<test_unit&>(tu).p_run_status.value = test_case::RS_INVALID;
79 // reinit the default/run status otherwise we cannot apply the decorators
80 // after the first run
81 traverse_test_tree( id, rstatus, true );
82 framework::get<test_suite>(id).p_default_status.value = test_unit::RS_ENABLED;
84 output << "* " << log_format << "-format *******************************************************************";
87 framework::finalize_setup_phase( id );
88 framework::run( id, false ); // do not continue the test tree to have the test_log_start/end
91 boost::unit_test::unit_test_log.set_format(OF_CLF);
92 boost::unit_test::unit_test_log.set_stream(std::cout);
94 BOOST_TEST( output.match_pattern(true) ); // flushes the stream at the end of the comparison.
97 //____________________________________________________________________________//
99 void check( output_test_stream& output, test_suite* ts )
101 check( output, OF_CLF, ts->p_id );
102 check( output, OF_XML, ts->p_id );
103 check( output, OF_JUNIT, ts->p_id, log_successful_tests );
104 check( output, OF_JUNIT, ts->p_id, log_cpp_exception_errors );
110 boost::unit_test::unit_test_log.set_format( runtime_config::get<output_format>( runtime_config::btrt_log_format ) );
111 boost::unit_test::unit_test_log.set_stream( std::cout );
116 BOOST_AUTO_TEST_CASE( test_logs )
119 boost::ignore_unused( G );
121 #define PATTERN_FILE_NAME "log-count-skipped-tests.pattern"
123 std::string pattern_file_name(
124 framework::master_test_suite().argc <= 1
125 ? (runtime_config::save_pattern() ? PATTERN_FILE_NAME : "./baseline-outputs/" PATTERN_FILE_NAME )
126 : framework::master_test_suite().argv[1] );
128 output_test_stream_for_loggers test_output( pattern_file_name,
129 !runtime_config::save_pattern(),
133 test_case* tc1 = BOOST_TEST_CASE(test_1);
134 test_case* tc2 = BOOST_TEST_CASE(test_2);
135 test_case* tc3 = BOOST_TEST_CASE(test_3);
137 // add decorators to the tests, should happen only once. The status will be reset in the check.
138 decorator::collector_t* decorator_collector = &(*utf::precondition(skip_with_message("-some precondition-")));
139 decorator_collector->store_in( *tc2 );
140 decorator_collector->reset();
142 decorator_collector = &(* utf::disabled());
143 decorator_collector->store_in( *tc3 );
144 decorator_collector->reset();
146 test_suite* ts_main = BOOST_TEST_SUITE( "fake master test suite" );
151 check( test_output, ts_main );
153 // change precondition
154 skip_with_message::default_enabled = true;
155 check( test_output, ts_main );
158 // disabling sub suites and subtests
159 test_suite* ts_main2 = BOOST_TEST_SUITE( "fake master test suite2" );
160 ts_main2->add( tc1 ); // active
161 ts_main2->add( tc2 ); // conditionally disabled
162 ts_main2->add( tc3 ); // disabled
164 // all disabled: count increases by 2
165 test_suite* ts_sub1 = BOOST_TEST_SUITE( "child1" );
166 ts_sub1->add( BOOST_TEST_CASE_NAME(test_1, "t1"));
167 ts_sub1->add( BOOST_TEST_CASE_NAME(test_1, "t2"));
169 test_case* tc_2_1 = BOOST_TEST_CASE(test_1); // disabled
170 test_suite* ts_sub2 = BOOST_TEST_SUITE( "child2" ); // conditionally disabled
171 ts_sub2->add( tc_2_1 );
172 ts_sub2->add( BOOST_TEST_CASE_NAME(test_1, "t2"));
174 ts_main2->add(ts_sub1);
175 ts_main2->add(ts_sub2);
178 decorator_collector = &(* utf::disabled());
179 decorator_collector->store_in( *ts_sub1 );
180 decorator_collector->reset();
182 decorator_collector = &(* utf::disabled());
183 decorator_collector->store_in( *tc_2_1 );
184 decorator_collector->reset();
186 decorator_collector = &(*utf::precondition(skip_with_message("-some precondition-")));
187 decorator_collector->store_in( *ts_sub2 );
188 decorator_collector->reset();
191 skip_with_message::default_enabled = false;
192 check( test_output, ts_main2 );
193 // count disabled = 2 (main) + 2 (ts_sub1) + 2 (ts_sub2)
195 // change precondition
196 skip_with_message::default_enabled = true;
197 check( test_output, ts_main2 );
198 // count disabled = 1 (main) + 2 (ts_sub1) + 1 (ts_sub2)