From e45ea7ea30561de9a4a30cb6bb1e70f5bbe42c07 Mon Sep 17 00:00:00 2001 From: Sander Vocke Date: Mon, 26 Jun 2023 12:29:13 +0000 Subject: [PATCH] Quick fix of JUnit XML generation. --- include/boost/ut.hpp | 110 ++++++++++++++++++++++++++----------------- 1 file changed, 68 insertions(+), 42 deletions(-) diff --git a/include/boost/ut.hpp b/include/boost/ut.hpp index 531a7411..5ecc806d 100644 --- a/include/boost/ut.hpp +++ b/include/boost/ut.hpp @@ -83,6 +83,7 @@ export import std; #include #include #include +#include #if __has_include() and __has_include() #include #include @@ -1840,11 +1841,17 @@ class reporter_junit { auto on(events::summary) -> void { std::cout.flush(); std::cout.rdbuf(cout_save); + std::ofstream maybe_of; + if (detail::cfg::output_filename != "") { maybe_of = std::ofstream(detail::cfg::output_filename); } + if (report_type_ == JUNIT) { - print_junit_summary(); + print_junit_summary(detail::cfg::output_filename != "" ? maybe_of : std::cout); return; } - print_console_summary(); + print_console_summary( + detail::cfg::output_filename != "" ? maybe_of : std::cout, + detail::cfg::output_filename != "" ? maybe_of : std::cerr + ); } protected: @@ -1860,10 +1867,10 @@ class reporter_junit { } } - void print_console_summary() { + void print_console_summary(std::ostream &out_stream, std::ostream &err_stream) { for (const auto& [suite_name, suite_result] : results_) { if (suite_result.fails) { - std::cerr + err_stream << "\n========================================================" "=======================\n" << "Suite " << suite_name // @@ -1875,7 +1882,7 @@ class reporter_junit { << color_.none << '\n'; std::cerr << std::endl; } else { - std::cout << color_.pass << "Suite '" << suite_name + out_stream << color_.pass << "Suite '" << suite_name << "': all tests passed" << color_.none << " (" << suite_result.assertions << " asserts in " << suite_result.n_tests << " tests)\n"; @@ -1889,58 +1896,77 @@ class reporter_junit { } } - void print_junit_summary() { - // mock junit output: - std::cout << "\n"; - for (const auto& [suite_name, suite_result] : results_) { - std::cout << "( suite_result.run_stop - suite_result.run_start) .count(); - std::cout << " time=\"" << (static_cast(time_ms) / 1000.0) - << '\"'; - std::cout << " version=\"" << BOOST_UT_VERSION << "\" />\n"; - print_result(suite_name, " ", suite_result); - std::cout << "\n"; - std::cout.flush(); + return static_cast(time_ms) / 1000.0; + }; + for (const auto& [suite_name, suite_result] : results_) { + n_tests += suite_result.assertions; + n_fails += suite_result.fails; + total_time += suite_time(suite_result); } - } - void print_result(const std::string& suite_name, std::string indent, + + // mock junit output: + stream << "\n"; + stream << "\n"; + + for (const auto& [suite_name, suite_result] : results_) { + stream << "\n"; + print_result(stream, suite_name, " ", suite_result); + stream << "\n"; + stream.flush(); + } + stream << ""; + } + void print_result(std::ostream &stream, const std::string& suite_name, std::string indent, const test_result& parent) { for (const auto& [name, result] : *parent.nested_tests) { - std::cout << indent; - std::cout << "( result.run_stop - result.run_start) .count(); - std::cout << " time=\"" << (static_cast(time_ms) / 1000.0) + stream << " time=\"" << (static_cast(time_ms) / 1000.0) << "\""; - std::cout << " status=\"" << result.status << '\"'; + stream << " status=\"" << result.status << '\"'; if (result.report_string.empty() && result.nested_tests->empty()) { - std::cout << " />\n"; + stream << " />\n"; } else if (!result.nested_tests->empty()) { - std::cout << " />\n"; - print_result(suite_name, indent + " ", result); - std::cout << indent << "\n"; + stream << " />\n"; + print_result(stream, suite_name, indent + " ", result); + stream << indent << "\n"; } else if (!result.report_string.empty()) { - std::cout << ">\n"; - std::cout << indent << indent << "\n"; - std::cout << result.report_string << "\n"; - std::cout << indent << indent << "\n"; - std::cout << indent << "\n"; + stream << ">\n"; + stream << indent << indent << "\n"; + stream << result.report_string << "\n"; + stream << indent << indent << "\n"; + stream << indent << "\n"; } } }