1 <!-- ##### SECTION Title ##### -->
4 <!-- ##### SECTION Short_Description ##### -->
7 <!-- ##### SECTION Long_Description ##### -->
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.
15 <term>Test case</term>
17 Tests (test methods) are grouped together with their
18 fixture into test cases.
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.
31 <term>Test suite</term>
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
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.
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.
56 GLib ships with two utilities called gtester and gtester-report to
57 facilitate running tests and producing nicely formatted test reports.
60 <!-- ##### SECTION See_Also ##### -->
62 <link linkend="gtester">gtester</link>,
63 <link linkend="gtester-report">gtester-report</link>
66 <!-- ##### SECTION Stability_Level ##### -->
69 <!-- ##### FUNCTION g_test_minimized_result ##### -->
79 <!-- ##### FUNCTION g_test_maximized_result ##### -->
89 <!-- ##### FUNCTION g_test_init ##### -->
99 <!-- ##### MACRO g_test_quick ##### -->
101 Returns %TRUE if tests are run in quick mode.
106 <!-- ##### MACRO g_test_slow ##### -->
108 Returns %TRUE if tests are run in slow mode.
113 <!-- ##### MACRO g_test_thorough ##### -->
115 Returns %TRUE if tests are run in thorough mode.
120 <!-- ##### MACRO g_test_perf ##### -->
122 Returns %TRUE if tests are run in performance mode.
127 <!-- ##### MACRO g_test_verbose ##### -->
129 Returns %TRUE if tests are run in verbose mode.
134 <!-- ##### MACRO g_test_quiet ##### -->
136 Returns %TRUE if tests are run in quiet mode.
141 <!-- ##### FUNCTION g_test_run ##### -->
149 <!-- ##### FUNCTION g_test_add_func ##### -->
158 <!-- ##### FUNCTION g_test_add_data_func ##### -->
168 <!-- ##### MACRO g_test_add ##### -->
181 <!-- ##### FUNCTION g_test_message ##### -->
190 <!-- ##### FUNCTION g_test_bug_base ##### -->
198 <!-- ##### FUNCTION g_test_bug ##### -->
206 <!-- ##### USER_FUNCTION GTestLogFatalFunc ##### -->
218 <!-- ##### FUNCTION g_test_log_set_fatal_handler ##### -->
227 <!-- ##### FUNCTION g_test_timer_start ##### -->
234 <!-- ##### FUNCTION g_test_timer_elapsed ##### -->
242 <!-- ##### FUNCTION g_test_timer_last ##### -->
250 <!-- ##### FUNCTION g_test_queue_free ##### -->
258 <!-- ##### FUNCTION g_test_queue_destroy ##### -->
267 <!-- ##### MACRO g_test_queue_unref ##### -->
269 Enqueue an object to be released with g_object_unref() during
270 the next teardown phase. This is equivalent to calling g_test_queue_destroy()
271 with a destroy callback of g_object_unref().
274 @gobject: the object to unref
278 <!-- ##### ENUM GTestTrapFlags ##### -->
280 Test traps are guards around forked tests. These flags
281 determine what traps to set.
284 @G_TEST_TRAP_SILENCE_STDOUT: Redirect stdout of the test child to
285 <filename>/dev/null</filename> so it cannot be observed on the
286 console during test runs. The actual output is still captured
287 though to allow later tests with g_test_trap_assert_stdout().
288 @G_TEST_TRAP_SILENCE_STDERR: Redirect stderr of the test child to
289 <filename>/dev/null</filename> so it cannot be observed on the
290 console during test runs. The actual output is still captured
291 though to allow later tests with g_test_trap_assert_stderr().
292 @G_TEST_TRAP_INHERIT_STDIN: If this flag is given, stdin of the forked
293 child process is shared with stdin of its parent process. It is
294 redirected to <filename>/dev/null</filename> otherwise.
296 <!-- ##### FUNCTION g_test_trap_fork ##### -->
306 <!-- ##### FUNCTION g_test_trap_has_passed ##### -->
314 <!-- ##### FUNCTION g_test_trap_reached_timeout ##### -->
322 <!-- ##### MACRO g_test_trap_assert_passed ##### -->
324 Assert that the last forked test passed. See g_test_trap_fork().
330 <!-- ##### MACRO g_test_trap_assert_failed ##### -->
332 Assert that the last forked test failed. See g_test_trap_fork().
338 <!-- ##### MACRO g_test_trap_assert_stdout ##### -->
340 Assert that the stdout output of the last forked test matches @soutpattern.
341 See g_test_trap_fork().
344 @soutpattern: a glob-style <link linkend="glib-Glob-style-pattern-matching">pattern</link>
348 <!-- ##### MACRO g_test_trap_assert_stdout_unmatched ##### -->
350 Assert that the stdout output of the last forked test does not match
351 @soutpattern. See g_test_trap_fork().
354 @soutpattern: a glob-style <link linkend="glib-Glob-style-pattern-matching">pattern</link>
358 <!-- ##### MACRO g_test_trap_assert_stderr ##### -->
360 Assert that the stderr output of the last forked test matches @serrpattern.
361 See g_test_trap_fork().
364 @serrpattern: a glob-style <link linkend="glib-Glob-style-pattern-matching">pattern</link>
368 <!-- ##### MACRO g_test_trap_assert_stderr_unmatched ##### -->
370 Assert that the stderr output of the last forked test does not match
371 @serrpattern. See g_test_trap_fork().
374 @serrpattern: a glob-style <link linkend="glib-Glob-style-pattern-matching">pattern</link>
378 <!-- ##### MACRO g_test_rand_bit ##### -->
380 Get a reproducible random bit (0 or 1),
381 see g_test_rand_int() for details on test case random numbers.
387 <!-- ##### FUNCTION g_test_rand_int ##### -->
395 <!-- ##### FUNCTION g_test_rand_int_range ##### -->
405 <!-- ##### FUNCTION g_test_rand_double ##### -->
413 <!-- ##### FUNCTION g_test_rand_double_range ##### -->
423 <!-- ##### MACRO g_assert ##### -->
425 Debugging macro to terminate the application if the assertion fails.
426 If the assertion fails (i.e. the expression is not true), an error message
427 is logged and the application is terminated.
430 The macro can be turned off in final releases of code by defining
431 #G_DISABLE_ASSERT when compiling the application.
434 @expr: the expression to check.
437 <!-- ##### MACRO g_assert_not_reached ##### -->
439 Debugging macro to terminate the application if it is ever reached.
440 If it is reached, an error message is logged and the application is terminated.
443 The macro can be turned off in final releases of code by defining
444 #G_DISABLE_ASSERT when compiling the application.
449 <!-- ##### MACRO g_assert_cmpstr ##### -->
451 Debugging macro to terminate the application with a warning message
452 if a string comparison fails.
453 The strings are compared using g_strcmp0().
456 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
458 is that it can produce a message that includes the actual values of @s1
461 <informalexample><programlisting>
462 g_assert_cmpstr (mystring, ==, "fubar");
463 </programlisting></informalexample>
465 @s1: a string (may be %NULL)
466 @cmp: The comparison operator to use. One of ==, !=, <, >, <=, >=.
467 @s2: another string (may be %NULL)
471 <!-- ##### MACRO g_assert_cmpint ##### -->
473 Debugging macro to terminate the application with a warning message
474 if an integer comparison fails.
477 The effect of <literal>g_assert_cmpint (n1, op, n2)</literal> is the same
478 as <literal>g_assert (n1 op n2)</literal>. The advantage of this macro
479 is that it can produce a message that includes the actual values of @n1
484 @cmp: The comparison operator to use. One of ==, !=, <, >, <=, >=.
489 <!-- ##### MACRO g_assert_cmpuint ##### -->
491 Debugging macro to terminate the application with a warning message
492 if an unsigned integer comparison fails.
495 The effect of <literal>g_assert_cmpuint (n1, op, n2)</literal> is the same
496 as <literal>g_assert (n1 op n2)</literal>. The advantage of this macro
497 is that it can produce a message that includes the actual values of @n1
501 @n1: an unsigned integer
502 @cmp: The comparison operator to use. One of ==, !=, <, >, <=, >=.
503 @n2: another unsigned integer
507 <!-- ##### MACRO g_assert_cmphex ##### -->
509 Debugging macro to terminate the application with a warning message
510 if an unsigned integer comparison fails. This is a variant of
511 g_assert_cmpuint() that displays the numbers in hexadecimal notation
515 @n1: an unsigned integer
516 @cmp: The comparison operator to use. One of ==, !=, <, >, <=, >=.
517 @n2: another unsigned integer
521 <!-- ##### MACRO g_assert_cmpfloat ##### -->
523 Debugging macro to terminate the application with a warning message
524 if a floating point number comparison fails.
527 The effect of <literal>g_assert_cmpfloat (n1, op, n2)</literal> is the same
528 as <literal>g_assert (n1 op n2)</literal>. The advantage of this function
529 is that it can produce a message that includes the actual values of @n1
533 @n1: an floating point number
534 @cmp: The comparison operator to use. One of ==, !=, <, >, <=, >=.
535 @n2: another floating point number
539 <!-- ##### MACRO g_assert_no_error ##### -->
541 Debugging macro to terminate the application with a warning message
542 if a method has returned a #GError.
545 The effect of <literal>g_assert_no_error (err)</literal> is the same
546 as <literal>g_assert (err == NULL)</literal>. The advantage of this macro
547 is that it can produce a message that includes the error message and code.
550 @err: a #GError, possibly %NULL
554 <!-- ##### MACRO g_assert_error ##### -->
556 Debugging macro to terminate the application with a warning message
557 if a method has not returned the correct #GError.
560 The effect of <literal>g_assert_error (err, dom, c)</literal> is the same
561 as <literal>g_assert (err != NULL && err->domain == dom && err->code == c)</literal>.
562 The advantage of this macro is that it can produce a message that
563 includes the incorrect error message and code.
566 This can only be used to test for a specific error. If you want to
567 test that @err is set, but don't care what it's set to, just use
568 <literal>g_assert (err != NULL)</literal>
571 @err: a #GError, possibly %NULL
572 @dom: the expected error domain (a #GQuark)
573 @c: the expected error code
577 <!-- ##### TYPEDEF GTestCase ##### -->
579 An opaque structure representing a test case.
583 <!-- ##### TYPEDEF GTestSuite ##### -->
585 An opaque structure representing a test suite.
589 <!-- ##### FUNCTION g_test_create_case ##### -->
603 <!-- ##### FUNCTION g_test_create_suite ##### -->
612 <!-- ##### FUNCTION g_test_get_root ##### -->
620 <!-- ##### FUNCTION g_test_suite_add ##### -->
629 <!-- ##### FUNCTION g_test_suite_add_suite ##### -->
638 <!-- ##### FUNCTION g_test_run_suite ##### -->