25 explicit ReporterXml (std::ostream& ostream = std::cout);
28 void Clear ()
override;
36 std::string xml_escape (
const std::string& value);
37 std::string build_failure_message (
const std::string& file,
int line, std::string
const& message);
43 std::chrono::system_clock::time_point start_time;
48std::string ReporterXml::xml_escape (
const std::string& value)
51 std::string escaped = value;
52 auto replace_char = [&escaped] (
char c,
const char* repl){
53 for (
auto pos = escaped.find(c); pos != std::string::npos; pos = escaped.find(c, pos + 1))
54 escaped.replace(pos, 1, repl);
57 replace_char (
'&',
"&");
58 replace_char (
'<',
"<");
59 replace_char (
'>',
">");
60 replace_char (
'\'',
"'");
61 replace_char (
'\"',
""");
67std::string ReporterXml::build_failure_message (
const std::string& file,
int line, std::string
const& message)
69 std::ostringstream failureMessage;
70 failureMessage << file <<
"(" << line <<
") : " << message;
71 return failureMessage.str();
82 , orig_state (nullptr)
84 start_time = std::chrono::system_clock::now();
85 orig_state.copyfmt (os);
86 os <<
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>" << std::endl;
94 using namespace std::chrono;
97 os.copyfmt (orig_state);
98 auto beg_time = time_point_cast<std::chrono::seconds>(start_time);
99 auto end_time = time_point_cast<std::chrono::seconds>(std::chrono::system_clock::now ());
100 auto total_time_s = duration_cast<duration<float, std::chrono::seconds::period>>(
total_time);
102 os <<
"<utpp-results"
105 <<
" failures=\"" <<
total_failures_count <<
'\"' <<
" duration=\"" << std::fixed << std::setprecision (3)
106#if _MSVC_LANG >= 202002L
107 << total_time_s <<
'\"'
109 << total_time_s.count() <<
"s\""
112#if _MSVC_LANG >= 202002L
113 os <<
" <start-time>" << std::format(
"{0:%F} {0:%T}Z", beg_time) <<
"</start-time>" << std::endl;
117 time_t t = system_clock::to_time_t (start_time);
118 timeinfo = gmtime (&t);
119 strftime (buffer,
sizeof(buffer),
"%F %TZ", timeinfo);
120 os <<
" <start-time>" << buffer <<
"</start-time>" << std::endl;
125 std::wstring wcmd{ GetCommandLineW () };
126 int nsz = WideCharToMultiByte (CP_UTF8, 0, wcmd.c_str (), -1, 0, 0, 0, 0);
130 WideCharToMultiByte (CP_UTF8, 0, wcmd.c_str (), -1, &cmd[0], nsz, 0, 0);
131 cmd.resize (nsz - 1);
133 os <<
" <command-line>" << xml_escape (cmd) <<
"</command-line>" << std::endl;
138 if (i->test_name.empty ())
141 os <<
" </suite>" << std::endl;
142 suite = i->suite_name;
146 os <<
" <suite name=\"" << suite <<
'\"';
147 if ((i + 1) ==
results.cend () || (i + 1)->test_name.empty ())
153 os <<
'>' << std::endl;
159 if (!i->failures.empty ())
166 os <<
" </suite>" << std::endl;
167#if _MSVC_LANG >= 202002L
168 os <<
" <end-time>" << std::format (
"{0:%F} {0:%T}Z", end_time) <<
"</end-time>" << std::endl;
170 t = system_clock::to_time_t (end_time);
171 timeinfo = gmtime (&t);
172 strftime (buffer,
sizeof (buffer),
"%F %TZ", timeinfo);
173 os <<
" <end-time>" << buffer <<
"</end-time>" << std::endl;
175 os <<
"</utpp-results>" << std::endl;
183 os <<
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>" << std::endl;
184 start_time = std::chrono::system_clock::now();
193 <<
" name=\"" << result.
test_name <<
"\""
194#if _MSVC_LANG >= 202002L
195 <<
" time=\"" << result.
test_time <<
"\"";
197 <<
" time=\"" << result.
test_time.count() <<
"ms\"";
202void ReporterXml::EndTest (
const ReporterDeferred::TestResult& result)
204 if (result.failures.empty ())
215 os <<
">" << std::endl;
217 for (
auto& fail : result.failures)
219 std::string escapedMessage = xml_escape (fail.message);
220 std::string message = build_failure_message (fail.filename, fail.line_number, escapedMessage);
222 os <<
" <failure" <<
" message=\"" << message <<
"\"" <<
"/>" << std::endl;
void Clear() override
Reset all statistics.
Definition utpp.h:711
std::deque< TestResult > results
Results of all tests.
Definition utpp.h:354
int total_failures_count
total number of failures
Definition utpp.h:322
std::chrono::milliseconds total_time
total running time in milliseconds
Definition utpp.h:323
int total_test_count
total number of tests
Definition utpp.h:320
virtual int Summary()
Generate results report.
Definition utpp.h:309
int total_failed_count
total number of failed tests
Definition utpp.h:321
int Summary() override
Generate XML report.
Definition reporter_xml.h:92
void Clear() override
Reset all statistics.
Definition reporter_xml.h:180
ReporterXml(std::ostream &ostream=std::cout)
Definition reporter_xml.h:80
Test results including all failure messages
Definition utpp.h:344
std::string test_name
test name
Definition utpp.h:349
std::chrono::milliseconds test_time
test running time in milliseconds
Definition utpp.h:350
#define DEFAULT_SUITE
Name of default suite.
Definition utpp.h:59