1 <!-- ##### SECTION Title ##### --> 2 Testing 3 4 <!-- ##### SECTION Short_Description ##### --> 5 a test framework 6 7 <!-- ##### SECTION Long_Description ##### --> 8 <para> 9 GLib provides a framework for writing and maintaining unit tests 10 in parallel to the code they are testing. The API is designed according 11 to established concepts found in the other test frameworks (JUnit, NUnit, 12 RUnit), which in turn is based on smalltalk unit testing concepts. 13 <variablelist> 14 <varlistentry> 15 <term>Test case</term> 16 <listitem><para> 17 Tests (test methods) are grouped together with their 18 fixture into test cases. 19 </para></listitem> 20 </varlistentry> 21 <varlistentry> 22 <term>Fixture</term> 23 <listitem><para> 24 A test fixture consists of fixture data and setup and teardown methods 25 to establish the environment for the test functions. We use fresh 26 fixtures, i.e. fixtures are newly set up and torn down around each test 27 invocation to avoid dependencies between tests. 28 </para></listitem> 29 </varlistentry> 30 <varlistentry> 31 <term>Test suite</term> 32 <listitem><para> 33 Test cases can be grouped into test suites, to allow subsets of the 34 available tests to be run. Test suites can be grouped into other test 35 suites as well. 36 </para></listitem> 37 </varlistentry> 38 </variablelist> 39 The API is designed to handle creation and registration of test suites and 40 test cases implicitly. A simple call like 41 <informalexample><programlisting> 42 g_test_add_func ("/misc/assertions", test_assertions); 43 </programlisting></informalexample> 44 creates a test suite called "misc" with a single test case named "assertions", 45 which consists of running the test_assertions function. 46 </para> 47 <para> 48 In addition to the traditional g_assert(), the test framework provides 49 an extended set of assertions for string and numerical comparisons: 50 g_assert_cmpfloat(), g_assert_cmpint(), g_assert_cmpuint(), g_assert_cmphex(), 51 g_assert_cmpstr(). The advantage of these variants over plain g_assert() 52 is that the assertion messages can be more elaborate, and include the 53 values of the compared entities. 54 </para> 55 <para> 56 GLib ships with two utilities called gtester and gtester-report to 57 facilitate running tests and producing nicely formatted test reports. 58 </para> 59 60 <!-- ##### SECTION See_Also ##### --> 61 <para> 62 <link linkend="gtester">gtester</link>, 63 <link linkend="gtester-report">gtester-report</link> 64 </para> 65 66 <!-- ##### SECTION Stability_Level ##### --> 67 68 69 <!-- ##### FUNCTION g_test_minimized_result ##### --> 70 <para> 71 72 </para> 73 74 @minimized_quantity: 75 @format: 76 @Varargs: 77 78 79 <!-- ##### FUNCTION g_test_maximized_result ##### --> 80 <para> 81 82 </para> 83 84 @maximized_quantity: 85 @format: 86 @Varargs: 87 88 89 <!-- ##### FUNCTION g_test_init ##### --> 90 <para> 91 92 </para> 93 94 @argc: 95 @argv: 96 @Varargs: 97 98 99 <!-- ##### MACRO g_test_quick ##### --> 100 <para> 101 Returns %TRUE if tests are run in quick mode. 102 </para> 103 104 105 106 <!-- ##### MACRO g_test_slow ##### --> 107 <para> 108 Returns %TRUE if tests are run in slow mode. 109 </para> 110 111 112 113 <!-- ##### MACRO g_test_thorough ##### --> 114 <para> 115 Returns %TRUE if tests are run in thorough mode. 116 </para> 117 118 119 120 <!-- ##### MACRO g_test_perf ##### --> 121 <para> 122 Returns %TRUE if tests are run in performance mode. 123 </para> 124 125 126 127 <!-- ##### MACRO g_test_verbose ##### --> 128 <para> 129 Returns %TRUE if tests are run in verbose mode. 130 </para> 131 132 133 134 <!-- ##### MACRO g_test_quiet ##### --> 135 <para> 136 Returns %TRUE if tests are run in quiet mode. 137 </para> 138 139 140 141 <!-- ##### FUNCTION g_test_run ##### --> 142 <para> 143 144 </para> 145 146 @Returns: 147 148 149 <!-- ##### FUNCTION g_test_add_func ##### --> 150 <para> 151 152 </para> 153 154 @testpath: 155 @test_func: 156 157 158 <!-- ##### FUNCTION g_test_add_data_func ##### --> 159 <para> 160 161 </para> 162 163 @testpath: 164 @test_data: 165 @test_func: 166 167 168 <!-- ##### MACRO g_test_add ##### --> 169 <para> 170 171 </para> 172 173 @testpath: 174 @Fixture: 175 @tdata: 176 @fsetup: 177 @ftest: 178 @fteardown: 179 180 181 <!-- ##### FUNCTION g_test_message ##### --> 182 <para> 183 184 </para> 185 186 @format: 187 @Varargs: 188 189 190 <!-- ##### FUNCTION g_test_bug_base ##### --> 191 <para> 192 193 </para> 194 195 @uri_pattern: 196 197 198 <!-- ##### FUNCTION g_test_bug ##### --> 199 <para> 200 201 </para> 202 203 @bug_uri_snippet: 204 205 206 <!-- ##### FUNCTION g_test_timer_start ##### --> 207 <para> 208 209 </para> 210 211 212 213 <!-- ##### FUNCTION g_test_timer_elapsed ##### --> 214 <para> 215 216 </para> 217 218 @Returns: 219 220 221 <!-- ##### FUNCTION g_test_timer_last ##### --> 222 <para> 223 224 </para> 225 226 @Returns: 227 228 229 <!-- ##### FUNCTION g_test_queue_free ##### --> 230 <para> 231 232 </para> 233 234 @gfree_pointer: 235 236 237 <!-- ##### FUNCTION g_test_queue_destroy ##### --> 238 <para> 239 240 </para> 241 242 @destroy_func: 243 @destroy_data: 244 245 246 <!-- ##### MACRO g_test_queue_unref ##### --> 247 <para> 248 Enqueue an object to be released with g_object_unref() during 249 the next teardown phase. This is equivalent to calling g_test_queue_destroy() 250 with a destroy callback of g_object_unref(). 251 </para> 252 253 @gobject: the object to unref 254 @Since: 2.16 255 256 257 <!-- ##### ENUM GTestTrapFlags ##### --> 258 <para> 259 Test traps are guards around forked tests. These flags 260 determine what traps to set. 261 </para> 262 263 @G_TEST_TRAP_SILENCE_STDOUT: Redirect stdout of the test child to 264 <filename>/dev/null</filename> so it cannot be observed on the 265 console during test runs. The actual output is still captured 266 though to allow later tests with g_test_trap_assert_stdout(). 267 @G_TEST_TRAP_SILENCE_STDERR: Redirect stderr of the test child to 268 <filename>/dev/null</filename> so it cannot be observed on the 269 console during test runs. The actual output is still captured 270 though to allow later tests with g_test_trap_assert_stderr(). 271 @G_TEST_TRAP_INHERIT_STDIN: If this flag is given, stdin of the forked 272 child process is shared with stdin of its parent process. It is 273 redirected to <filename>/dev/null</filename> otherwise. 274 275 <!-- ##### FUNCTION g_test_trap_fork ##### --> 276 <para> 277 278 </para> 279 280 @usec_timeout: 281 @test_trap_flags: 282 @Returns: 283 284 285 <!-- ##### FUNCTION g_test_trap_has_passed ##### --> 286 <para> 287 288 </para> 289 290 @Returns: 291 292 293 <!-- ##### FUNCTION g_test_trap_reached_timeout ##### --> 294 <para> 295 296 </para> 297 298 @Returns: 299 300 301 <!-- ##### MACRO g_test_trap_assert_passed ##### --> 302 <para> 303 Assert that the last forked test passed. See g_test_trap_fork(). 304 </para> 305 306 @Since: 2.16 307 308 309 <!-- ##### MACRO g_test_trap_assert_failed ##### --> 310 <para> 311 Assert that the last forked test failed. See g_test_trap_fork(). 312 </para> 313 314 @Since: 2.16 315 316 317 <!-- ##### MACRO g_test_trap_assert_stdout ##### --> 318 <para> 319 Assert that the stdout output of the last forked test matches @soutpattern. 320 See g_test_trap_fork(). 321 </para> 322 323 @soutpattern: a glob-style <link linkend="glib-Glob-style-pattern-matching">pattern</link> 324 @Since: 2.16 325 326 327 <!-- ##### MACRO g_test_trap_assert_stdout_unmatched ##### --> 328 <para> 329 Assert that the stdout output of the last forked test does not match 330 @soutpattern. See g_test_trap_fork(). 331 </para> 332 333 @soutpattern: a glob-style <link linkend="glib-Glob-style-pattern-matching">pattern</link> 334 @Since: 2.16 335 336 337 <!-- ##### MACRO g_test_trap_assert_stderr ##### --> 338 <para> 339 Assert that the stderr output of the last forked test matches @serrpattern. 340 See g_test_trap_fork(). 341 </para> 342 343 @serrpattern: a glob-style <link linkend="glib-Glob-style-pattern-matching">pattern</link> 344 @Since: 2.16 345 346 347 <!-- ##### MACRO g_test_trap_assert_stderr_unmatched ##### --> 348 <para> 349 Assert that the stderr output of the last forked test does not match 350 @serrpattern. See g_test_trap_fork(). 351 </para> 352 353 @serrpattern: a glob-style <link linkend="glib-Glob-style-pattern-matching">pattern</link> 354 @Since: 2.16 355 356 357 <!-- ##### MACRO g_test_rand_bit ##### --> 358 <para> 359 Get a reproducible random bit (0 or 1), 360 see g_test_rand_int() for details on test case random numbers. 361 </para> 362 363 @Since: 2.16 364 365 366 <!-- ##### FUNCTION g_test_rand_int ##### --> 367 <para> 368 369 </para> 370 371 @Returns: 372 373 374 <!-- ##### FUNCTION g_test_rand_int_range ##### --> 375 <para> 376 377 </para> 378 379 @begin: 380 @end: 381 @Returns: 382 383 384 <!-- ##### FUNCTION g_test_rand_double ##### --> 385 <para> 386 387 </para> 388 389 @Returns: 390 391 392 <!-- ##### FUNCTION g_test_rand_double_range ##### --> 393 <para> 394 395 </para> 396 397 @range_start: 398 @range_end: 399 @Returns: 400 401 402 <!-- ##### MACRO g_assert ##### --> 403 <para> 404 Debugging macro to terminate the application if the assertion fails. 405 If the assertion fails (i.e. the expression is not true), an error message 406 is logged and the application is terminated. 407 </para> 408 <para> 409 The macro can be turned off in final releases of code by defining 410 #G_DISABLE_ASSERT when compiling the application. 411 </para> 412 413 @expr: the expression to check. 414 415 416 <!-- ##### MACRO g_assert_not_reached ##### --> 417 <para> 418 Debugging macro to terminate the application if it is ever reached. 419 If it is reached, an error message is logged and the application is terminated. 420 </para> 421 <para> 422 The macro can be turned off in final releases of code by defining 423 #G_DISABLE_ASSERT when compiling the application. 424 </para> 425 426 427 428 <!-- ##### MACRO g_assert_cmpstr ##### --> 429 <para> 430 Debugging macro to terminate the application with a warning message 431 if a string comparison fails. 432 The strings are compared using g_strcmp0(). 433 </para> 434 <para> 435 The effect of <literal>g_assert_cmpstr (s1, op, s2)</literal> is the same 436 as <literal>g_assert (g_strcmp0 (s1, s2) op 0)</literal>. The advantage of this macro 437 is that it can produce a message that includes the actual values of @s1 438 and @s2. 439 </para> 440 <informalexample><programlisting> 441 g_assert_cmpstr (mystring, ==, "fubar"); 442 </programlisting></informalexample> 443 444 @s1: a string (may be %NULL) 445 @cmp: The comparison operator to use. One of ==, !=, <, >, <=, >=. 446 @s2: another string (may be %NULL) 447 @Since: 2.16 448 449 450 <!-- ##### MACRO g_assert_cmpint ##### --> 451 <para> 452 Debugging macro to terminate the application with a warning message 453 if an integer comparison fails. 454 </para> 455 <para> 456 The effect of <literal>g_assert_cmpint (n1, op, n2)</literal> is the same 457 as <literal>g_assert (n1 op n2)</literal>. The advantage of this macro 458 is that it can produce a message that includes the actual values of @n1 459 and @n2. 460 </para> 461 462 @n1: an integer 463 @cmp: The comparison operator to use. One of ==, !=, <, >, <=, >=. 464 @n2: another integer 465 @Since: 2.16 466 467 468 <!-- ##### MACRO g_assert_cmpuint ##### --> 469 <para> 470 Debugging macro to terminate the application with a warning message 471 if an unsigned integer comparison fails. 472 </para> 473 <para> 474 The effect of <literal>g_assert_cmpuint (n1, op, n2)</literal> is the same 475 as <literal>g_assert (n1 op n2)</literal>. The advantage of this macro 476 is that it can produce a message that includes the actual values of @n1 477 and @n2. 478 </para> 479 480 @n1: an unsigned integer 481 @cmp: The comparison operator to use. One of ==, !=, <, >, <=, >=. 482 @n2: another unsigned integer 483 @Since: 2.16 484 485 486 <!-- ##### MACRO g_assert_cmphex ##### --> 487 <para> 488 Debugging macro to terminate the application with a warning message 489 if an unsigned integer comparison fails. This is a variant of 490 g_assert_cmpuint() that displays the numbers in hexadecimal notation 491 in the message. 492 </para> 493 494 @n1: an unsigned integer 495 @cmp: The comparison operator to use. One of ==, !=, <, >, <=, >=. 496 @n2: another unsigned integer 497 @Since: 2.16 498 499 500 <!-- ##### MACRO g_assert_cmpfloat ##### --> 501 <para> 502 Debugging macro to terminate the application with a warning message 503 if a floating point number comparison fails. 504 </para> 505 <para> 506 The effect of <literal>g_assert_cmpfloat (n1, op, n2)</literal> is the same 507 as <literal>g_assert (n1 op n2)</literal>. The advantage of this function 508 is that it can produce a message that includes the actual values of @n1 509 and @n2. 510 </para> 511 512 @n1: an floating point number 513 @cmp: The comparison operator to use. One of ==, !=, <, >, <=, >=. 514 @n2: another floating point number 515 @Since: 2.16 516 517 518 <!-- ##### MACRO g_assert_no_error ##### --> 519 <para> 520 Debugging macro to terminate the application with a warning message 521 if a method has returned a #GError. 522 </para> 523 <para> 524 The effect of <literal>g_assert_no_error (err)</literal> is the same 525 as <literal>g_assert (err == NULL)</literal>. The advantage of this macro 526 is that it can produce a message that includes the error message and code. 527 </para> 528 529 @err: a #GError, possibly %NULL 530 @Since: 2.20 531 532 533 <!-- ##### MACRO g_assert_error ##### --> 534 <para> 535 Debugging macro to terminate the application with a warning message 536 if a method has not returned the correct #GError. 537 </para> 538 <para> 539 The effect of <literal>g_assert_error (err, dom, c)</literal> is the same 540 as <literal>g_assert (err != NULL && err->domain == dom && err->code == c)</literal>. 541 The advantage of this macro is that it can produce a message that 542 includes the incorrect error message and code. 543 </para> 544 <para> 545 This can only be used to test for a specific error. If you want to 546 test that @err is set, but don't care what it's set to, just use 547 <literal>g_assert (err != NULL)</literal> 548 </para> 549 550 @err: a #GError, possibly %NULL 551 @dom: the expected error domain (a #GQuark) 552 @c: the expected error code 553 @Since: 2.20 554 555 556 <!-- ##### TYPEDEF GTestCase ##### --> 557 <para> 558 An opaque structure representing a test case. 559 </para> 560 561 562 <!-- ##### TYPEDEF GTestSuite ##### --> 563 <para> 564 An opaque structure representing a test suite. 565 </para> 566 567 568 <!-- ##### FUNCTION g_test_create_case ##### --> 569 <para> 570 571 </para> 572 573 @test_name: 574 @data_size: 575 @test_data: 576 @data_setup: 577 @data_test: 578 @data_teardown: 579 @Returns: 580 581 582 <!-- ##### FUNCTION g_test_create_suite ##### --> 583 <para> 584 585 </para> 586 587 @suite_name: 588 @Returns: 589 590 591 <!-- ##### FUNCTION g_test_get_root ##### --> 592 <para> 593 594 </para> 595 596 @Returns: 597 598 599 <!-- ##### FUNCTION g_test_suite_add ##### --> 600 <para> 601 602 </para> 603 604 @suite: 605 @test_case: 606 607 608 <!-- ##### FUNCTION g_test_suite_add_suite ##### --> 609 <para> 610 611 </para> 612 613 @suite: 614 @nestedsuite: 615 616 617 <!-- ##### FUNCTION g_test_run_suite ##### --> 618 <para> 619 620 </para> 621 622 @suite: 623 @Returns: 624 625 626