testing.sgml 13.5 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 69 70 71 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
</para>

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


<!-- ##### 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
101
Returns %TRUE if tests are run in quick mode.
102 103 104 105 106 107
</para>



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



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



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



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



<!-- ##### MACRO g_test_quiet ##### -->
<para>
Matthias Clasen's avatar
Matthias Clasen committed
136
Returns %TRUE if tests are run in quiet mode.
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
</para>



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

</para>

@Returns: 


<!-- ##### FUNCTION g_test_add_func ##### -->
<para>

</para>

@testpath: 
@test_func: 


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

</para>

@testpath: 
@test_data: 
@test_func: 


<!-- ##### 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
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
<!-- ##### 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: 


227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
<!-- ##### FUNCTION g_test_timer_start ##### -->
<para>

</para>



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

</para>

@Returns: 


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

</para>

@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
269 270 271
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().
272 273
</para>

Matthias Clasen's avatar
Matthias Clasen committed
274 275
@gobject: the object to unref
@Since: 2.16
276 277 278 279


<!-- ##### ENUM GTestTrapFlags ##### -->
<para>
Matthias Clasen's avatar
Matthias Clasen committed
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
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.
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323

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

</para>

@usec_timeout: 
@test_trap_flags: 
@Returns: 


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

</para>

@Returns: 


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

</para>

@Returns: 


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

Matthias Clasen's avatar
Matthias Clasen committed
327
@Since: 2.16
328 329 330 331


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

Matthias Clasen's avatar
Matthias Clasen committed
335
@Since: 2.16
336 337 338 339


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

Matthias Clasen's avatar
Matthias Clasen committed
344 345
@soutpattern: a glob-style <link linkend="glib-Glob-style-pattern-matching">pattern</link>
@Since: 2.16
346 347 348 349


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

Matthias Clasen's avatar
Matthias Clasen committed
354 355
@soutpattern: a glob-style <link linkend="glib-Glob-style-pattern-matching">pattern</link>
@Since: 2.16
356 357 358 359


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

Matthias Clasen's avatar
Matthias Clasen committed
364 365
@serrpattern: a glob-style <link linkend="glib-Glob-style-pattern-matching">pattern</link>
@Since: 2.16
366 367 368 369


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

Matthias Clasen's avatar
Matthias Clasen committed
374 375
@serrpattern: a glob-style <link linkend="glib-Glob-style-pattern-matching">pattern</link>
@Since: 2.16
376 377 378 379


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

Matthias Clasen's avatar
Matthias Clasen committed
384
@Since: 2.16
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424


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

</para>

@Returns: 


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

</para>

@begin: 
@end: 
@Returns: 


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

</para>

@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
425 426 427 428 429 430 431
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.
432 433
</para>

Matthias Clasen's avatar
Matthias Clasen committed
434
@expr: the expression to check.
435 436 437 438


<!-- ##### MACRO g_assert_not_reached ##### -->
<para>
Matthias Clasen's avatar
Matthias Clasen committed
439 440 441 442 443 444
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.
445 446 447 448 449 450
</para>



<!-- ##### MACRO g_assert_cmpstr ##### -->
<para>
Matthias Clasen's avatar
Matthias Clasen committed
451 452 453
Debugging macro to terminate the application with a warning message 
if a string comparison fails.
The strings are compared using g_strcmp0().
454
</para>
Matthias Clasen's avatar
Matthias Clasen committed
455 456
<para>
The effect of <literal>g_assert_cmpstr (s1, op, s2)</literal> is the same
457
as <literal>g_assert (g_strcmp0 (s1, s2) op 0)</literal>. The advantage of this macro
Matthias Clasen's avatar
Matthias Clasen committed
458 459 460 461 462 463
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>
464

Matthias Clasen's avatar
Matthias Clasen committed
465
@s1: a string (may be %NULL)
466
@cmp: The comparison operator to use. One of ==, !=, &lt;, &gt;, &lt;=, &gt;=.
Matthias Clasen's avatar
Matthias Clasen committed
467
@s2: another string (may be %NULL)
Matthias Clasen's avatar
Matthias Clasen committed
468
@Since: 2.16
469 470 471 472


<!-- ##### MACRO g_assert_cmpint ##### -->
<para>
Matthias Clasen's avatar
Matthias Clasen committed
473 474 475 476 477 478 479 480
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.
481 482
</para>

Matthias Clasen's avatar
Matthias Clasen committed
483
@n1: an integer
484
@cmp: The comparison operator to use. One of ==, !=, &lt;, &gt;, &lt;=, &gt;=.
Matthias Clasen's avatar
Matthias Clasen committed
485
@n2: another integer
Matthias Clasen's avatar
Matthias Clasen committed
486
@Since: 2.16
487 488 489 490


<!-- ##### MACRO g_assert_cmpuint ##### -->
<para>
Matthias Clasen's avatar
Matthias Clasen committed
491 492 493 494 495 496 497 498
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.
499 500
</para>

Matthias Clasen's avatar
Matthias Clasen committed
501
@n1: an unsigned integer
502
@cmp: The comparison operator to use. One of ==, !=, &lt;, &gt;, &lt;=, &gt;=.
Matthias Clasen's avatar
Matthias Clasen committed
503
@n2: another unsigned integer
Matthias Clasen's avatar
Matthias Clasen committed
504
@Since: 2.16
505 506 507 508


<!-- ##### MACRO g_assert_cmphex ##### -->
<para>
Matthias Clasen's avatar
Matthias Clasen committed
509 510 511 512
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.
513 514
</para>

Matthias Clasen's avatar
Matthias Clasen committed
515
@n1: an unsigned integer
516
@cmp: The comparison operator to use. One of ==, !=, &lt;, &gt;, &lt;=, &gt;=.
Matthias Clasen's avatar
Matthias Clasen committed
517
@n2: another unsigned integer
Matthias Clasen's avatar
Matthias Clasen committed
518
@Since: 2.16
519 520 521 522


<!-- ##### MACRO g_assert_cmpfloat ##### -->
<para>
Matthias Clasen's avatar
Matthias Clasen committed
523 524 525 526
Debugging macro to terminate the application with a warning message 
if a floating point number comparison fails.
</para>
<para>
527
The effect of <literal>g_assert_cmpfloat (n1, op, n2)</literal> is the same
Matthias Clasen's avatar
Matthias Clasen committed
528 529 530
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.
531 532
</para>

Matthias Clasen's avatar
Matthias Clasen committed
533
@n1: an floating point number
534
@cmp: The comparison operator to use. One of ==, !=, &lt;, &gt;, &lt;=, &gt;=.
Matthias Clasen's avatar
Matthias Clasen committed
535
@n2: another floating point number
Matthias Clasen's avatar
Matthias Clasen committed
536
@Since: 2.16
537 538


539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
<!-- ##### 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
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 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646
<!-- ##### TYPEDEF GTestCase ##### -->
<para>
An opaque structure representing a test case.
</para>


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


<!-- ##### FUNCTION g_test_create_case ##### -->
<para>

</para>

@test_name: 
@data_size: 
@test_data: 
@data_setup: 
@data_test: 
@data_teardown: 
@Returns: 


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

</para>

@suite_name: 
@Returns: 


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

</para>

@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: