QuickCppLib 0.10
Eliminate all the tedious hassle when making state-of-the-art C++ 14 - 23 libraries!
Loading...
Searching...
No Matches
quickcpplib::_xxx::unit_test Namespace Reference

Classes

struct  requirement_failed
struct  test_case
struct  test_suite
struct  test_suite_registration
struct  test_case_registration

Functions

jmp_buf & test_case_failed ()
std::vector< test_suite > & test_suites ()
test_suite *& current_test_suite ()
test_case *& current_test_case ()
void test_case_skipped (const char *reason)
 Marks the running test case as SKIPPED, giving the reason.
int run (int argc, const char *const argv[])

Function Documentation

◆ test_case_failed()

jmp_buf & quickcpplib::_xxx::unit_test::test_case_failed ( )
externinline
113 {
114 static jmp_buf b;
115 return b;
116 }

◆ test_suites()

std::vector< test_suite > & quickcpplib::_xxx::unit_test::test_suites ( )
externinline
174 {
175 static std::vector<test_suite> v;
176 return v;
177 }

◆ current_test_suite()

test_suite *& quickcpplib::_xxx::unit_test::current_test_suite ( )
externinline
179 {
180 static test_suite *v;
181 return v;
182 }
Definition unit_test.hpp:165

◆ current_test_case()

test_case *& quickcpplib::_xxx::unit_test::current_test_case ( )
externinline
184 {
185 static test_case default_test_case("unnamed", "Default test case for unit test which don't declare test cases",
186 nullptr);
187 static test_case *v = &default_test_case;
188 return v;
189 }
Definition unit_test.hpp:119

◆ test_case_skipped()

void quickcpplib::_xxx::unit_test::test_case_skipped ( const char * reason)
externinline

Marks the running test case as SKIPPED, giving the reason.

A case that cannot run in this configuration must say so through here. Returning quietly instead reports PASSED, because the runner counts a case as passed whenever it did not fail and was not excluded by a name filter - which is how several cases in this tree came to report green having asserted nothing, with no <skipped/> in the JUnit output either.

198 {
200 if(tc != nullptr)
201 {
202 tc->skipped = true;
203 tc->self_skipped = true;
204 tc->skip_reason = reason;
205 std::cout << yellow << "SKIPPED: " << (reason != nullptr ? reason : "no reason given") << normal << std::endl;
206 }
207 }
std::ostream & yellow(std::ostream &s)
Makes the text on the console yellow.
Definition console_colours.hpp:145
std::ostream & normal(std::ostream &s)
Makes the text on the console non-bold and white.
Definition console_colours.hpp:155
test_case *& current_test_case()
Definition unit_test.hpp:183
bool self_skipped
Definition unit_test.hpp:129
const char * skip_reason
Definition unit_test.hpp:130
bool skipped
Definition unit_test.hpp:124

◆ run()

int quickcpplib::_xxx::unit_test::run ( int argc,
const char *const argv[] )
externinline
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 // Double -- means it's a flag
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 // -regex is disabled, otherwise it's an enabled
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;
266 for(const auto &j : test_suites())
267 {
268 for(const auto &i : j.test_cases)
269 {
270 if(strlen(i.name) > maxname)
271 maxname = strlen(i.name);
272 }
273 }
274 for(const auto &j : test_suites())
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 }
289 for(auto &j : test_suites())
290 {
291 for(auto &i : j.test_cases)
292 {
293 if(std::regex_match(i.name, enabled) && !std::regex_match(i.name, disabled))
294 {
295 current_test_case() = &i;
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
302 if(setjmp(test_case_failed()))
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 }
316 catch(const requirement_failed &)
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 }
347 current_test_case() = nullptr;
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;
358 for(const auto &j : test_suites())
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 // The reason is carried into the report, so a skipped case is
386 // visibly a case that did not run rather than one that passed.
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 << "&amp;";
397 break;
398 case '<':
399 oh << "&lt;";
400 break;
401 case '>':
402 oh << "&gt;";
403 break;
404 case '"':
405 oh << "&quot;";
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 // A run that executed NOTHING must not report success. An empty suite, a
436 // test-name regex that matches nothing, or a duplicate binary whose cases
437 // were compiled away all used to print "0 passed 0 failed" and exit 0 - a
438 // green test that tested nothing (reviewer P0 r1:T-9 / r3:T1). A case that
439 // skipped ITSELF is different from a case a filter excluded: the binary did
440 // run it and it said so, so an all-skipped platform build is not this
441 // failure - otherwise fixing the vacuous cases in signal_guard.cpp would
442 // turn every such platform red for reporting the truth.
443 if(totalpassed + totalfailed == 0 && totalselfskipped == 0)
444 {
445 // Nothing PASSED, nothing FAILED and nothing reported itself skipped:
446 // either the suite is empty or a name filter excluded every case (those
447 // show up as skipped). Both used to exit 0 with a "0 passed 0 failed"
448 // summary.
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