testing.sgml 13.2 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 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
</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: 


<!-- ##### 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
248 249 250
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().
251 252
</para>

Matthias Clasen's avatar
Matthias Clasen committed
253 254
@gobject: the object to unref
@Since: 2.16
255 256 257 258


<!-- ##### ENUM GTestTrapFlags ##### -->
<para>
Matthias Clasen's avatar
Matthias Clasen committed
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
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.
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302

<!-- ##### 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
303
Assert that the last forked test passed. See g_test_trap_fork().
304 305
</para>

Matthias Clasen's avatar
Matthias Clasen committed
306
@Since: 2.16
307 308 309 310


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

Matthias Clasen's avatar
Matthias Clasen committed
314
@Since: 2.16
315 316 317 318


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

Matthias Clasen's avatar
Matthias Clasen committed
323 324
@soutpattern: a glob-style <link linkend="glib-Glob-style-pattern-matching">pattern</link>
@Since: 2.16
325 326 327 328


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

Matthias Clasen's avatar
Matthias Clasen committed
333 334
@soutpattern: a glob-style <link linkend="glib-Glob-style-pattern-matching">pattern</link>
@Since: 2.16
335 336 337 338


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

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


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

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


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

Matthias Clasen's avatar
Matthias Clasen committed
363
@Since: 2.16
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403


<!-- ##### 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
404 405 406 407 408 409 410
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.
411 412
</para>

Matthias Clasen's avatar
Matthias Clasen committed
413
@expr: the expression to check.
414 415 416 417


<!-- ##### MACRO g_assert_not_reached ##### -->
<para>
Matthias Clasen's avatar
Matthias Clasen committed
418 419 420 421 422 423
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.
424 425 426 427 428 429
</para>



<!-- ##### MACRO g_assert_cmpstr ##### -->
<para>
Matthias Clasen's avatar
Matthias Clasen committed
430 431 432
Debugging macro to terminate the application with a warning message 
if a string comparison fails.
The strings are compared using g_strcmp0().
433
</para>
Matthias Clasen's avatar
Matthias Clasen committed
434 435
<para>
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
Matthias Clasen's avatar
Matthias Clasen committed
437 438 439 440 441 442
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>
443

Matthias Clasen's avatar
Matthias Clasen committed
444
@s1: a string (may be %NULL)
445
@cmp: The comparison operator to use. One of ==, !=, &lt;, &gt;, &lt;=, &gt;=.
Matthias Clasen's avatar
Matthias Clasen committed
446
@s2: another string (may be %NULL)
Matthias Clasen's avatar
Matthias Clasen committed
447
@Since: 2.16
448 449 450 451


<!-- ##### MACRO g_assert_cmpint ##### -->
<para>
Matthias Clasen's avatar
Matthias Clasen committed
452 453 454 455 456 457 458 459
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.
460 461
</para>

Matthias Clasen's avatar
Matthias Clasen committed
462
@n1: an integer
463
@cmp: The comparison operator to use. One of ==, !=, &lt;, &gt;, &lt;=, &gt;=.
Matthias Clasen's avatar
Matthias Clasen committed
464
@n2: another integer
Matthias Clasen's avatar
Matthias Clasen committed
465
@Since: 2.16
466 467 468 469


<!-- ##### MACRO g_assert_cmpuint ##### -->
<para>
Matthias Clasen's avatar
Matthias Clasen committed
470 471 472 473 474 475 476 477
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.
478 479
</para>

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


<!-- ##### MACRO g_assert_cmphex ##### -->
<para>
Matthias Clasen's avatar
Matthias Clasen committed
488 489 490 491
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.
492 493
</para>

Matthias Clasen's avatar
Matthias Clasen committed
494
@n1: an unsigned integer
495
@cmp: The comparison operator to use. One of ==, !=, &lt;, &gt;, &lt;=, &gt;=.
Matthias Clasen's avatar
Matthias Clasen committed
496
@n2: another unsigned integer
Matthias Clasen's avatar
Matthias Clasen committed
497
@Since: 2.16
498 499 500 501


<!-- ##### MACRO g_assert_cmpfloat ##### -->
<para>
Matthias Clasen's avatar
Matthias Clasen committed
502 503 504 505
Debugging macro to terminate the application with a warning message 
if a floating point number comparison fails.
</para>
<para>
506
The effect of <literal>g_assert_cmpfloat (n1, op, n2)</literal> is the same
Matthias Clasen's avatar
Matthias Clasen committed
507 508 509
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.
510 511
</para>

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


518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
<!-- ##### 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
556 557 558 559 560 561 562 563 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 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625
<!-- ##### 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: