testing.sgml 13.8 KB
Newer Older
1 2 3 4
<!-- ##### SECTION Title ##### -->
Testing

<!-- ##### SECTION Short_Description ##### -->
Matthias Clasen's avatar
Matthias Clasen committed
5
a test framework
6 7 8

<!-- ##### SECTION Long_Description ##### -->
<para>
Matthias Clasen's avatar
Matthias Clasen committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
GLib provides a framework for writing and maintaining unit tests
in parallel to the code they are testing. The API is designed according 
to established concepts found in the other test frameworks (JUnit, NUnit, 
RUnit), which in turn is based on smalltalk unit testing concepts.
<variablelist>
  <varlistentry>
    <term>Test case</term>
    <listitem><para>
      Tests (test methods) are grouped together with their 
      fixture into test cases.
    </para></listitem>
  </varlistentry>
  <varlistentry>
    <term>Fixture</term>
    <listitem><para>
      A test fixture consists of fixture data and setup and teardown methods 
      to establish the environment for the test functions. We use fresh 
      fixtures, i.e. fixtures are newly set up and torn down around each test 
27
      invocation to avoid dependencies between tests.
Matthias Clasen's avatar
Matthias Clasen committed
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
    </para></listitem>
  </varlistentry>
  <varlistentry>
    <term>Test suite</term>
    <listitem><para>
      Test cases can be grouped into test suites, to allow subsets of the 
      available tests to be run. Test suites can be grouped into other test 
      suites as well.
    </para></listitem>
  </varlistentry>
</variablelist>
The API is designed to handle creation and registration of test suites and
test cases implicitly. A simple call like
<informalexample><programlisting>
  g_test_add_func ("/misc/assertions", test_assertions);
</programlisting></informalexample>
creates a test suite called "misc" with a single test case named "assertions",
which consists of running the test_assertions function.
</para>
<para>
In addition to the traditional g_assert(), the test framework provides
an extended set of assertions for string and numerical comparisons:
g_assert_cmpfloat(), g_assert_cmpint(), g_assert_cmpuint(), g_assert_cmphex(),
g_assert_cmpstr(). The advantage of these variants over plain g_assert()
is that the assertion messages can be more elaborate, and include the
values of the compared entities.
</para>
<para>
56
GLib ships with two utilities called gtester and gtester-report to 
Matthias Clasen's avatar
Matthias Clasen committed
57
facilitate running tests and producing nicely formatted test reports. 
58 59 60 61
</para>

<!-- ##### SECTION See_Also ##### -->
<para>
Matthias Clasen's avatar
Matthias Clasen committed
62 63
<link linkend="gtester">gtester</link>,
<link linkend="gtester-report">gtester-report</link>
64 65 66 67 68
</para>

<!-- ##### SECTION Stability_Level ##### -->


Matthias Clasen's avatar
2.25.0  
Matthias Clasen committed
69 70 71
<!-- ##### SECTION Image ##### -->


72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
<!-- ##### FUNCTION g_test_minimized_result ##### -->
<para>

</para>

@minimized_quantity: 
@format: 
@Varargs: 


<!-- ##### FUNCTION g_test_maximized_result ##### -->
<para>

</para>

@maximized_quantity: 
@format: 
@Varargs: 


<!-- ##### FUNCTION g_test_init ##### -->
<para>

</para>

@argc: 
@argv: 
@Varargs: 


<!-- ##### MACRO g_test_quick ##### -->
<para>
Matthias Clasen's avatar
Matthias Clasen committed
104
Returns %TRUE if tests are run in quick mode.
105 106 107 108 109 110
</para>



<!-- ##### MACRO g_test_slow ##### -->
<para>
Matthias Clasen's avatar
Matthias Clasen committed
111
Returns %TRUE if tests are run in slow mode.
112 113 114 115 116 117
</para>



<!-- ##### MACRO g_test_thorough ##### -->
<para>
Matthias Clasen's avatar
Matthias Clasen committed
118
Returns %TRUE if tests are run in thorough mode.
119 120 121 122 123 124
</para>



<!-- ##### MACRO g_test_perf ##### -->
<para>
Matthias Clasen's avatar
Matthias Clasen committed
125
Returns %TRUE if tests are run in performance mode.
126 127 128 129 130 131
</para>



<!-- ##### MACRO g_test_verbose ##### -->
<para>
Matthias Clasen's avatar
Matthias Clasen committed
132
Returns %TRUE if tests are run in verbose mode.
133 134 135 136 137 138
</para>



<!-- ##### MACRO g_test_quiet ##### -->
<para>
Matthias Clasen's avatar
Matthias Clasen committed
139
Returns %TRUE if tests are run in quiet mode.
140 141 142 143 144 145 146 147 148
</para>



<!-- ##### FUNCTION g_test_run ##### -->
<para>

</para>

Matthias Clasen's avatar
2.25.0  
Matthias Clasen committed
149
@void: 
150 151 152
@Returns: 


153 154 155 156 157 158 159 160
<!-- ##### USER_FUNCTION GTestFunc ##### -->
<para>

</para>

@void: 


161 162 163 164 165 166
<!-- ##### FUNCTION g_test_add_func ##### -->
<para>

</para>

@testpath: 
167 168 169 170 171 172 173 174 175
@test_func: 


<!-- ##### USER_FUNCTION GTestDataFunc ##### -->
<para>

</para>

@user_data: 
176 177 178 179 180 181 182 183 184


<!-- ##### FUNCTION g_test_add_data_func ##### -->
<para>

</para>

@testpath: 
@test_data: 
185
@test_func: 
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225


<!-- ##### MACRO g_test_add ##### -->
<para>

</para>

@testpath: 
@Fixture: 
@tdata: 
@fsetup: 
@ftest: 
@fteardown: 


<!-- ##### FUNCTION g_test_message ##### -->
<para>

</para>

@format: 
@Varargs: 


<!-- ##### FUNCTION g_test_bug_base ##### -->
<para>

</para>

@uri_pattern: 


<!-- ##### FUNCTION g_test_bug ##### -->
<para>

</para>

@bug_uri_snippet: 


Matthias Clasen's avatar
2.22.0  
Matthias Clasen committed
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
<!-- ##### USER_FUNCTION GTestLogFatalFunc ##### -->
<para>

</para>

@log_domain: 
@log_level: 
@message: 
@user_data: 
@Returns: 


<!-- ##### FUNCTION g_test_log_set_fatal_handler ##### -->
<para>

</para>

@log_func: 
@user_data: 


247 248 249 250 251
<!-- ##### FUNCTION g_test_timer_start ##### -->
<para>

</para>

Matthias Clasen's avatar
2.25.0  
Matthias Clasen committed
252
@void: 
253 254 255 256 257 258 259


<!-- ##### FUNCTION g_test_timer_elapsed ##### -->
<para>

</para>

Matthias Clasen's avatar
2.25.0  
Matthias Clasen committed
260
@void: 
261 262 263 264 265 266 267 268
@Returns: 


<!-- ##### FUNCTION g_test_timer_last ##### -->
<para>

</para>

Matthias Clasen's avatar
2.25.0  
Matthias Clasen committed
269
@void: 
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
@Returns: 


<!-- ##### FUNCTION g_test_queue_free ##### -->
<para>

</para>

@gfree_pointer: 


<!-- ##### FUNCTION g_test_queue_destroy ##### -->
<para>

</para>

@destroy_func: 
@destroy_data: 


<!-- ##### MACRO g_test_queue_unref ##### -->
<para>
Matthias Clasen's avatar
Matthias Clasen committed
292 293 294
Enqueue an object to be released with g_object_unref() during
the next teardown phase. This is equivalent to calling g_test_queue_destroy()
with a destroy callback of g_object_unref().
295 296
</para>

Matthias Clasen's avatar
Matthias Clasen committed
297 298
@gobject: the object to unref
@Since: 2.16
299 300 301 302


<!-- ##### ENUM GTestTrapFlags ##### -->
<para>
Matthias Clasen's avatar
Matthias Clasen committed
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
Test traps are guards around forked tests. These flags
determine what traps to set.
</para>

@G_TEST_TRAP_SILENCE_STDOUT: Redirect stdout of the test child to 
    <filename>/dev/null</filename> so it cannot be observed on the 
    console during test runs. The actual output is still captured 
    though to allow later tests with g_test_trap_assert_stdout().
@G_TEST_TRAP_SILENCE_STDERR: Redirect stderr of the test child to 
    <filename>/dev/null</filename> so it cannot be observed on the 
    console during test runs. The actual output is still captured 
    though to allow later tests with g_test_trap_assert_stderr().
@G_TEST_TRAP_INHERIT_STDIN: If this flag is given, stdin of the forked 
    child process is shared with stdin of its parent process. It is 
    redirected to <filename>/dev/null</filename> otherwise.
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333

<!-- ##### FUNCTION g_test_trap_fork ##### -->
<para>

</para>

@usec_timeout: 
@test_trap_flags: 
@Returns: 


<!-- ##### FUNCTION g_test_trap_has_passed ##### -->
<para>

</para>

Matthias Clasen's avatar
2.25.0  
Matthias Clasen committed
334
@void: 
335 336 337 338 339 340 341 342
@Returns: 


<!-- ##### FUNCTION g_test_trap_reached_timeout ##### -->
<para>

</para>

Matthias Clasen's avatar
2.25.0  
Matthias Clasen committed
343
@void: 
344 345 346 347 348
@Returns: 


<!-- ##### MACRO g_test_trap_assert_passed ##### -->
<para>
Matthias Clasen's avatar
Matthias Clasen committed
349
Assert that the last forked test passed. See g_test_trap_fork().
350 351
</para>

Matthias Clasen's avatar
Matthias Clasen committed
352
@Since: 2.16
353 354 355 356


<!-- ##### MACRO g_test_trap_assert_failed ##### -->
<para>
Matthias Clasen's avatar
Matthias Clasen committed
357
Assert that the last forked test failed. See g_test_trap_fork().
358 359
</para>

Matthias Clasen's avatar
Matthias Clasen committed
360
@Since: 2.16
361 362 363 364


<!-- ##### MACRO g_test_trap_assert_stdout ##### -->
<para>
Matthias Clasen's avatar
Matthias Clasen committed
365 366
Assert that the stdout output of the last forked test matches @soutpattern. 
See g_test_trap_fork().
367 368
</para>

Matthias Clasen's avatar
Matthias Clasen committed
369 370
@soutpattern: a glob-style <link linkend="glib-Glob-style-pattern-matching">pattern</link>
@Since: 2.16
371 372 373 374


<!-- ##### MACRO g_test_trap_assert_stdout_unmatched ##### -->
<para>
Matthias Clasen's avatar
Matthias Clasen committed
375 376
Assert that the stdout output of the last forked test does not match 
@soutpattern.  See g_test_trap_fork().
377 378
</para>

Matthias Clasen's avatar
Matthias Clasen committed
379 380
@soutpattern: a glob-style <link linkend="glib-Glob-style-pattern-matching">pattern</link>
@Since: 2.16
381 382 383 384


<!-- ##### MACRO g_test_trap_assert_stderr ##### -->
<para>
Matthias Clasen's avatar
Matthias Clasen committed
385 386
Assert that the stderr output of the last forked test matches @serrpattern. 
See g_test_trap_fork().
387 388
</para>

Matthias Clasen's avatar
Matthias Clasen committed
389 390
@serrpattern: a glob-style <link linkend="glib-Glob-style-pattern-matching">pattern</link>
@Since: 2.16
391 392 393 394


<!-- ##### MACRO g_test_trap_assert_stderr_unmatched ##### -->
<para>
Matthias Clasen's avatar
Matthias Clasen committed
395 396
Assert that the stderr output of the last forked test does not match 
@serrpattern.  See g_test_trap_fork().
397 398
</para>

Matthias Clasen's avatar
Matthias Clasen committed
399 400
@serrpattern: a glob-style <link linkend="glib-Glob-style-pattern-matching">pattern</link>
@Since: 2.16
401 402 403 404


<!-- ##### MACRO g_test_rand_bit ##### -->
<para>
Matthias Clasen's avatar
Matthias Clasen committed
405 406
Get a reproducible random bit (0 or 1),
see g_test_rand_int() for details on test case random numbers.
407 408
</para>

Matthias Clasen's avatar
Matthias Clasen committed
409
@Since: 2.16
410 411 412 413 414 415 416


<!-- ##### FUNCTION g_test_rand_int ##### -->
<para>

</para>

Matthias Clasen's avatar
2.25.0  
Matthias Clasen committed
417
@void: 
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
@Returns: 


<!-- ##### FUNCTION g_test_rand_int_range ##### -->
<para>

</para>

@begin: 
@end: 
@Returns: 


<!-- ##### FUNCTION g_test_rand_double ##### -->
<para>

</para>

Matthias Clasen's avatar
2.25.0  
Matthias Clasen committed
436
@void: 
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
@Returns: 


<!-- ##### FUNCTION g_test_rand_double_range ##### -->
<para>

</para>

@range_start: 
@range_end: 
@Returns: 


<!-- ##### MACRO g_assert ##### -->
<para>
Matthias Clasen's avatar
Matthias Clasen committed
452 453 454 455 456 457 458
Debugging macro to terminate the application if the assertion fails.
If the assertion fails (i.e. the expression is not true), an error message
is logged and the application is terminated.
</para>
<para>
The macro can be turned off in final releases of code by defining
#G_DISABLE_ASSERT when compiling the application.
459 460
</para>

Matthias Clasen's avatar
Matthias Clasen committed
461
@expr: the expression to check.
462 463 464 465


<!-- ##### MACRO g_assert_not_reached ##### -->
<para>
Matthias Clasen's avatar
Matthias Clasen committed
466 467 468 469 470 471
Debugging macro to terminate the application if it is ever reached.
If it is reached, an error message is logged and the application is terminated.
</para>
<para>
The macro can be turned off in final releases of code by defining
#G_DISABLE_ASSERT when compiling the application.
472 473 474 475 476 477
</para>



<!-- ##### MACRO g_assert_cmpstr ##### -->
<para>
Matthias Clasen's avatar
Matthias Clasen committed
478 479 480
Debugging macro to terminate the application with a warning message 
if a string comparison fails.
The strings are compared using g_strcmp0().
481
</para>
Matthias Clasen's avatar
Matthias Clasen committed
482 483
<para>
The effect of <literal>g_assert_cmpstr (s1, op, s2)</literal> is the same
484
as <literal>g_assert (g_strcmp0 (s1, s2) op 0)</literal>. The advantage of this macro
Matthias Clasen's avatar
Matthias Clasen committed
485 486 487 488 489 490
is that it can produce a message that includes the actual values of @s1
and @s2.
</para>
<informalexample><programlisting>
  g_assert_cmpstr (mystring, ==, "fubar");
</programlisting></informalexample>
491

Matthias Clasen's avatar
Matthias Clasen committed
492
@s1: a string (may be %NULL)
493
@cmp: The comparison operator to use. One of ==, !=, &lt;, &gt;, &lt;=, &gt;=.
Matthias Clasen's avatar
Matthias Clasen committed
494
@s2: another string (may be %NULL)
Matthias Clasen's avatar
Matthias Clasen committed
495
@Since: 2.16
496 497 498 499


<!-- ##### MACRO g_assert_cmpint ##### -->
<para>
Matthias Clasen's avatar
Matthias Clasen committed
500 501 502 503 504 505 506 507
Debugging macro to terminate the application with a warning message 
if an integer comparison fails.
</para>
<para>
The effect of <literal>g_assert_cmpint (n1, op, n2)</literal> is the same
as <literal>g_assert (n1 op n2)</literal>. The advantage of this macro
is that it can produce a message that includes the actual values of @n1
and @n2.
508 509
</para>

Matthias Clasen's avatar
Matthias Clasen committed
510
@n1: an integer
511
@cmp: The comparison operator to use. One of ==, !=, &lt;, &gt;, &lt;=, &gt;=.
Matthias Clasen's avatar
Matthias Clasen committed
512
@n2: another integer
Matthias Clasen's avatar
Matthias Clasen committed
513
@Since: 2.16
514 515 516 517


<!-- ##### MACRO g_assert_cmpuint ##### -->
<para>
Matthias Clasen's avatar
Matthias Clasen committed
518 519 520 521 522 523 524 525
Debugging macro to terminate the application with a warning message 
if an unsigned integer comparison fails.
</para>
<para>
The effect of <literal>g_assert_cmpuint (n1, op, n2)</literal> is the same
as <literal>g_assert (n1 op n2)</literal>. The advantage of this macro
is that it can produce a message that includes the actual values of @n1
and @n2.
526 527
</para>

Matthias Clasen's avatar
Matthias Clasen committed
528
@n1: an unsigned integer
529
@cmp: The comparison operator to use. One of ==, !=, &lt;, &gt;, &lt;=, &gt;=.
Matthias Clasen's avatar
Matthias Clasen committed
530
@n2: another unsigned integer
Matthias Clasen's avatar
Matthias Clasen committed
531
@Since: 2.16
532 533 534 535


<!-- ##### MACRO g_assert_cmphex ##### -->
<para>
Matthias Clasen's avatar
Matthias Clasen committed
536 537 538 539
Debugging macro to terminate the application with a warning message 
if an unsigned integer comparison fails. This is a variant of
g_assert_cmpuint() that displays the numbers in hexadecimal notation
in the message.
540 541
</para>

Matthias Clasen's avatar
Matthias Clasen committed
542
@n1: an unsigned integer
543
@cmp: The comparison operator to use. One of ==, !=, &lt;, &gt;, &lt;=, &gt;=.
Matthias Clasen's avatar
Matthias Clasen committed
544
@n2: another unsigned integer
Matthias Clasen's avatar
Matthias Clasen committed
545
@Since: 2.16
546 547 548 549


<!-- ##### MACRO g_assert_cmpfloat ##### -->
<para>
Matthias Clasen's avatar
Matthias Clasen committed
550 551 552 553
Debugging macro to terminate the application with a warning message 
if a floating point number comparison fails.
</para>
<para>
554
The effect of <literal>g_assert_cmpfloat (n1, op, n2)</literal> is the same
Matthias Clasen's avatar
Matthias Clasen committed
555 556 557
as <literal>g_assert (n1 op n2)</literal>. The advantage of this function
is that it can produce a message that includes the actual values of @n1
and @n2.
558 559
</para>

Matthias Clasen's avatar
Matthias Clasen committed
560
@n1: an floating point number
561
@cmp: The comparison operator to use. One of ==, !=, &lt;, &gt;, &lt;=, &gt;=.
Matthias Clasen's avatar
Matthias Clasen committed
562
@n2: another floating point number
Matthias Clasen's avatar
Matthias Clasen committed
563
@Since: 2.16
564 565


566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
<!-- ##### MACRO g_assert_no_error ##### -->
<para>
Debugging macro to terminate the application with a warning message 
if a method has returned a #GError.
</para>
<para>
The effect of <literal>g_assert_no_error (err)</literal> is the same
as <literal>g_assert (err == NULL)</literal>. The advantage of this macro
is that it can produce a message that includes the error message and code.
</para>

@err: a #GError, possibly %NULL
@Since: 2.20


<!-- ##### MACRO g_assert_error ##### -->
<para>
Debugging macro to terminate the application with a warning message 
if a method has not returned the correct #GError.
</para>
<para>
The effect of <literal>g_assert_error (err, dom, c)</literal> is the same
as <literal>g_assert (err != NULL &amp;&amp; err->domain == dom &amp;&amp; err->code == c)</literal>.
The advantage of this macro is that it can produce a message that
includes the incorrect error message and code.
</para>
<para>
This can only be used to test for a specific error. If you want to
test that @err is set, but don't care what it's set to, just use
<literal>g_assert (err != NULL)</literal>
</para>

@err: a #GError, possibly %NULL
@dom: the expected error domain (a #GQuark)
@c: the expected error code
@Since: 2.20


Matthias Clasen's avatar
Matthias Clasen committed
604 605 606 607 608 609 610 611 612 613 614 615
<!-- ##### TYPEDEF GTestCase ##### -->
<para>
An opaque structure representing a test case.
</para>


<!-- ##### TYPEDEF GTestSuite ##### -->
<para>
An opaque structure representing a test suite.
</para>


616 617 618 619 620 621 622 623 624
<!-- ##### USER_FUNCTION GTestFixtureFunc ##### -->
<para>

</para>

@fixture: 
@user_data: 


Matthias Clasen's avatar
Matthias Clasen committed
625 626 627 628 629 630 631 632
<!-- ##### FUNCTION g_test_create_case ##### -->
<para>

</para>

@test_name: 
@data_size: 
@test_data: 
633 634 635
@data_setup: 
@data_test: 
@data_teardown: 
Matthias Clasen's avatar
Matthias Clasen committed
636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652
@Returns: 


<!-- ##### FUNCTION g_test_create_suite ##### -->
<para>

</para>

@suite_name: 
@Returns: 


<!-- ##### FUNCTION g_test_get_root ##### -->
<para>

</para>

Matthias Clasen's avatar
2.25.0  
Matthias Clasen committed
653
@void: 
Matthias Clasen's avatar
Matthias Clasen committed
654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683
@Returns: 


<!-- ##### FUNCTION g_test_suite_add ##### -->
<para>

</para>

@suite: 
@test_case: 


<!-- ##### FUNCTION g_test_suite_add_suite ##### -->
<para>

</para>

@suite: 
@nestedsuite: 


<!-- ##### FUNCTION g_test_run_suite ##### -->
<para>

</para>

@suite: 
@Returns: