209 {
210 std::regex enabled(".*"), disabled;
211 bool list_tests = false;
212 std::string output_xml;
213 for(int n = 1; n < argc; n++)
214 {
215
216 if(argv[n][0] == '-' && argv[n][1] == '-')
217 {
218 if(strstr(argv[n] + 2, "help"))
219 {
220 std::cout << "\nQuickCppLib minimal unit test framework\n\n"
221 << "Usage: " << argv[0]
222 << " [options] [<regex for tests to run, defaults to .*>] [-<regex for tests to not run>]\n\n"
223 << " --help : Prints this help\n"
224 << " --list-tests : List matching tests\n"
225 << " --reporter <format> : Reporter to use, only format possible is junit\n"
226 << " --out <filename> : Write JUnit XML test report to filename\n"
227 << std::endl;
228 return 0;
229 }
230 else if(strstr(argv[n] + 2, "list-tests"))
231 {
232 list_tests = true;
233 }
234 else if(strstr(argv[n] + 2, "reporter"))
235 {
236 if(n + 1 >= argc || strcmp(argv[n + 1], "junit"))
237 {
238 std::cerr << "--reporter must be followed by 'junit'" << std::endl;
239 return 1;
240 }
241 n++;
242 }
243 else if(strstr(argv[n] + 2, "out"))
244 {
245 if(n + 1 >= argc)
246 {
247 std::cerr << "--out must be followed by the output filename" << std::endl;
248 return 1;
249 }
250 output_xml = argv[n + 1];
251 n++;
252 }
253 }
254 else
255 {
256
257 if(argv[n][0] == '-')
258 disabled.assign(argv[n] + 1);
259 else
260 enabled.assign(argv[n]);
261 }
262 }
263 if(list_tests)
264 {
265 size_t maxname = 0;
267 {
268 for(const auto &i : j.test_cases)
269 {
270 if(strlen(i.name) > maxname)
271 maxname = strlen(i.name);
272 }
273 }
275 {
276 std::cout << "\n" << j.name << ":\n";
277 for(const auto &i : j.test_cases)
278 {
279 if(std::regex_match(i.name, enabled) && !std::regex_match(i.name, disabled))
280 {
281 std::string padding(maxname - strlen(i.name), ' ');
282 std::cout << " " << i.name << padding << " (" << i.desc << ")\n";
283 }
284 }
285 }
286 std::cout << std::endl;
287 return 0;
288 }
290 {
291 for(auto &i : j.test_cases)
292 {
293 if(std::regex_match(i.name, enabled) && !std::regex_match(i.name, disabled))
294 {
296 std::cout << std::endl <<
bold <<
blue << i.name <<
white <<
" : " << i.desc <<
normal << std::endl;
297 std::chrono::steady_clock::time_point begin, end;
298#ifdef __cpp_exceptions
299 try
300 {
301#else
303 {
304 end = std::chrono::steady_clock::now();
305 i.requirement_failed = true;
306 }
307 else
308#endif
309 {
310 begin = std::chrono::steady_clock::now();
311 i.func();
312 end = std::chrono::steady_clock::now();
313 }
314#ifdef __cpp_exceptions
315 }
317 {
318 end = std::chrono::steady_clock::now();
319 i.requirement_failed = true;
320 }
321 catch(const std::exception &e)
322 {
323 end = std::chrono::steady_clock::now();
324 ++i.fails;
325 std::cerr <<
red <<
"FAILURE: std::exception '" << e.what() <<
"' thrown out of test case" <<
normal
326 << std::endl;
327 }
328 catch(...)
329 {
330 end = std::chrono::steady_clock::now();
331 ++i.fails;
332 std::cerr <<
red <<
"FAILURE: Exception thrown out of test case" <<
normal << std::endl;
333 }
334#endif
335 i.duration = end - begin;
336 if(i.passes)
337 std::cout <<
green << i.passes <<
" checks passed ";
338 if(i.fails)
339 std::cout <<
red << i.fails <<
" checks failed ";
340 std::cout <<
normal <<
"duration "
341 << std::chrono::duration_cast<std::chrono::milliseconds>(i.duration).count() << " ms" << std::endl;
342 }
343 else
344 i.skipped = true;
345 }
346 }
348 std::ofstream oh;
349 if(!output_xml.empty())
350 {
351 oh.open(output_xml);
352 oh << R"(<?xml version="1.0" encoding="UTF-8"?>
353<testsuites>
354)";
355 }
356 size_t totalpassed = 0, totalfailed = 0, totalskipped = 0, totalselfskipped = 0;
357 double totaltime = 0;
359 {
360 size_t passed = 0, failed = 0, skipped = 0;
361 double time = 0;
362 for(const auto &i : j.test_cases)
363 {
364 if(i.skipped)
365 ++skipped;
366 else if(i.fails)
367 ++failed;
368 else
369 ++passed;
370 if(i.self_skipped)
371 ++totalselfskipped;
372 time += std::chrono::duration_cast<std::chrono::duration<double>>(i.duration).count();
373 }
374 if(!output_xml.empty())
375 {
376 oh << " <testsuite name=\"" << j.name << "\" errors=\"" << 0 << "\" failures=\"" << failed << "\" skipped=\""
377 << skipped << "\" tests=\"" << j.test_cases.size() << "\" hostname=\"\" time=\"" << time
378 << "\" timestamp=\"\">\n";
379 for(const auto &i : j.test_cases)
380 {
381 oh << " <testcase classname=\"\" name=\"" << i.name << "\" time=\""
382 << std::chrono::duration_cast<std::chrono::duration<double>>(i.duration).count() << "\">";
383 if(i.skipped)
384 {
385
386
387 oh << "<skipped";
388 if(i.self_skipped && i.skip_reason != nullptr)
389 {
390 oh << " message=\"";
391 for(const char *p = i.skip_reason; *p != 0; ++p)
392 {
393 switch(*p)
394 {
395 case '&':
396 oh << "&";
397 break;
398 case '<':
399 oh << "<";
400 break;
401 case '>':
402 oh << ">";
403 break;
404 case '"':
405 oh << """;
406 break;
407 default:
408 oh << *p;
409 break;
410 }
411 }
412 oh << "\"";
413 }
414 oh << "/>";
415 }
416 else if(i.fails)
417 oh << "<failure/>";
418 oh << "</testcase>\n";
419 }
420 oh << " </testsuite>\n";
421 }
422 totalpassed += passed;
423 totalfailed += failed;
424 totalskipped += skipped;
425 totaltime += time;
426 }
427 if(!output_xml.empty())
428 {
429 oh << R"(</testsuites>
430)";
431 }
432 std::cout << bold << white << "\n\nTest case summary: " <<
green << totalpassed <<
" passed " <<
red << totalfailed
433 <<
" failed " <<
yellow << totalskipped <<
" skipped" <<
normal <<
"\nTotal duration: " << totaltime
434 << " seconds." << std::endl;
435
436
437
438
439
440
441
442
443 if(totalpassed + totalfailed == 0 && totalselfskipped == 0)
444 {
445
446
447
448
449 std::cerr <<
red <<
"FAILURE: no test case ran (" << totalskipped
450 <<
" skipped) - empty suite, or a name filter that matched nothing" <<
normal << std::endl;
451 return true;
std::ostream & blue(std::ostream &s)
Makes the text on the console blue.
Definition console_colours.hpp:143
std::ostream & green(std::ostream &s)
Makes the text on the console green.
Definition console_colours.hpp:141
std::ostream & bold(std::ostream &s)
Makes the text on the console bold.
Definition console_colours.hpp:153
std::ostream & white(std::ostream &s)
Makes the text on the console white.
Definition console_colours.hpp:151
std::ostream & red(std::ostream &s)
Makes the text on the console red.
Definition console_colours.hpp:139
std::vector< test_suite > & test_suites()
Definition unit_test.hpp:173
jmp_buf & test_case_failed()
Definition unit_test.hpp:112
Definition unit_test.hpp:109