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

thetic / mu.tiny / 34079598885

07 Sep 2026 03:25AM UTC coverage: 98.947% (+0.02%) from 98.928%
34079598885

push

github

web-flow
Merge pull request #102 from thetic/junit-interleaved-groups

Merge interleaved JUnit test groups into one testsuite block

125 of 126 new or added lines in 1 file covered. (99.21%)

5358 of 5415 relevant lines covered (98.95%)

3312.1 hits per line

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

96.9
/src/test/JUnitOutput.cpp
1
#include "mu/tiny/test/JUnitOutput.hpp"
2

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

8
#include "mu/tiny/time.hpp"
9

10
#include <inttypes.h>
11

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

16
namespace {
17
constexpr uint_least64_t ms_per_s{ 1000 };
18

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

27
} // namespace
28

29
class JUnitTestCaseResultNode
30
{
31
public:
32
  JUnitTestCaseResultNode() = default;
68 ✔
33

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

48
class JUnitTestGroupResult
49
{
50
public:
51
  JUnitTestGroupResult() = default;
54 ✔
52

53
  unsigned int test_count{ 0 };
54
  unsigned int failure_count{ 0 };
55
  unsigned int error_count{ 0 };
56
  unsigned int skip_count{ 0 };
57
  uint_least64_t group_exec_time{ 0 };
58
  String group;
59
  JUnitTestCaseResultNode* head{ nullptr };
60
  JUnitTestCaseResultNode* tail{ nullptr };
61
  JUnitTestGroupResult* next{ nullptr };
62
};
63

64
class JUnitTestOutputImpl
65
{
66
public:
67
  // Every group seen this run, keyed by name, in order of first
68
  // appearance. A group visited more than once (e.g. under `-s` shuffle,
69
  // which can interleave groups) keeps appending to the same bucket
70
  // instead of starting a new one, so its tests all land in a single
71
  // <testsuite> block.
72
  JUnitTestGroupResult* groups{ nullptr };
73
  // The bucket for the group currently running, or nullptr between
74
  // groups / when no test in the current group has started yet.
75
  JUnitTestGroupResult* current{ nullptr };
76
  unsigned int last_global_check_count{ 0 };
77
  String package;
78
  String start_timestamp;
79
};
80

81
JUnitOutput::JUnitOutput()
59 ✔
82
  : impl_(new JUnitTestOutputImpl)
59 ✔
83
{
84
}
59 ✔
85

86
JUnitOutput::~JUnitOutput()
118 ✔
87
{
88
  reset_test_group_result();
59 ✔
89
  delete impl_;
59 ✔
90
}
118 ✔
91

92
JUnitTestGroupResult* JUnitOutput::find_or_create_group(const String& group)
68 ✔
93
{
94
  JUnitTestGroupResult* cur = impl_->groups;
68 ✔
95
  JUnitTestGroupResult* last = nullptr;
68 ✔
96
  while (cur != nullptr) {
75 ✔
97
    if (cur->group == group) {
21 ✔
98
      return cur;
14 ✔
99
    }
100
    last = cur;
7 ✔
101
    cur = cur->next;
7 ✔
102
  }
103

104
  auto* created = new JUnitTestGroupResult;
54 ✔
105
  created->group = group;
54 ✔
106
  if (last != nullptr) {
54 ✔
107
    last->next = created;
7 ✔
108
  } else {
109
    impl_->groups = created;
47 ✔
110
  }
111
  return created;
54 ✔
112
}
113

114
void JUnitOutput::reset_test_group_result()
106 ✔
115
{
116
  JUnitTestGroupResult* group = impl_->groups;
106 ✔
117
  while (group != nullptr) {
160 ✔
118
    JUnitTestCaseResultNode* cur = group->head;
54 ✔
119
    while (cur != nullptr) {
122 ✔
120
      JUnitTestCaseResultNode* tmp = cur->next;
68 ✔
121
      delete cur->failure;
68 ✔
122
      TestProperty* prop = cur->properties;
68 ✔
123
      while (prop != nullptr) {
73 ✔
124
        TestProperty* prop_tmp = prop->next;
5 ✔
125
        delete prop;
5 ✔
126
        prop = prop_tmp;
5 ✔
127
      }
128
      delete cur;
68 ✔
129
      cur = tmp;
68 ✔
130
    }
131
    JUnitTestGroupResult* next = group->next;
54 ✔
132
    delete group;
54 ✔
133
    group = next;
54 ✔
134
  }
135
  impl_->groups = nullptr;
106 ✔
136
  impl_->current = nullptr;
106 ✔
137
}
106 ✔
138

139
void JUnitOutput::print_tests_started()
47 ✔
140
{
141
  reset_test_group_result();
47 ✔
142
  impl_->last_global_check_count = 0;
47 ✔
143
  impl_->start_timestamp = get_time_string();
47 ✔
144
}
47 ✔
145

146
void JUnitOutput::print_current_group_started(const Shell& /*test*/)
55 ✔
147
{
148
  impl_->current = nullptr;
55 ✔
149
}
55 ✔
150

151
void JUnitOutput::print_current_test_ended(const Result& result)
68 ✔
152
{
153
  const unsigned int global_check_count = result.get_check_count();
68 ✔
154
  impl_->current->tail->exec_time =
136 ✔
155
      result.get_current_test_total_execution_time();
68 ✔
156
  impl_->current->tail->check_count =
68 ✔
157
      global_check_count - impl_->last_global_check_count;
68 ✔
158
  impl_->last_global_check_count = global_check_count;
68 ✔
159
}
68 ✔
160

161
void JUnitOutput::print_tests_ended(const Result& /*result*/)
47 ✔
162
{
163
  unsigned int total_test_count = 0;
47 ✔
164
  unsigned int total_failure_count = 0;
47 ✔
165
  unsigned int total_error_count = 0;
47 ✔
166
  unsigned int total_skip_count = 0;
47 ✔
167
  uint_least64_t total_exec_time = 0;
47 ✔
168
  for (JUnitTestGroupResult* g = impl_->groups; g != nullptr; g = g->next) {
101 ✔
169
    total_test_count += g->test_count;
54 ✔
170
    total_failure_count += g->failure_count;
54 ✔
171
    total_error_count += g->error_count;
54 ✔
172
    total_skip_count += g->skip_count;
54 ✔
173
    total_exec_time += g->group_exec_time;
54 ✔
174
  }
175

176
  Output::File file = fopen_(create_file_name().c_str(), "w");
47 ✔
177
  String header = string_from_format(
178
      "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"
179
      "<testsuites tests=\"%d\" failures=\"%d\" errors=\"%d\" "
180
      "skipped=\"%d\" time=\"%d.%03d\" timestamp=\"%s\">\n",
181
      static_cast<int>(total_test_count),
182
      static_cast<int>(total_failure_count),
183
      static_cast<int>(total_error_count),
184
      static_cast<int>(total_skip_count),
185
      static_cast<int>(total_exec_time / ms_per_s),
47 ✔
186
      static_cast<int>(total_exec_time % ms_per_s),
47 ✔
187
      impl_->start_timestamp.c_str()
47 ✔
188
  );
47 ✔
189
  fputs_(header.c_str(), file);
47 ✔
190

191
  for (JUnitTestGroupResult* g = impl_->groups; g != nullptr; g = g->next) {
101 ✔
192
    write_test_group_to_file(file, g);
54 ✔
193
  }
194

195
  fputs_("</testsuites>\n", file);
47 ✔
196
  fclose_(file);
47 ✔
197
}
47 ✔
198

199
void JUnitOutput::print_current_group_ended(const Result& result)
55 ✔
200
{
201
  if (impl_->current == nullptr) {
55 ✔
202
    return;
×
203
  }
204
  impl_->current->group_exec_time +=
55 ✔
205
      result.get_current_group_total_execution_time();
55 ✔
206
  impl_->current = nullptr;
55 ✔
207
}
208

209
void JUnitOutput::print_current_test_started(const Shell& test)
68 ✔
210
{
211
  impl_->current = find_or_create_group(test.get_group());
68 ✔
212
  impl_->current->test_count++;
68 ✔
213

214
  if (impl_->current->tail == nullptr) {
68 ✔
215
    impl_->current->head = impl_->current->tail = new JUnitTestCaseResultNode;
54 ✔
216
  } else {
217
    impl_->current->tail->next = new JUnitTestCaseResultNode;
14 ✔
218
    impl_->current->tail = impl_->current->tail->next;
14 ✔
219
  }
220
  impl_->current->tail->name = test.get_name();
68 ✔
221
  impl_->current->tail->file = test.get_file();
68 ✔
222
  impl_->current->tail->line_number = test.get_line_number();
68 ✔
223
  if (!test.will_run()) {
68 ✔
224
    impl_->current->tail->skipped = true;
1 ✔
225
    impl_->current->tail->skip_message = test.get_macro_name();
1 ✔
226
    impl_->current->skip_count++;
1 ✔
227
  }
228
}
68 ✔
229

230
String JUnitOutput::create_file_name()
47 ✔
231
{
232
  if (!impl_->package.empty()) {
47 ✔
233
    return encode_file_name(impl_->package) + ".xml";
20 ✔
234
  }
235
  return "mutiny.xml";
37 ✔
236
}
237

238
String JUnitOutput::encode_file_name(const String& file_name)
10 ✔
239
{
240
  // special character list based on: https://en.wikipedia.org/wiki/Filename
241
  static const char* const forbidden_characters = "/\\?%*:|\"<>";
242

243
  String result = file_name;
10 ✔
244
  for (const char* sym = forbidden_characters; *sym != 0; ++sym) {
110 ✔
245
    string_replace(result, *sym, '_');
100 ✔
246
  }
247
  return result;
10 ✔
248
}
×
249

250
void JUnitOutput::set_package_name(const String& package)
10 ✔
251
{
252
  if (impl_ != nullptr) {
10 ✔
253
    impl_->package = package;
10 ✔
254
  }
255
}
10 ✔
256

257
void JUnitOutput::write_test_suite_summary(
54 ✔
258
    Output::File file,
259
    JUnitTestGroupResult* group
260
)
261
{
262
  unsigned int total_assertions = 0;
54 ✔
263
  for (JUnitTestCaseResultNode* n = group->head; n != nullptr; n = n->next) {
122 ✔
264
    total_assertions += n->check_count;
68 ✔
265
  }
266

267
  String buf = string_from_format(
268
      "<testsuite errors=\"%d\" failures=\"%d\" skipped=\"%d\" "
269
      "assertions=\"%d\" name=\"%s\" tests=\"%d\" "
270
      "time=\"%d.%03d\" timestamp=\"%s\">\n",
271
      static_cast<int>(group->error_count),
54 ✔
272
      static_cast<int>(group->failure_count),
54 ✔
273
      static_cast<int>(group->skip_count),
54 ✔
274
      static_cast<int>(total_assertions),
275
      group->group.c_str(),
276
      static_cast<int>(group->test_count),
54 ✔
277
      static_cast<int>(group->group_exec_time / ms_per_s),
54 ✔
278
      static_cast<int>(group->group_exec_time % ms_per_s),
54 ✔
279
      get_time_string()
280
  );
54 ✔
281
  write_to_file(file, buf.c_str());
54 ✔
282
}
54 ✔
283

284
String JUnitOutput::encode_xml_text(const String& textbody)
46 ✔
285
{
286
  String buf = textbody.c_str();
46 ✔
287
  string_replace(buf, "&", "&amp;");
46 ✔
288
  string_replace(buf, "\"", "&quot;");
46 ✔
289
  string_replace(buf, "<", "&lt;");
46 ✔
290
  string_replace(buf, ">", "&gt;");
46 ✔
291
  string_replace(buf, "\r", "&#13;");
46 ✔
292
  string_replace(buf, "\n", "&#10;");
46 ✔
293
  return buf;
46 ✔
294
}
×
295

296
void JUnitOutput::write_test_cases(
54 ✔
297
    Output::File file,
298
    JUnitTestGroupResult* group
299
)
300
{
301
  JUnitTestCaseResultNode* cur = group->head;
54 ✔
302

303
  while (cur != nullptr) {
122 ✔
304
    String buf = string_from_format(
305
        "<testcase classname=\"%s%s%s\" name=\"%s\" assertions=\"%d\" "
306
        "time=\"%d.%03d\" file=\"%s\" line=\"%" PRIdLEAST32 "\">\n",
307
        impl_->package.c_str(),
68 ✔
308
        impl_->package.empty() ? "" : ".",
136 ✔
309
        group->group.c_str(),
310
        cur->name.c_str(),
311
        static_cast<int>(cur->check_count),
68 ✔
312
        static_cast<int>(cur->exec_time / ms_per_s),
68 ✔
313
        static_cast<int>(cur->exec_time % ms_per_s),
68 ✔
314
        cur->file.c_str(),
315
        cur->line_number
316
    );
68 ✔
317
    write_to_file(file, buf.c_str());
68 ✔
318

319
    if (cur->properties != nullptr) {
68 ✔
320
      write_to_file(file, "<properties>\n");
4 ✔
321
      for (TestProperty* prop = cur->properties; prop != nullptr;
9 ✔
322
           prop = prop->next) {
5 ✔
323
        String prop_buf = string_from_format(
324
            "<property name=\"%s\" value=\"%s\"/>\n",
325
            encode_xml_text(prop->name).c_str(),
5 ✔
326
            encode_xml_text(prop->value).c_str()
10 ✔
327
        );
5 ✔
328
        write_to_file(file, prop_buf.c_str());
5 ✔
329
      }
5 ✔
330
      write_to_file(file, "</properties>\n");
4 ✔
331
    }
332

333
    if (cur->failure != nullptr) {
68 ✔
334
      if (cur->failure_is_error) {
18 ✔
335
        write_error(file, cur);
4 ✔
336
      } else {
337
        write_failure(file, cur);
14 ✔
338
      }
339
    } else if (cur->skipped) {
50 ✔
340
      if (cur->skip_message.empty()) {
4 ✔
NEW
341
        write_to_file(file, "<skipped />\n");
×
342
      } else {
343
        write_to_file(
4 ✔
344
            file,
345
            string_from_format(
8 ✔
346
                "<skipped message=\"%s\" />\n",
347
                encode_xml_text(cur->skip_message).c_str()
8 ✔
348
            )
349
                .c_str()
350
        );
351
      }
352
    }
353

354
    write_to_file(file, "</testcase>\n");
68 ✔
355
    cur = cur->next;
68 ✔
356
  }
68 ✔
357
}
54 ✔
358

359
void JUnitOutput::write_failure(
14 ✔
360
    Output::File file,
361
    JUnitTestCaseResultNode* node
362
)
363
{
364
  String failure_file = encode_xml_text(node->failure->get_file_name());
14 ✔
365
  String msg = encode_xml_text(node->failure->get_message());
14 ✔
366
  String buf = string_from_format(
367
      "<failure message=\"%s:%" PRIdLEAST32
368
      ": %s\" type=\"AssertionFailedError\">\n"
369
      "%s:%" PRIdLEAST32 ": %s\n",
370
      failure_file.c_str(),
371
      node->failure->get_failure_line_number(),
14 ✔
372
      msg.c_str(),
373
      failure_file.c_str(),
374
      node->failure->get_failure_line_number(),
14 ✔
375
      msg.c_str()
376
  );
14 ✔
377
  write_to_file(file, buf.c_str());
14 ✔
378
  write_to_file(file, "</failure>\n");
14 ✔
379
}
14 ✔
380

381
void JUnitOutput::write_error(Output::File file, JUnitTestCaseResultNode* node)
4 ✔
382
{
383
  String msg = encode_xml_text(node->failure->get_message());
4 ✔
384
  String buf = string_from_format(
385
      "<error message=\"%s\" type=\"UnexpectedException\">\n"
386
      "%s\n",
387
      msg.c_str(),
388
      msg.c_str()
389
  );
4 ✔
390
  write_to_file(file, buf.c_str());
4 ✔
391
  write_to_file(file, "</error>\n");
4 ✔
392
}
4 ✔
393

394
void JUnitOutput::write_file_ending(Output::File file)
54 ✔
395
{
396
  write_to_file(file, "</testsuite>\n");
54 ✔
397
}
54 ✔
398

399
void JUnitOutput::write_test_group_to_file(
54 ✔
400
    Output::File file,
401
    JUnitTestGroupResult* group
402
)
403
{
404
  write_test_suite_summary(file, group);
54 ✔
405
  write_test_cases(file, group);
54 ✔
406
  write_file_ending(file);
54 ✔
407
}
54 ✔
408

409
void JUnitOutput::print_buffer(const char* /*buffer*/) {}
×
410

411
void JUnitOutput::print_test_property(const char* name, const char* value)
5 ✔
412
{
413
  if (impl_->current->tail == nullptr) {
5 ✔
414
    return;
×
415
  }
416
  auto* prop = new TestProperty;
5 ✔
417
  prop->name = name;
5 ✔
418
  prop->value = value;
5 ✔
419
  if (impl_->current->tail->properties == nullptr) {
5 ✔
420
    impl_->current->tail->properties = prop;
4 ✔
421
    impl_->current->tail->properties_tail = prop;
4 ✔
422
  } else {
423
    impl_->current->tail->properties_tail->next = prop;
1 ✔
424
    impl_->current->tail->properties_tail = prop;
1 ✔
425
  }
426
}
427

428
void JUnitOutput::print_skipped(const char* message)
3 ✔
429
{
430
  if (impl_->current->tail == nullptr) {
3 ✔
431
    return;
×
432
  }
433
  impl_->current->tail->skipped = true;
3 ✔
434
  impl_->current->tail->skip_message = message;
3 ✔
435
  impl_->current->skip_count++;
3 ✔
436
}
437

438
void JUnitOutput::print_failure(const Failure& failure)
18 ✔
439
{
440
  if (impl_->current->tail->failure == nullptr) {
18 ✔
441
    if (failure.is_error()) {
18 ✔
442
      impl_->current->error_count++;
4 ✔
443
      impl_->current->tail->failure_is_error = true;
4 ✔
444
    } else {
445
      impl_->current->failure_count++;
14 ✔
446
    }
447
    impl_->current->tail->failure = new Failure(failure);
18 ✔
448
  }
449
}
18 ✔
450

451
void JUnitOutput::write_to_file(Output::File file, const String& buffer)
297 ✔
452
{
453
  fputs_(buffer.c_str(), file);
297 ✔
454
}
297 ✔
455

456
} // namespace test
457
} // namespace tiny
458
} // 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