testing.sgml 10.1 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 27 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 56 57
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 
      invokation to avoid dependencies between tests.
    </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>
GLib ships with two utilites called gtester and gtester-report to 
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 quite 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 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 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 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
</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>

</para>

@gobject: 


<!-- ##### ENUM GTestTrapFlags ##### -->
<para>

</para>

@G_TEST_TRAP_SILENCE_STDOUT: 
@G_TEST_TRAP_SILENCE_STDERR: 
@G_TEST_TRAP_INHERIT_STDIN: 

<!-- ##### 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>

</para>



<!-- ##### MACRO g_test_trap_assert_failed ##### -->
<para>

</para>



<!-- ##### MACRO g_test_trap_assert_stdout ##### -->
<para>

</para>

@soutpattern: 


<!-- ##### MACRO g_test_trap_assert_stdout_unmatched ##### -->
<para>

</para>

@soutpattern: 


<!-- ##### MACRO g_test_trap_assert_stderr ##### -->
<para>

</para>

@serrpattern: 


<!-- ##### MACRO g_test_trap_assert_stderr_unmatched ##### -->
<para>

</para>

@serrpattern: 


<!-- ##### MACRO g_test_rand_bit ##### -->
<para>

</para>



<!-- ##### 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
380 381 382 383 384 385 386
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.
387 388
</para>

Matthias Clasen's avatar
Matthias Clasen committed
389
@expr: the expression to check.
390 391 392 393


<!-- ##### MACRO g_assert_not_reached ##### -->
<para>
Matthias Clasen's avatar
Matthias Clasen committed
394 395 396 397 398 399
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.
400 401 402 403 404 405
</para>



<!-- ##### MACRO g_assert_cmpstr ##### -->
<para>
Matthias Clasen's avatar
Matthias Clasen committed
406 407 408
Debugging macro to terminate the application with a warning message 
if a string comparison fails.
The strings are compared using g_strcmp0().
409
</para>
Matthias Clasen's avatar
Matthias Clasen committed
410 411 412 413 414 415 416 417 418
<para>
The effect of <literal>g_assert_cmpstr (s1, op, s2)</literal> is the same
as <literal>g_assert (s1 op s2)</literal>. The advantage of this macro
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>
419

Matthias Clasen's avatar
Matthias Clasen committed
420 421 422 423
@s1: a string (may be %NULL)
@cmp: The comparsion operator to use. One of ==, !=, &lt;, &gt;, &lt;=, &gt;=.
@s2: another string (may be %NULL)
Since: 2.16
424 425 426 427


<!-- ##### MACRO g_assert_cmpint ##### -->
<para>
Matthias Clasen's avatar
Matthias Clasen committed
428 429 430 431 432 433 434 435
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.
436 437
</para>

Matthias Clasen's avatar
Matthias Clasen committed
438 439 440 441
@n1: an integer
@cmp: The comparsion operator to use. One of ==, !=, &lt;, &gt;, &lt;=, &gt;=.
@n2: another integer
Since: 2.16
442 443 444 445


<!-- ##### MACRO g_assert_cmpuint ##### -->
<para>
Matthias Clasen's avatar
Matthias Clasen committed
446 447 448 449 450 451 452 453
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.
454 455
</para>

Matthias Clasen's avatar
Matthias Clasen committed
456 457 458 459
@n1: an unsigned integer
@cmp: The comparsion operator to use. One of ==, !=, &lt;, &gt;, &lt;=, &gt;=.
@n2: another unsigned integer
Since: 2.16
460 461 462 463


<!-- ##### MACRO g_assert_cmphex ##### -->
<para>
Matthias Clasen's avatar
Matthias Clasen committed
464 465 466 467
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.
468 469
</para>

Matthias Clasen's avatar
Matthias Clasen committed
470 471 472 473
@n1: an unsigned integer
@cmp: The comparsion operator to use. One of ==, !=, &lt;, &gt;, &lt;=, &gt;=.
@n2: another unsigned integer
Since: 2.16
474 475 476 477


<!-- ##### MACRO g_assert_cmpfloat ##### -->
<para>
Matthias Clasen's avatar
Matthias Clasen committed
478 479 480 481 482 483 484 485
Debugging macro to terminate the application with a warning message 
if a floating point number comparison fails.
</para>
<para>
The effect of <literal>g_assert_cmpflott (n1, op, n2)</literal> is the same
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.
486 487
</para>

Matthias Clasen's avatar
Matthias Clasen committed
488 489 490 491
@n1: an floating point number
@cmp: The comparsion operator to use. One of ==, !=, &lt;, &gt;, &lt;=, &gt;=.
@n2: another floating point number
Since: 2.16
492 493 494 495 496 497 498 499 500 501 502 503


<!-- ##### FUNCTION g_strcmp0 ##### -->
<para>

</para>

@str1: 
@str2: 
@Returns: 


Matthias Clasen's avatar
Matthias Clasen committed
504 505 506 507 508 509 510 511 512 513 514 515 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 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
<!-- ##### 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: