• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

thetic / mutiny / 24489391918

16 Apr 2026 02:51AM UTC coverage: 98.642% (-0.1%) from 98.753%
24489391918

push

github

web-flow
Merge pull request #56 from thetic/last-exit

Add PASS_TEST/SKIP_TEST macros and improve JUnit XML skipped output

24 of 33 new or added lines in 10 files covered. (72.73%)

5084 of 5154 relevant lines covered (98.64%)

3892.25 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.68
/src/test/JUnitOutput.cpp
1
#include "mutiny/test/JUnitOutput.hpp"
2

3
#include "mutiny/test/Failure.hpp"
4
#include "mutiny/test/Output.hpp"
5
#include "mutiny/test/Result.hpp"
6
#include "mutiny/test/Shell.hpp"
7

8
#include "mutiny/time.hpp"
9

10
#include <stdint.h>
11

12
namespace mu {
13
namespace tiny {
14
namespace test {
15

16
namespace {
17

18
class TestProperty
19
{
20
public:
21
  String name;
22
  String value;
23
  TestProperty* next{ nullptr };
24
};
25

26
} // namespace
27

28
class JUnitTestCaseResultNode
29
{
30
public:
31
  JUnitTestCaseResultNode() = default;
61 ✔
32

33
  String name;
34
  uint_least64_t exec_time{ 0 };
35
  Failure* failure{ nullptr };
36
  bool failure_is_error{ false };
37
  bool ignored{ false };
38
  String skip_message;
39
  String file;
40
  size_t line_number{ 0 };
41
  size_t check_count{ 0 };
42
  TestProperty* properties{ nullptr };
43
  TestProperty* properties_tail{ nullptr };
44
  JUnitTestCaseResultNode* next{ nullptr };
45
};
46

47
class JUnitTestGroupResult
48
{
49
public:
50
  JUnitTestGroupResult() = default;
44 ✔
51

52
  size_t test_count{ 0 };
53
  size_t failure_count{ 0 };
54
  size_t error_count{ 0 };
55
  size_t skip_count{ 0 };
56
  size_t total_check_count{ 0 };
57
  uint_least64_t start_time{ 0 };
58
  uint_least64_t group_exec_time{ 0 };
59
  String group;
60
  JUnitTestCaseResultNode* head{ nullptr };
61
  JUnitTestCaseResultNode* tail{ nullptr };
62
};
63

64
class JUnitTestOutputImpl
65
{
66
public:
67
  JUnitTestGroupResult results;
68
  Output::File file;
69
  String package;
70
  String std_output;
71
};
72

73
JUnitOutput::JUnitOutput()
44 ✔
74
  : impl_(new JUnitTestOutputImpl)
44 ✔
75
{
76
}
44 ✔
77

78
JUnitOutput::~JUnitOutput()
88 ✔
79
{
80
  reset_test_group_result();
44 ✔
81
  delete impl_;
44 ✔
82
}
88 ✔
83

84
void JUnitOutput::reset_test_group_result()
92 ✔
85
{
86
  impl_->results.test_count = 0;
92 ✔
87
  impl_->results.failure_count = 0;
92 ✔
88
  impl_->results.error_count = 0;
92 ✔
89
  impl_->results.skip_count = 0;
92 ✔
90
  impl_->results.group = "";
92 ✔
91
  JUnitTestCaseResultNode* cur = impl_->results.head;
92 ✔
92
  while (cur) {
153 ✔
93
    JUnitTestCaseResultNode* tmp = cur->next;
61 ✔
94
    delete cur->failure;
61 ✔
95
    TestProperty* prop = cur->properties;
61 ✔
96
    while (prop) {
66 ✔
97
      TestProperty* prop_tmp = prop->next;
5 ✔
98
      delete prop;
5 ✔
99
      prop = prop_tmp;
5 ✔
100
    }
101
    delete cur;
61 ✔
102
    cur = tmp;
61 ✔
103
  }
104
  impl_->results.head = nullptr;
92 ✔
105
  impl_->results.tail = nullptr;
92 ✔
106
}
92 ✔
107

108
void JUnitOutput::print_tests_started() {}
43 ✔
109

110
void JUnitOutput::print_current_group_started(const Shell& /*test*/) {}
48 ✔
111

112
void JUnitOutput::print_current_test_ended(const Result& result)
61 ✔
113
{
114
  impl_->results.tail->exec_time =
122 ✔
115
      result.get_current_test_total_execution_time();
61 ✔
116
  impl_->results.tail->check_count = result.get_check_count();
61 ✔
117
}
61 ✔
118

119
void JUnitOutput::print_tests_ended(const Result& /*result*/) {}
43 ✔
120

121
void JUnitOutput::print_current_group_ended(const Result& result)
48 ✔
122
{
123
  impl_->results.group_exec_time =
96 ✔
124
      result.get_current_group_total_execution_time();
48 ✔
125
  write_test_group_to_file();
48 ✔
126
  reset_test_group_result();
48 ✔
127
}
48 ✔
128

129
void JUnitOutput::print_current_test_started(const Shell& test)
61 ✔
130
{
131
  impl_->results.test_count++;
61 ✔
132
  impl_->results.group = test.get_group();
61 ✔
133
  impl_->results.start_time = get_time_in_millis();
61 ✔
134

135
  if (impl_->results.tail == nullptr) {
61 ✔
136
    impl_->results.head = impl_->results.tail = new JUnitTestCaseResultNode;
48 ✔
137
  } else {
138
    impl_->results.tail->next = new JUnitTestCaseResultNode;
13 ✔
139
    impl_->results.tail = impl_->results.tail->next;
13 ✔
140
  }
141
  impl_->results.tail->name = test.get_name();
61 ✔
142
  impl_->results.tail->file = test.get_file();
61 ✔
143
  impl_->results.tail->line_number = test.get_line_number();
61 ✔
144
  if (!test.will_run()) {
61 ✔
145
    impl_->results.tail->ignored = true;
1 ✔
146
    impl_->results.skip_count++;
1 ✔
147
  }
148
}
61 ✔
149

150
String JUnitOutput::create_file_name(const String& group)
49 ✔
151
{
152
  String file_name = "mutiny_";
49 ✔
153
  if (!impl_->package.empty()) {
49 ✔
154
    file_name += impl_->package;
6 ✔
155
    file_name += "_";
6 ✔
156
  }
157
  file_name += group;
49 ✔
158
  return encode_file_name(file_name) + ".xml";
147 ✔
159
}
49 ✔
160

161
String JUnitOutput::encode_file_name(const String& file_name)
49 ✔
162
{
163
  // special character list based on: https://en.wikipedia.org/wiki/Filename
164
  static const char* const forbidden_characters = "/\\?%*:|\"<>";
165

166
  String result = file_name;
49 ✔
167
  for (const char* sym = forbidden_characters; *sym; ++sym) {
539 ✔
168
    string_replace(result, *sym, '_');
490 ✔
169
  }
170
  return result;
49 ✔
171
}
×
172

173
void JUnitOutput::set_package_name(const String& package)
6 ✔
174
{
175
  if (impl_ != nullptr) {
6 ✔
176
    impl_->package = package;
6 ✔
177
  }
178
}
6 ✔
179

180
void JUnitOutput::write_xml_header()
48 ✔
181
{
182
  write_to_file("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n");
48 ✔
183
}
48 ✔
184

185
void JUnitOutput::write_test_suite_summary()
48 ✔
186
{
187
  size_t total_assertions = 0;
48 ✔
188
  for (JUnitTestCaseResultNode* n = impl_->results.head; n; n = n->next)
109 ✔
189
    total_assertions = n->check_count;
61 ✔
190

191
  String buf = string_from_format(
192
      "<testsuite errors=\"%d\" failures=\"%d\" skipped=\"%d\" "
193
      "assertions=\"%d\" name=\"%s\" tests=\"%d\" "
194
      "time=\"%d.%03d\" timestamp=\"%s\">\n",
195
      static_cast<int>(impl_->results.error_count),
48 ✔
196
      static_cast<int>(impl_->results.failure_count),
48 ✔
197
      static_cast<int>(impl_->results.skip_count),
48 ✔
198
      static_cast<int>(total_assertions),
199
      impl_->results.group.c_str(),
48 ✔
200
      static_cast<int>(impl_->results.test_count),
48 ✔
201
      static_cast<int>(impl_->results.group_exec_time / 1000),
48 ✔
202
      static_cast<int>(impl_->results.group_exec_time % 1000),
48 ✔
203
      get_time_string()
204
  );
48 ✔
205
  write_to_file(buf.c_str());
48 ✔
206
}
48 ✔
207

208
void JUnitOutput::write_properties() {}
48 ✔
209

210
String JUnitOutput::encode_xml_text(const String& textbody)
77 ✔
211
{
212
  String buf = textbody.c_str();
77 ✔
213
  string_replace(buf, "&", "&amp;");
77 ✔
214
  string_replace(buf, "\"", "&quot;");
77 ✔
215
  string_replace(buf, "<", "&lt;");
77 ✔
216
  string_replace(buf, ">", "&gt;");
77 ✔
217
  string_replace(buf, "\r", "&#13;");
77 ✔
218
  string_replace(buf, "\n", "&#10;");
77 ✔
219
  return buf;
77 ✔
220
}
×
221

222
void JUnitOutput::write_test_cases()
48 ✔
223
{
224
  JUnitTestCaseResultNode* cur = impl_->results.head;
48 ✔
225

226
  while (cur) {
109 ✔
227
    String buf = string_from_format(
228
        "<testcase classname=\"%s%s%s\" name=\"%s\" assertions=\"%d\" "
229
        "time=\"%d.%03d\" file=\"%s\" line=\"%d\">\n",
230
        impl_->package.c_str(),
61 ✔
231
        impl_->package.empty() ? "" : ".",
122 ✔
232
        impl_->results.group.c_str(),
61 ✔
233
        cur->name.c_str(),
234
        static_cast<int>(cur->check_count - impl_->results.total_check_count),
61 ✔
235
        static_cast<int>(cur->exec_time / 1000),
61 ✔
236
        static_cast<int>(cur->exec_time % 1000),
61 ✔
237
        cur->file.c_str(),
238
        static_cast<int>(cur->line_number)
61 ✔
239
    );
61 ✔
240
    write_to_file(buf.c_str());
61 ✔
241

242
    impl_->results.total_check_count = cur->check_count;
61 ✔
243

244
    if (cur->properties) {
61 ✔
245
      write_to_file("<properties>\n");
4 ✔
246
      for (TestProperty* prop = cur->properties; prop; prop = prop->next) {
9 ✔
247
        String prop_buf = string_from_format(
248
            "<property name=\"%s\" value=\"%s\"/>\n",
249
            encode_xml_text(prop->name).c_str(),
5 ✔
250
            encode_xml_text(prop->value).c_str()
10 ✔
251
        );
5 ✔
252
        write_to_file(prop_buf.c_str());
5 ✔
253
      }
5 ✔
254
      write_to_file("</properties>\n");
4 ✔
255
    }
256

257
    if (cur->failure) {
61 ✔
258
      if (cur->failure_is_error)
17 ✔
259
        write_error(cur);
4 ✔
260
      else
261
        write_failure(cur);
13 ✔
262
    } else if (cur->ignored) {
44 ✔
263
      if (cur->skip_message.empty()) {
3 ✔
264
        write_to_file("<skipped />\n");
1 ✔
265
      } else {
266
        write_to_file(string_from_format(
2 ✔
267
                          "<skipped message=\"%s\" />\n",
268
                          encode_xml_text(cur->skip_message).c_str()
4 ✔
269
        )
270
                          .c_str());
271
      }
272
    }
273
    write_to_file("</testcase>\n");
61 ✔
274
    cur = cur->next;
61 ✔
275
  }
61 ✔
276
}
48 ✔
277

278
void JUnitOutput::write_failure(JUnitTestCaseResultNode* node)
13 ✔
279
{
280
  String msg = encode_xml_text(node->failure->get_message());
13 ✔
281
  String buf = string_from_format(
282
      "<failure message=\"%s:%d: %s\" type=\"AssertionFailedError\">\n"
283
      "%s:%d: %s\n",
284
      node->failure->get_file_name().c_str(),
26 ✔
285
      static_cast<int>(node->failure->get_failure_line_number()),
26 ✔
286
      msg.c_str(),
287
      node->failure->get_file_name().c_str(),
26 ✔
288
      static_cast<int>(node->failure->get_failure_line_number()),
26 ✔
289
      msg.c_str()
290
  );
13 ✔
291
  write_to_file(buf.c_str());
13 ✔
292
  write_to_file("</failure>\n");
13 ✔
293
}
13 ✔
294

295
void JUnitOutput::write_error(JUnitTestCaseResultNode* node)
4 ✔
296
{
297
  String msg = encode_xml_text(node->failure->get_message());
4 ✔
298
  String buf = string_from_format(
299
      "<error message=\"%s\" type=\"UnexpectedException\">\n"
300
      "%s\n",
301
      msg.c_str(),
302
      msg.c_str()
303
  );
4 ✔
304
  write_to_file(buf.c_str());
4 ✔
305
  write_to_file("</error>\n");
4 ✔
306
}
4 ✔
307

308
void JUnitOutput::write_file_ending()
48 ✔
309
{
310
  write_to_file("<system-out>");
48 ✔
311
  write_to_file(encode_xml_text(impl_->std_output));
48 ✔
312
  write_to_file("</system-out>\n");
48 ✔
313
  write_to_file("<system-err></system-err>\n");
48 ✔
314
  write_to_file("</testsuite>\n");
48 ✔
315
}
48 ✔
316

317
void JUnitOutput::write_test_group_to_file()
48 ✔
318
{
319
  open_file_for_write(create_file_name(impl_->results.group));
48 ✔
320
  write_xml_header();
48 ✔
321
  write_test_suite_summary();
48 ✔
322
  write_properties();
48 ✔
323
  write_test_cases();
48 ✔
324
  write_file_ending();
48 ✔
325
  close_file();
48 ✔
326
}
48 ✔
327

328
void JUnitOutput::print_buffer(const char*) {}
×
329

330
void JUnitOutput::print(const char* output)
2 ✔
331
{
332
  impl_->std_output += output;
2 ✔
333
}
2 ✔
334

335
void JUnitOutput::print(long) {}
×
336

337
void JUnitOutput::print(size_t) {}
×
338

339
void JUnitOutput::print_test_property(const char* name, const char* value)
5 ✔
340
{
341
  if (impl_->results.tail == nullptr)
5 ✔
342
    return;
×
343
  auto* prop = new TestProperty;
5 ✔
344
  prop->name = name;
5 ✔
345
  prop->value = value;
5 ✔
346
  if (impl_->results.tail->properties == nullptr) {
5 ✔
347
    impl_->results.tail->properties = prop;
4 ✔
348
    impl_->results.tail->properties_tail = prop;
4 ✔
349
  } else {
350
    impl_->results.tail->properties_tail->next = prop;
1 ✔
351
    impl_->results.tail->properties_tail = prop;
1 ✔
352
  }
353
}
354

355
void JUnitOutput::print_skipped(const char* message)
2 ✔
356
{
357
  if (impl_->results.tail == nullptr)
2 ✔
NEW
358
    return;
×
359
  impl_->results.tail->ignored = true;
2 ✔
360
  impl_->results.tail->skip_message = message;
2 ✔
361
  impl_->results.skip_count++;
2 ✔
362
}
363

364
void JUnitOutput::print_failure(const Failure& failure)
17 ✔
365
{
366
  if (impl_->results.tail->failure == nullptr) {
17 ✔
367
    if (failure.is_error()) {
17 ✔
368
      impl_->results.error_count++;
4 ✔
369
      impl_->results.tail->failure_is_error = true;
4 ✔
370
    } else {
371
      impl_->results.failure_count++;
13 ✔
372
    }
373
    impl_->results.tail->failure = new Failure(failure);
17 ✔
374
  }
375
}
17 ✔
376

377
void JUnitOutput::open_file_for_write(const String& file_name)
48 ✔
378
{
379
  impl_->file = fopen_(file_name.c_str(), "w");
48 ✔
380
}
48 ✔
381

382
void JUnitOutput::write_to_file(const String& buffer)
508 ✔
383
{
384
  fputs_(buffer.c_str(), impl_->file);
508 ✔
385
}
508 ✔
386

387
void JUnitOutput::close_file()
48 ✔
388
{
389
  fclose_(impl_->file);
48 ✔
390
}
48 ✔
391

392
} // namespace test
393
} // namespace tiny
394
} // namespace mu
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc