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 invokation 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 utilites 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 quite 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 <!-- ##### FUNCTION g_test_timer_start ##### -->
213 <!-- ##### FUNCTION g_test_timer_elapsed ##### -->
221 <!-- ##### FUNCTION g_test_timer_last ##### -->
229 <!-- ##### FUNCTION g_test_queue_free ##### -->
237 <!-- ##### FUNCTION g_test_queue_destroy ##### -->
246 <!-- ##### MACRO g_test_queue_unref ##### -->
254 <!-- ##### ENUM GTestTrapFlags ##### -->
256 Test traps are guards around forked tests. These flags
257 determine what traps to set.
260 @G_TEST_TRAP_SILENCE_STDOUT: Redirect stdout of the test child to
261 <filename>/dev/null</filename> so it cannot be observed on the
262 console during test runs. The actual output is still captured
263 though to allow later tests with g_test_trap_assert_stdout().
265 @G_TEST_TRAP_SILENCE_STDERR: Redirect stderr of the test child to
266 <filename>/dev/null</filename> so it cannot be observed on the
267 console during test runs. The actual output is still captured
268 though to allow later tests with g_test_trap_assert_stderr().
270 @G_TEST_TRAP_INHERIT_STDIN: If this flag is given, stdin of the forked
271 child process is shared with stdin of its parent process. It is
272 redirected to <filename>/dev/null</filename> otherwise.
275 <!-- ##### FUNCTION g_test_trap_fork ##### -->
285 <!-- ##### FUNCTION g_test_trap_has_passed ##### -->
293 <!-- ##### FUNCTION g_test_trap_reached_timeout ##### -->
301 <!-- ##### MACRO g_test_trap_assert_passed ##### -->
308 <!-- ##### MACRO g_test_trap_assert_failed ##### -->
315 <!-- ##### MACRO g_test_trap_assert_stdout ##### -->
323 <!-- ##### MACRO g_test_trap_assert_stdout_unmatched ##### -->
331 <!-- ##### MACRO g_test_trap_assert_stderr ##### -->
339 <!-- ##### MACRO g_test_trap_assert_stderr_unmatched ##### -->
347 <!-- ##### MACRO g_test_rand_bit ##### -->
354 <!-- ##### FUNCTION g_test_rand_int ##### -->
362 <!-- ##### FUNCTION g_test_rand_int_range ##### -->
372 <!-- ##### FUNCTION g_test_rand_double ##### -->
380 <!-- ##### FUNCTION g_test_rand_double_range ##### -->
390 <!-- ##### MACRO g_assert ##### -->
392 Debugging macro to terminate the application if the assertion fails.
393 If the assertion fails (i.e. the expression is not true), an error message
394 is logged and the application is terminated.
397 The macro can be turned off in final releases of code by defining
398 #G_DISABLE_ASSERT when compiling the application.
401 @expr: the expression to check.
404 <!-- ##### MACRO g_assert_not_reached ##### -->
406 Debugging macro to terminate the application if it is ever reached.
407 If it is reached, an error message is logged and the application is terminated.
410 The macro can be turned off in final releases of code by defining
411 #G_DISABLE_ASSERT when compiling the application.
416 <!-- ##### MACRO g_assert_cmpstr ##### -->
418 Debugging macro to terminate the application with a warning message
419 if a string comparison fails.
420 The strings are compared using g_strcmp0().
423 The effect of <literal>g_assert_cmpstr (s1, op, s2)</literal> is the same
424 as <literal>g_assert (s1 op s2)</literal>. The advantage of this macro
425 is that it can produce a message that includes the actual values of @s1
428 <informalexample><programlisting>
429 g_assert_cmpstr (mystring, ==, "fubar");
430 </programlisting></informalexample>
432 @s1: a string (may be %NULL)
433 @cmp: The comparsion operator to use. One of ==, !=, <, >, <=, >=.
434 @s2: another string (may be %NULL)
438 <!-- ##### MACRO g_assert_cmpint ##### -->
440 Debugging macro to terminate the application with a warning message
441 if an integer comparison fails.
444 The effect of <literal>g_assert_cmpint (n1, op, n2)</literal> is the same
445 as <literal>g_assert (n1 op n2)</literal>. The advantage of this macro
446 is that it can produce a message that includes the actual values of @n1
451 @cmp: The comparsion operator to use. One of ==, !=, <, >, <=, >=.
456 <!-- ##### MACRO g_assert_cmpuint ##### -->
458 Debugging macro to terminate the application with a warning message
459 if an unsigned integer comparison fails.
462 The effect of <literal>g_assert_cmpuint (n1, op, n2)</literal> is the same
463 as <literal>g_assert (n1 op n2)</literal>. The advantage of this macro
464 is that it can produce a message that includes the actual values of @n1
468 @n1: an unsigned integer
469 @cmp: The comparsion operator to use. One of ==, !=, <, >, <=, >=.
470 @n2: another unsigned integer
474 <!-- ##### MACRO g_assert_cmphex ##### -->
476 Debugging macro to terminate the application with a warning message
477 if an unsigned integer comparison fails. This is a variant of
478 g_assert_cmpuint() that displays the numbers in hexadecimal notation
482 @n1: an unsigned integer
483 @cmp: The comparsion operator to use. One of ==, !=, <, >, <=, >=.
484 @n2: another unsigned integer
488 <!-- ##### MACRO g_assert_cmpfloat ##### -->
490 Debugging macro to terminate the application with a warning message
491 if a floating point number comparison fails.
494 The effect of <literal>g_assert_cmpflott (n1, op, n2)</literal> is the same
495 as <literal>g_assert (n1 op n2)</literal>. The advantage of this function
496 is that it can produce a message that includes the actual values of @n1
500 @n1: an floating point number
501 @cmp: The comparsion operator to use. One of ==, !=, <, >, <=, >=.
502 @n2: another floating point number
506 <!-- ##### FUNCTION g_strcmp0 ##### -->
516 <!-- ##### TYPEDEF GTestCase ##### -->
518 An opaque structure representing a test case.
522 <!-- ##### TYPEDEF GTestSuite ##### -->
524 An opaque structure representing a test suite.
528 <!-- ##### FUNCTION g_test_create_case ##### -->
542 <!-- ##### FUNCTION g_test_create_suite ##### -->
551 <!-- ##### FUNCTION g_test_get_root ##### -->
559 <!-- ##### FUNCTION g_test_suite_add ##### -->
568 <!-- ##### FUNCTION g_test_suite_add_suite ##### -->
577 <!-- ##### FUNCTION g_test_run_suite ##### -->