README.md (16397B)
1 # ๐งช utest.h 2 3 [](https://github.com/sheredom/utest.h/actions) 4 [](https://ci.appveyor.com/project/sheredom/utest-h) 5 [](https://github.com/sponsors/sheredom) 6 7 A simple one header solution to unit testing for C/C++. 8 9 ## Usage 10 11 Just `#include "utest.h"` in your code! 12 13 The current supported platforms are Linux, macOS and Windows. 14 15 The current supported compilers are gcc, clang, MSVC's cl.exe, and clang-cl.exe. 16 17 It also works with tcc but with a caveat: the latest release of the tcc compiler (version 0.9.27) lacks a feature for UTEST to work. Make sure to use a tcc that is patched with the constructor attribute extension. Recent Ubuntu and Debian Linux distros ship tcc with that patch already included. If you compile tcc yourself, use the trunk version and it will work as expected. 18 19 ## Command Line Options 20 21 utest.h supports some command line options: 22 23 * `--help` to output the help message 24 * `--filter=<filter>` will filter the test cases to run (useful for re-running one 25 particular offending test case). 26 * `--list-tests` will list testnames, one per line. Output names can be passed to `--filter`. 27 * `--output=<output>` will output an xunit XML file with the test results (that 28 Jenkins, travis-ci, and appveyor can parse for the test results). 29 * `--enable-mixed-units` will enable the per-test output to contain mixed units (s/ms/us/ns). 30 * `--random-order[=<seed>]` will randomize the order that the tests are ran in. If the optional <seed> argument is not provided, then a random starting seed is used. 31 32 ## Design 33 34 UTest is a single header library to enable all the fun of unit testing in C and 35 C++. The library has been designed to provide an output similar to Google's 36 googletest framework: 37 38 ``` 39 [==========] Running 1 test cases. 40 [ RUN ] foo.bar 41 [ OK ] foo.bar (631ns) 42 [==========] 1 test cases ran. 43 [ PASSED ] 1 tests. 44 ``` 45 46 ## UTEST_MAIN 47 48 In one C or C++ file, you must call the macro UTEST_MAIN: 49 50 ```c 51 UTEST_MAIN() 52 ``` 53 54 This will call into utest.h, instantiate all the testcases and run the unit test 55 framework. 56 57 Alternatively, if you want to write your own main and call into utest.h, you can 58 instead, in one C or C++ file call: 59 60 ```c 61 UTEST_STATE(); 62 ``` 63 64 And then when you are ready to call into the utest.h framework do: 65 66 ```c 67 int main(int argc, const char *const argv[]) { 68 // do your own thing 69 return utest_main(argc, argv); 70 } 71 ``` 72 73 ## Define a Testcase 74 75 To define a test case to run, you can do the following; 76 77 ```c 78 #include "utest.h" 79 80 UTEST(foo, bar) { 81 ASSERT_TRUE(1); 82 } 83 ``` 84 85 The UTEST macro takes two parameters - the first being the set that the test 86 case belongs to, the second being the name of the test. This allows tests to be 87 grouped for convenience. 88 89 ## Define a Fixtured Testcase 90 91 A fixtured testcase is one in which there is a struct that is instantiated that 92 can be shared across multiple testcases. 93 94 ```c 95 struct MyTestFixture { 96 char c; 97 int i; 98 float f; 99 }; 100 101 UTEST_F_SETUP(MyTestFixture) { 102 utest_fixture->c = 'a'; 103 utest_fixture->i = 42; 104 utest_fixture->f = 3.14f; 105 106 // we can even assert and expect in setup! 107 ASSERT_EQ(42, utest_fixture->i); 108 EXPECT_TRUE(true); 109 } 110 111 UTEST_F_TEARDOWN(MyTestFixture) { 112 // and also assert and expect in teardown! 113 ASSERT_EQ(13, utest_fixture->i); 114 } 115 116 UTEST_F(MyTestFixture, a) { 117 utest_fixture->i = 13; 118 // teardown will succeed because i is 13... 119 } 120 121 UTEST_F(MyTestFixture, b) { 122 utest_fixture->i = 83; 123 // teardown will fail because i is not 13! 124 } 125 ``` 126 127 Some things to note that were demonstrated above: 128 * We have this new implicit variable within our macros - utest_fixture. This is 129 a pointer to the struct you decided as your fixture (so MyTestFixture in the 130 above code). 131 * Instead of specifying a testcase set (like we do with the UTEST macro), we 132 instead specify the name of the fixture struct we are using. 133 * Every fixture has to have a `UTEST_F_SETUP` and `UTEST_F_TEARDOWN` macro - 134 even if they do nothing in the body. 135 * Multiple testcases (UTEST_F's) can use the same fixture. 136 * You can use EXPECT_* and ASSERT_* macros within the body of both the fixture's 137 setup and teardown macros. 138 139 ## Define an Indexed Testcase 140 141 Sometimes you want to use the same fixture _and_ testcase repeatedly, but 142 perhaps subtly change one variable within. This is where indexed testcases come 143 in. 144 145 ```c 146 struct MyTestIndexedFixture{ 147 bool x; 148 bool y; 149 }; 150 151 UTEST_I_SETUP(MyTestIndexedFixture) { 152 if (utest_index < 30) { 153 utest_fixture->x = utest_index & 1; 154 utest_fixture->y = (utest_index + 1) & 1; 155 } 156 } 157 158 UTEST_I_TEARDOWN(MyTestIndexedFixture) { 159 EXPECT_LE(0, utest_index); 160 } 161 162 UTEST_I(MyTestIndexedFixture, a, 2) { 163 ASSERT_TRUE(utest_fixture->x | utest_fixture->y); 164 } 165 166 UTEST_I(MyTestIndexedFixture, b, 42) { 167 // this will fail when the index is >= 30 168 ASSERT_TRUE(utest_fixture->x | utest_fixture->y); 169 } 170 ``` 171 172 Note: 173 * We use UTEST_I_* as the prefix for the setup and teardown functions now. 174 * We use UTEST_I to declare the testcases. 175 * We have access to a new variable utest_index in our setup and teardown 176 functions, that we can use to slightly vary our fixture. 177 * We provide a number as the third parameter of the UTEST_I macro - this is the 178 number of times we should run the test case for that index. It must be a 179 literal. 180 181 ## Testing Macros 182 183 Matching what googletest has, we provide two variants of each of the error 184 checking conditions - ASSERTs and EXPECTs. If an ASSERT fails, the test case 185 will cease execution, and utest.h will continue with the next test case to be 186 run. If an EXPECT fails, the remainder of the test case will still be executed, 187 allowing for further checks to be carried out. 188 189 We currently provide the following macros to be used within UTESTs: 190 191 ### ASSERT_TRUE(x) 192 193 Asserts that x evaluates to true (EG. non-zero). 194 195 ```c 196 UTEST(foo, bar) { 197 int i = 1; 198 ASSERT_TRUE(i); // pass! 199 ASSERT_TRUE(42); // pass! 200 ASSERT_TRUE(0); // fail! 201 } 202 ``` 203 204 ### ASSERT_FALSE(x) 205 206 Asserts that x evaluates to false (EG. zero). 207 208 ```c 209 UTEST(foo, bar) { 210 int i = 0; 211 ASSERT_FALSE(i); // pass! 212 ASSERT_FALSE(1); // fail! 213 } 214 ``` 215 216 ### ASSERT_EQ(x, y) 217 218 Asserts that x and y are equal. 219 220 ```c 221 UTEST(foo, bar) { 222 int a = 42; 223 int b = 42; 224 ASSERT_EQ(a, b); // pass! 225 ASSERT_EQ(a, 42); // pass! 226 ASSERT_EQ(42, b); // pass! 227 ASSERT_EQ(42, 42); // pass! 228 ASSERT_EQ(a, b + 1); // fail! 229 } 230 ``` 231 232 ### ASSERT_NE(x, y) 233 234 Asserts that x and y are not equal. 235 236 ```c 237 UTEST(foo, bar) { 238 int a = 42; 239 int b = 13; 240 ASSERT_NE(a, b); // pass! 241 ASSERT_NE(a, 27); // pass! 242 ASSERT_NE(69, b); // pass! 243 ASSERT_NE(42, 13); // pass! 244 ASSERT_NE(a, 42); // fail! 245 } 246 ``` 247 248 ### ASSERT_LT(x, y) 249 250 Asserts that x is less than y. 251 252 ```c 253 UTEST(foo, bar) { 254 int a = 13; 255 int b = 42; 256 ASSERT_LT(a, b); // pass! 257 ASSERT_LT(a, 27); // pass! 258 ASSERT_LT(27, b); // pass! 259 ASSERT_LT(13, 42); // pass! 260 ASSERT_LT(b, a); // fail! 261 } 262 ``` 263 264 ### ASSERT_LE(x, y) 265 266 Asserts that x is less than or equal to y. 267 268 ```c 269 UTEST(foo, bar) { 270 int a = 13; 271 int b = 42; 272 ASSERT_LE(a, b); // pass! 273 ASSERT_LE(a, 27); // pass! 274 ASSERT_LE(a, 13); // pass! 275 ASSERT_LE(27, b); // pass! 276 ASSERT_LE(42, b); // pass! 277 ASSERT_LE(13, 13); // pass! 278 ASSERT_LE(13, 42); // pass! 279 ASSERT_LE(b, a); // fail! 280 } 281 ``` 282 283 ### ASSERT_GT(x, y) 284 285 Asserts that x is greater than y. 286 287 ```c 288 UTEST(foo, bar) { 289 int a = 42; 290 int b = 13; 291 ASSERT_GT(a, b); // pass! 292 ASSERT_GT(a, 27); // pass! 293 ASSERT_GT(27, b); // pass! 294 ASSERT_GT(42, 13); // pass! 295 ASSERT_GT(b, a); // fail! 296 } 297 ``` 298 299 ### ASSERT_GE(x, y) 300 301 Asserts that x is greater than or equal to y. 302 303 ```c 304 UTEST(foo, bar) { 305 int a = 42; 306 int b = 13; 307 ASSERT_GE(a, b); // pass! 308 ASSERT_GE(a, 27); // pass! 309 ASSERT_GE(a, 13); // pass! 310 ASSERT_GE(27, b); // pass! 311 ASSERT_GE(42, b); // pass! 312 ASSERT_GE(13, 13); // pass! 313 ASSERT_GE(42, 13); // pass! 314 ASSERT_GE(b, a); // fail! 315 } 316 ``` 317 318 ### ASSERT_STREQ(x, y) 319 320 Asserts that the strings x and y are equal. 321 322 ```c 323 UTEST(foo, bar) { 324 char* a = "foo"; 325 char* b = "bar"; 326 ASSERT_STREQ(a, a); // pass! 327 ASSERT_STREQ(b, b); // pass! 328 ASSERT_STREQ(a, b); // fail! 329 } 330 ``` 331 332 ### ASSERT_STRNE(x, y) 333 334 Asserts that the strings x and y are not equal. 335 336 ```c 337 UTEST(foo, bar) { 338 char* a = "foo"; 339 char* b = "bar"; 340 ASSERT_STRNE(a, b); // pass! 341 ASSERT_STRNE(a, a); // fail! 342 } 343 ``` 344 345 ### ASSERT_STRNEQ(x, y) 346 347 Asserts that the strings x and y are equal up to the length of the string x. 348 349 ```c 350 UTEST(foo, bar) { 351 char* a = "foobar"; 352 char* b = "foo"; 353 ASSERT_STRNEQ(a, a); // pass! 354 ASSERT_STRNEQ(b, b); // pass! 355 ASSERT_STRNEQ(a, b); // pass! 356 } 357 ``` 358 359 ### ASSERT_STRNNE(x, y) 360 361 Asserts that the strings x and y are not equal up to the length of the string x. 362 363 ```c 364 UTEST(foo, bar) { 365 char* a = "foobar"; 366 char* b = "bar"; 367 ASSERT_STRNNE(a, b); // pass! 368 ASSERT_STRNNE(a, a); // fail! 369 } 370 ``` 371 372 ### ASSERT_NEAR(x, y, epsilon) 373 374 Asserts that the floating-point values x and y are within epsilon distance of 375 each other. 376 377 ```c 378 UTEST(foo, bar) { 379 float a = 42.0f; 380 float b = 42.01f; 381 ASSERT_NEAR(a, b, 0.01f); // pass! 382 ASSERT_NEAR(a, b, 0.001f); // fail! 383 } 384 ``` 385 386 ### ASSERT_EXCEPTION(x, exception_type) 387 388 Asserts that exception_type will be thrown when code x is executed. 389 390 ```cpp 391 void foo(int bar) { 392 if (bar == 1) 393 throw std::range_error; 394 } 395 396 UTEST(foo, bar) { 397 ASSERT_EXCEPTION(foo(1), std::range_error); // pass! 398 ASSERT_EXCEPTION(foo(2), std::range_error); // fail! 399 ASSERT_EXCEPTION(foo(1), std::exception); // fail! 400 } 401 ``` 402 403 ### EXPECT_TRUE(x) 404 405 Expects that x evaluates to true (i.e. non-zero). 406 407 ```c 408 UTEST(foo, bar) { 409 int i = 1; 410 EXPECT_TRUE(i); // pass! 411 EXPECT_TRUE(42); // pass! 412 EXPECT_TRUE(0); // fail! 413 } 414 ``` 415 416 ### EXPECT_FALSE(x) 417 418 Expects that x evaluates to false (i.e. zero). 419 420 ```c 421 UTEST(foo, bar) { 422 int i = 0; 423 EXPECT_FALSE(i); // pass! 424 EXPECT_FALSE(1); // fail! 425 } 426 ``` 427 428 ### EXPECT_EQ(x, y) 429 430 Expects that x and y are equal. 431 432 ```c 433 UTEST(foo, bar) { 434 int a = 42; 435 int b = 42; 436 EXPECT_EQ(a, b); // pass! 437 EXPECT_EQ(a, 42); // pass! 438 EXPECT_EQ(42, b); // pass! 439 EXPECT_EQ(42, 42); // pass! 440 EXPECT_EQ(a, b + 1); // fail! 441 } 442 ``` 443 444 ### EXPECT_NE(x, y) 445 446 Expects that x and y are not equal. 447 448 ```c 449 UTEST(foo, bar) { 450 int a = 42; 451 int b = 13; 452 EXPECT_NE(a, b); // pass! 453 EXPECT_NE(a, 27); // pass! 454 EXPECT_NE(69, b); // pass! 455 EXPECT_NE(42, 13); // pass! 456 EXPECT_NE(a, 42); // fail! 457 } 458 ``` 459 460 ### EXPECT_LT(x, y) 461 462 Expects that x is less than y. 463 464 ```c 465 UTEST(foo, bar) { 466 int a = 13; 467 int b = 42; 468 EXPECT_LT(a, b); // pass! 469 EXPECT_LT(a, 27); // pass! 470 EXPECT_LT(27, b); // pass! 471 EXPECT_LT(13, 42); // pass! 472 EXPECT_LT(b, a); // fail! 473 } 474 ``` 475 476 ### EXPECT_LE(x, y) 477 478 Expects that x is less than or equal to y. 479 480 ```c 481 UTEST(foo, bar) { 482 int a = 13; 483 int b = 42; 484 EXPECT_LE(a, b); // pass! 485 EXPECT_LE(a, 27); // pass! 486 EXPECT_LE(a, 13); // pass! 487 EXPECT_LE(27, b); // pass! 488 EXPECT_LE(42, b); // pass! 489 EXPECT_LE(13, 13); // pass! 490 EXPECT_LE(13, 42); // pass! 491 EXPECT_LE(b, a); // fail! 492 } 493 ``` 494 495 ### EXPECT_GT(x, y) 496 497 Expects that x is greater than y. 498 499 ```c 500 UTEST(foo, bar) { 501 int a = 42; 502 int b = 13; 503 EXPECT_GT(a, b); // pass! 504 EXPECT_GT(a, 27); // pass! 505 EXPECT_GT(27, b); // pass! 506 EXPECT_GT(42, 13); // pass! 507 EXPECT_GT(b, a); // fail! 508 } 509 ``` 510 511 ### EXPECT_GE(x, y) 512 513 Expects that x is greater than or equal to y. 514 515 ```c 516 UTEST(foo, bar) { 517 int a = 42; 518 int b = 13; 519 EXPECT_GE(a, b); // pass! 520 EXPECT_GE(a, 27); // pass! 521 EXPECT_GE(a, 13); // pass! 522 EXPECT_GE(27, b); // pass! 523 EXPECT_GE(42, b); // pass! 524 EXPECT_GE(13, 13); // pass! 525 EXPECT_GE(42, 13); // pass! 526 EXPECT_GE(b, a); // fail! 527 } 528 ``` 529 530 ### EXPECT_STREQ(x, y) 531 532 Expects that the strings x and y are equal. 533 534 ```c 535 UTEST(foo, bar) { 536 char* a = "foo"; 537 char* b = "bar"; 538 EXPECT_STREQ(a, a); // pass! 539 EXPECT_STREQ(b, b); // pass! 540 EXPECT_STREQ(a, b); // fail! 541 } 542 ``` 543 544 ### EXPECT_STRNE(x, y) 545 546 Expects that the strings x and y are not equal. 547 548 ```c 549 UTEST(foo, bar) { 550 char* a = "foo"; 551 char* b = "bar"; 552 EXPECT_STRNE(a, b); // pass! 553 EXPECT_STRNE(a, a); // fail! 554 } 555 ``` 556 557 ### EXPECT_STRNEQ(x, y) 558 559 Expects that the strings x and y are equal up to the length of the string x. 560 561 ```c 562 UTEST(foo, bar) { 563 char* a = "foobar"; 564 char* b = "foo"; 565 EXPECT_STRNEQ(a, a); // pass! 566 EXPECT_STRNEQ(b, b); // pass! 567 EXPECT_STRNEQ(a, b); // pass! 568 } 569 ``` 570 571 ### EXPECT_STRNNE(x, y) 572 573 Expects that the strings x and y are not equal up to the length of the string x. 574 575 ```c 576 UTEST(foo, bar) { 577 char* a = "foobar"; 578 char* b = "bar"; 579 EXPECT_STRNNE(a, b); // pass! 580 EXPECT_STRNNE(a, a); // fail! 581 } 582 ``` 583 584 ### EXPECT_NEAR(x, y, epsilon) 585 586 Expects that the floating-point values x and y are within epsilon distance of 587 each other. 588 589 ```c 590 UTEST(foo, bar) { 591 float a = 42.0f; 592 float b = 42.01f; 593 EXPECT_NEAR(a, b, 0.01f); // pass! 594 EXPECT_NEAR(a, b, 0.001f); // fail! 595 } 596 ``` 597 598 ### EXPECT_EXCEPTION(x, exception_type) 599 600 Expects that exception_type will be thrown when code x is executed. 601 602 ```cpp 603 void foo(int bar) { 604 if (bar == 1) 605 throw std::range_error; 606 } 607 608 UTEST(foo, bar) { 609 EXPECT_EXCEPTION(foo(1), std::range_error); // pass! 610 EXPECT_EXCEPTION(foo(2), std::range_error); // fail! 611 EXPECT_EXCEPTION(foo(1), std::exception); // fail! 612 } 613 ``` 614 615 ### EXPECT_EXCEPTION_WITH_MESSAGE(x, exception_type, exception_message) 616 617 Expects that exception_type will be thrown with message exception_message when code x is executed. 618 619 ```cpp 620 void foo(int bar) { 621 if (bar == 1) 622 throw std::range_error("bad bar"); 623 } 624 625 UTEST(foo, bar) { 626 EXPECT_EXCEPTION_WITH_MESSAGE(foo(1), std::range_error, "bad bar"); // pass! 627 EXPECT_EXCEPTION_WITH_MESSAGE(foo(2), std::range_error, "bad bar2"); // fail! 628 EXPECT_EXCEPTION_WITH_MESSAGE(foo(1), std::exception, "bad bar"); // fail! 629 } 630 ``` 631 632 ### UTEST_SKIP(msg) 633 634 This macro lets you mark a test case as being skipped - eg. that the test case 635 is not to be executed. The test will stop running as you execute the macro, 636 report the `msg` as the reason for the skipped, and mark the test as 637 _'skipped'_. These will be reported at the end of execution before failures, and 638 skipped test cases will **not** cause the process to exit with a non-zero code. 639 640 ```c 641 UTEST(foo, bar) { 642 UTEST_SKIP("Need to implement this test!"); 643 } 644 ``` 645 646 ### Testing macros with custom message 647 648 In addition, to give the possibility of having custom messages in the fault 649 tests, all macros can be used with a suffix called "_MSG", which receives an 650 extra parameter, which is the string with the custom message to print in case 651 of failure. 652 653 For example: 654 655 ```c 656 UTEST(foo, bar) { 657 int i = 1; 658 EXPECT_TRUE_MSG(i, "custom message"); // pass! 659 EXPECT_TRUE_MSG(42, "custom message"); // pass! 660 EXPECT_TRUE_MSG(0, "custom message"); // fail! (with the following output) 661 } 662 ``` 663 664 ``` 665 test.cpp:42: Failure 666 Expected : true 667 Actual : false 668 Message : custom message 669 [ FAILED ] foo.bar (8086ns) 670 ``` 671 672 ## Types Supported for Checks 673 674 The library supports asserting on any builtin integer, floating-point, or 675 pointer type. 676 677 ## License 678 679 This is free and unencumbered software released into the public domain. 680 681 Anyone is free to copy, modify, publish, use, compile, sell, or 682 distribute this software, either in source code form or as a compiled 683 binary, for any purpose, commercial or non-commercial, and by any 684 means. 685 686 In jurisdictions that recognize copyright laws, the author or authors 687 of this software dedicate any and all copyright interest in the 688 software to the public domain. We make this dedication for the benefit 689 of the public at large and to the detriment of our heirs and 690 successors. We intend this dedication to be an overt act of 691 relinquishment in perpetuity of all present and future rights to this 692 software under copyright law. 693 694 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 695 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 696 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 697 IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR 698 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 699 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 700 OTHER DEALINGS IN THE SOFTWARE. 701 702 For more information, please refer to <http://unlicense.org/>