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 class reset_status
: public test_tree_visitor
{
67 virtual bool visit( test_unit
const& tu
)
69 const_cast<test_unit
&>(tu
).p_default_status
.value
= test_case::RS_INHERIT
;
70 const_cast<test_unit
&>(tu
).p_run_status
.value
= test_case::RS_INVALID
;
76 log_setup_teardown
holder(output
, log_format
, ll
);
78 // reinit the default/run status otherwise we cannot apply the decorators
79 // after the first run
80 traverse_test_tree( id
, rstatus
, true );
81 framework::get
<test_suite
>(id
).p_default_status
.value
= test_unit::RS_ENABLED
;
83 output
<< "* " << log_format
<< "-format *******************************************************************";
86 framework::finalize_setup_phase( id
);
87 framework::run( id
, false ); // do not continue the test tree to have the test_log_start/end
91 BOOST_TEST( output
.match_pattern(true) ); // flushes the stream at the end of the comparison.
94 //____________________________________________________________________________//
96 void check( output_test_stream
& output
, test_suite
* ts
)
98 check( output
, OF_CLF
, ts
->p_id
);
99 check( output
, OF_XML
, ts
->p_id
);
100 check( output
, OF_JUNIT
, ts
->p_id
, log_successful_tests
);
101 check( output
, OF_JUNIT
, ts
->p_id
, log_cpp_exception_errors
);
107 boost::unit_test::unit_test_log
.set_format( runtime_config::get
<output_format
>( runtime_config::btrt_log_format
) );
108 boost::unit_test::unit_test_log
.set_stream( std::cout
);
113 BOOST_AUTO_TEST_CASE( test_logs
)
116 boost::ignore_unused( G
);
118 #define PATTERN_FILE_NAME "log-count-skipped-tests.pattern"
120 std::string
pattern_file_name(
121 framework::master_test_suite().argc
<= 1
122 ? (runtime_config::save_pattern() ? PATTERN_FILE_NAME
: "./baseline-outputs/" PATTERN_FILE_NAME
)
123 : framework::master_test_suite().argv
[1] );
125 output_test_stream_for_loggers
test_output( pattern_file_name
,
126 !runtime_config::save_pattern(),
130 test_case
* tc1
= BOOST_TEST_CASE(test_1
);
131 test_case
* tc2
= BOOST_TEST_CASE(test_2
);
132 test_case
* tc3
= BOOST_TEST_CASE(test_3
);
134 // add decorators to the tests, should happen only once. The status will be reset in the check.
135 decorator::collector_t
* decorator_collector
= &(*utf::precondition(skip_with_message("-some precondition-")));
136 decorator_collector
->store_in( *tc2
);
137 decorator_collector
->reset();
139 decorator_collector
= &(* utf::disabled());
140 decorator_collector
->store_in( *tc3
);
141 decorator_collector
->reset();
143 test_suite
* ts_main
= BOOST_TEST_SUITE( "fake master test suite" );
148 check( test_output
, ts_main
);
150 // change precondition
151 skip_with_message::default_enabled
= true;
152 check( test_output
, ts_main
);
155 // disabling sub suites and subtests
156 test_suite
* ts_main2
= BOOST_TEST_SUITE( "fake master test suite2" );
157 ts_main2
->add( tc1
); // active
158 ts_main2
->add( tc2
); // conditionally disabled
159 ts_main2
->add( tc3
); // disabled
161 // all disabled: count increases by 2
162 test_suite
* ts_sub1
= BOOST_TEST_SUITE( "child1" );
163 ts_sub1
->add( BOOST_TEST_CASE_NAME(test_1
, "t1"));
164 ts_sub1
->add( BOOST_TEST_CASE_NAME(test_1
, "t2"));
166 test_case
* tc_2_1
= BOOST_TEST_CASE(test_1
); // disabled
167 test_suite
* ts_sub2
= BOOST_TEST_SUITE( "child2" ); // conditionally disabled
168 ts_sub2
->add( tc_2_1
);
169 ts_sub2
->add( BOOST_TEST_CASE_NAME(test_1
, "t2"));
171 ts_main2
->add(ts_sub1
);
172 ts_main2
->add(ts_sub2
);
175 decorator_collector
= &(* utf::disabled());
176 decorator_collector
->store_in( *ts_sub1
);
177 decorator_collector
->reset();
179 decorator_collector
= &(* utf::disabled());
180 decorator_collector
->store_in( *tc_2_1
);
181 decorator_collector
->reset();
183 decorator_collector
= &(*utf::precondition(skip_with_message("-some precondition-")));
184 decorator_collector
->store_in( *ts_sub2
);
185 decorator_collector
->reset();
188 skip_with_message::default_enabled
= false;
189 check( test_output
, ts_main2
);
190 // count disabled = 2 (main) + 2 (ts_sub1) + 2 (ts_sub2)
192 // change precondition
193 skip_with_message::default_enabled
= true;
194 check( test_output
, ts_main2
);
195 // count disabled = 1 (main) + 2 (ts_sub1) + 1 (ts_sub2)