Synopsis#include <glib.h> void g_test_minimized_result (double minimized_quantity, const char *format, ...); void g_test_maximized_result (double maximized_quantity, const char *format, ...); void g_test_init (int *argc, char ***argv, ...); #define g_test_quick () #define g_test_slow () #define g_test_thorough () #define g_test_perf () #define g_test_verbose () #define g_test_quiet () int g_test_run (void); void g_test_add_func (const char *testpath, void (test_funcvoid) ()); void g_test_add_data_func (const char *testpath, gconstpointer test_data, void (test_funcgconstpointer) ()); #define g_test_add (testpath, Fixture, tdata, fsetup, ftest, fteardown) void g_test_message (const char *format, ...); void g_test_bug_base (const char *uri_pattern); void g_test_bug (const char *bug_uri_snippet); void g_test_timer_start (void); double g_test_timer_elapsed (void); double g_test_timer_last (void); void g_test_queue_free (gpointer gfree_pointer); void g_test_queue_destroy (GDestroyNotify destroy_func, gpointer destroy_data); #define g_test_queue_unref (gobject) enum GTestTrapFlags; gboolean g_test_trap_fork (guint64 usec_timeout, GTestTrapFlags test_trap_flags); gboolean g_test_trap_has_passed (void); gboolean g_test_trap_reached_timeout (void); #define g_test_trap_assert_passed () #define g_test_trap_assert_failed () #define g_test_trap_assert_stdout (soutpattern) #define g_test_trap_assert_stdout_unmatched (soutpattern) #define g_test_trap_assert_stderr (serrpattern) #define g_test_trap_assert_stderr_unmatched (serrpattern) #define g_test_rand_bit () gint32 g_test_rand_int (void); gint32 g_test_rand_int_range (gint32 begin, gint32 end); double g_test_rand_double (void); double g_test_rand_double_range (double range_start, double range_end); #define g_assert (expr) #define g_assert_not_reached () #define g_assert_cmpstr (s1, cmp, s2) #define g_assert_cmpint (n1, cmp, n2) #define g_assert_cmpuint (n1, cmp, n2) #define g_assert_cmphex (n1, cmp, n2) #define g_assert_cmpfloat (n1,cmp,n2) #define g_assert_no_error (err) #define g_assert_error (err, dom, c) typedef GTestCase; typedef GTestSuite; GTestCase* g_test_create_case (const char *test_name, gsize data_size, gconstpointer test_data, void (data_setupvoid) (), void (data_testvoid) (), void (data_teardownvoid) ()); GTestSuite* g_test_create_suite (const char *suite_name); GTestSuite* g_test_get_root (void); void g_test_suite_add (GTestSuite *suite, GTestCase *test_case); void g_test_suite_add_suite (GTestSuite *suite, GTestSuite *nestedsuite); int g_test_run_suite (GTestSuite *suite); DescriptionGLib 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.
The API is designed to handle creation and registration of test suites and test cases implicitly. A simple call like g_test_add_func ("/misc/assertions", test_assertions); creates a test suite called "misc" with a single test case named "assertions", which consists of running the test_assertions function.
In addition to the traditional GLib ships with two utilities called gtester and gtester-report to facilitate running tests and producing nicely formatted test reports. Detailsg_test_minimized_result ()void g_test_minimized_result (double minimized_quantity, const char *format, ...);
Report the result of a performance or measurement test.
The test should generally strive to minimize the reported
quantities (smaller values are better than larger ones),
this and
Since 2.16 g_test_maximized_result ()void g_test_maximized_result (double maximized_quantity, const char *format, ...);
Report the result of a performance or measurement test.
The test should generally strive to maximize the reported
quantities (larger values are better than smaller ones),
this and
Since 2.16 g_test_init ()void g_test_init (int *argc, char ***argv, ...);
Initialize the GLib testing framework, e.g. by seeding the
test random number generator, the name for
Since 2.16 g_test_run ()int g_test_run (void);
Runs all tests under the toplevel suite which can be retrieved
with
Since 2.16 g_test_add_func ()void g_test_add_func (const char *testpath, void (test_funcvoid) ());
Create a new test case, similar to
Since 2.16 g_test_add_data_func ()void g_test_add_data_func (const char *testpath, gconstpointer test_data, void (test_funcgconstpointer) ());
Create a new test case, similar to
Since 2.16 g_test_add()#define g_test_add(testpath, Fixture, tdata, fsetup, ftest, fteardown)
Hook up a new test case at
Since 2.16 g_test_message ()void g_test_message (const char *format, ...); Add a message to the test report.
Since 2.16 g_test_bug_base ()void g_test_bug_base (const char *uri_pattern); Specify the base URI for bug reports.
The base URI is used to construct bug report messages for
Since 2.16 g_test_bug ()void g_test_bug (const char *bug_uri_snippet);
This function adds a message to test reports that
associates a bug URI with a test case.
Bug URIs are constructed from a base URI set with
Since 2.16 g_test_timer_start ()void g_test_timer_start (void);
Start a timing test. Call
Since 2.16 g_test_timer_elapsed ()double g_test_timer_elapsed (void);
Get the time since the last start of the timer with
Since 2.16 g_test_timer_last ()double g_test_timer_last (void);
Report the last result of
Since 2.16 g_test_queue_free ()void g_test_queue_free (gpointer gfree_pointer);
Enqueue a pointer to be released with
Since 2.16 g_test_queue_destroy ()void g_test_queue_destroy (GDestroyNotify destroy_func, gpointer destroy_data);
This function enqueus a callback @
Since 2.16 g_test_queue_unref()#define g_test_queue_unref(gobject)
Enqueue an object to be released with
Since 2.16 enum GTestTrapFlagstypedef enum { G_TEST_TRAP_SILENCE_STDOUT = 1 << 7, G_TEST_TRAP_SILENCE_STDERR = 1 << 8, G_TEST_TRAP_INHERIT_STDIN = 1 << 9 } GTestTrapFlags; Test traps are guards around forked tests. These flags determine what traps to set.
g_test_trap_fork ()gboolean g_test_trap_fork (guint64 usec_timeout, GTestTrapFlags test_trap_flags);
Fork the current test program to execute a test case that might
not return or that might abort. The forked test case is aborted
and considered failing if its run time exceeds The forking behavior can be configured with the GTestTrapFlags flags. In the following example, the test code forks, the forked child process produces some sample output and exits successfully. The forking parent process then asserts successful child program termination and validates child program outputs.
static void test_fork_patterns (void) { if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDOUT | G_TEST_TRAP_SILENCE_STDERR)) { g_print ("some stdout text: somagic17\n"); g_printerr ("some stderr text: semagic43\n"); exit (0); /* successful test run */ } g_test_trap_assert_passed(); g_test_trap_assert_stdout ("*somagic17*"); g_test_trap_assert_stderr ("*semagic43*"); }
This function is implemented only on Unix platforms.
Since 2.16 g_test_trap_has_passed ()gboolean g_test_trap_has_passed (void);
Check the result of the last
Since 2.16 g_test_trap_reached_timeout ()gboolean g_test_trap_reached_timeout (void);
Check the result of the last
Since 2.16 g_test_trap_assert_passed()#define g_test_trap_assert_passed()
Assert that the last forked test passed. See Since 2.16 g_test_trap_assert_failed()#define g_test_trap_assert_failed()
Assert that the last forked test failed. See Since 2.16 g_test_trap_assert_stdout()#define g_test_trap_assert_stdout(soutpattern)
Assert that the stdout output of the last forked test matches
Since 2.16 g_test_trap_assert_stdout_unmatched()#define g_test_trap_assert_stdout_unmatched(soutpattern)
Assert that the stdout output of the last forked test does not match
Since 2.16 g_test_trap_assert_stderr()#define g_test_trap_assert_stderr(serrpattern)
Assert that the stderr output of the last forked test matches
Since 2.16 g_test_trap_assert_stderr_unmatched()#define g_test_trap_assert_stderr_unmatched(serrpattern)
Assert that the stderr output of the last forked test does not match
Since 2.16 g_test_rand_bit()#define g_test_rand_bit()
Get a reproducible random bit (0 or 1),
see Since 2.16 g_test_rand_int ()gint32 g_test_rand_int (void); Get a reproducible random integer number. The random numbers generated by the g_test_rand_*() family of functions change with every new test program start, unless the --seed option is given when starting test programs. For individual test cases however, the random number generator is reseeded, to avoid dependencies between tests and to make --seed effective for all test cases.
Since 2.16 g_test_rand_int_range ()gint32 g_test_rand_int_range (gint32 begin, gint32 end);
Get a reproducible random integer number out of a specified range,
see
Since 2.16 g_test_rand_double ()double g_test_rand_double (void);
Get a reproducible random floating point number,
see
Since 2.16 g_test_rand_double_range ()double g_test_rand_double_range (double range_start, double range_end);
Get a reproducible random floating pointer number out of a specified range,
see
Since 2.16 g_assert()#define g_assert(expr) 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. The macro can be turned off in final releases of code by defining G_DISABLE_ASSERT when compiling the application.
g_assert_not_reached()#define g_assert_not_reached() 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. The macro can be turned off in final releases of code by defining G_DISABLE_ASSERT when compiling the application. g_assert_cmpstr()#define g_assert_cmpstr(s1, cmp, s2)
Debugging macro to terminate the application with a warning message
if a string comparison fails.
The strings are compared using
The effect of g_assert_cmpstr (mystring, ==, "fubar");
Since 2.16 g_assert_cmpint()#define g_assert_cmpint(n1, cmp, n2) Debugging macro to terminate the application with a warning message if an integer comparison fails.
The effect of
Since 2.16 g_assert_cmpuint()#define g_assert_cmpuint(n1, cmp, n2) Debugging macro to terminate the application with a warning message if an unsigned integer comparison fails.
The effect of
Since 2.16 g_assert_cmphex()#define g_assert_cmphex(n1, cmp, n2)
Debugging macro to terminate the application with a warning message
if an unsigned integer comparison fails. This is a variant of
Since 2.16 g_assert_cmpfloat()#define g_assert_cmpfloat(n1,cmp,n2) Debugging macro to terminate the application with a warning message if a floating point number comparison fails.
The effect of
Since 2.16 g_assert_no_error()#define g_assert_no_error(err) Debugging macro to terminate the application with a warning message if a method has returned a GError.
The effect of Since 2.20 g_assert_error()#define g_assert_error(err, dom, c) Debugging macro to terminate the application with a warning message if a method has not returned the correct GError.
The effect of
This can only be used to test for a specific error. If you want to
test that
Since 2.20 g_test_create_case ()GTestCase* g_test_create_case (const char *test_name, gsize data_size, gconstpointer test_data, void (data_setupvoid) (), void (data_testvoid) (), void (data_teardownvoid) ());
Create a new GTestCase, named
Splitting up a test run into fixture setup, test function and
fixture teardown is most usful if the same fixture is used for
multiple tests. In this cases,
Since 2.16 g_test_create_suite ()GTestSuite* g_test_create_suite (const char *suite_name);
Create a new test suite with the name
Since 2.16 g_test_get_root ()GTestSuite* g_test_get_root (void); Get the toplevel test suite for the test path API.
Since 2.16 g_test_suite_add ()void g_test_suite_add (GTestSuite *suite, GTestCase *test_case);
Adds
Since 2.16 g_test_suite_add_suite ()void g_test_suite_add_suite (GTestSuite *suite, GTestSuite *nestedsuite);
Adds
Since 2.16 g_test_run_suite ()int g_test_run_suite (GTestSuite *suite);
Execute the tests within
Since 2.16 |