API: testutils: Add g_test_fail()

This allows tests to fail in a nonfatal way and the test runner can
continue if invoked with -k.

https://bugzilla.gnome.org/show_bug.cgi?id=647826
This commit is contained in:
Benjamin Otte 2011-04-15 03:23:12 +02:00
parent 9a12103259
commit d5dc79c0b0
4 changed files with 36 additions and 2 deletions

View File

@ -2798,6 +2798,7 @@ GTestDataFunc
g_test_add_data_func
g_test_add
g_test_fail
g_test_message
g_test_bug_base
g_test_bug

View File

@ -1451,6 +1451,7 @@ g_test_config_vars
#endif
g_test_create_case
g_test_create_suite
g_test_fail
g_test_get_root
g_test_init
g_test_log_buffer_free

View File

@ -101,6 +101,7 @@ static GRand *test_run_rand = NULL;
static gchar *test_run_name = "";
static guint test_run_forks = 0;
static guint test_run_count = 0;
static guint test_run_success = FALSE;
static guint test_skip_count = 0;
static GTimer *test_user_timer = NULL;
static double test_user_stamp = 0;
@ -997,6 +998,31 @@ g_test_add_vtable (const char *testpath,
g_strfreev (segments);
}
/**
* g_test_fail:
*
* Indicates that a test failed. This function can be called
* multiple times from the same test. You can use this function
* if your test failed in a recoverable way.
*
* Do not use this function if the failure of a test could cause
* other tests to malfunction.
*
* Calling this function will not stop the test from running, you
* need to return from the test function yourself. So you can
* produce additional diagnostic messages or even continue running
* the test.
*
* If not called from inside a test, this function does nothing.
*
* @Since: 2.30
**/
void
g_test_fail (void)
{
test_run_success = FALSE;
}
/**
* GTestFunc:
*
@ -1166,6 +1192,7 @@ static gboolean
test_case_run (GTestCase *tc)
{
gchar *old_name = test_run_name, *old_base = g_strdup (test_uri_base);
gboolean success = TRUE;
test_run_name = g_strconcat (old_name, "/", tc->name, NULL);
if (++test_run_count <= test_skip_count)
@ -1182,6 +1209,7 @@ test_case_run (GTestCase *tc)
void *fixture;
g_test_log (G_TEST_LOG_START_CASE, test_run_name, NULL, 0, NULL);
test_run_forks = 0;
test_run_success = TRUE;
g_test_log_set_fatal_handler (NULL, NULL);
g_timer_start (test_run_timer);
fixture = tc->fixture_size ? g_malloc0 (tc->fixture_size) : tc->test_data;
@ -1202,7 +1230,9 @@ test_case_run (GTestCase *tc)
if (tc->fixture_size)
g_free (fixture);
g_timer_stop (test_run_timer);
largs[0] = 0; /* OK */
success = test_run_success;
test_run_success = FALSE;
largs[0] = success ? 0 : 1; /* OK */
largs[1] = test_run_forks;
largs[2] = g_timer_elapsed (test_run_timer, NULL);
g_test_log (G_TEST_LOG_STOP_CASE, NULL, NULL, G_N_ELEMENTS (largs), largs);
@ -1213,7 +1243,7 @@ test_case_run (GTestCase *tc)
g_free (test_uri_base);
test_uri_base = old_base;
return TRUE;
return success;
}
static int

View File

@ -107,6 +107,8 @@ void g_test_add_func (const char *testpath,
void g_test_add_data_func (const char *testpath,
gconstpointer test_data,
GTestDataFunc test_func);
/* tell about failure */
void g_test_fail (void);
/* hook up a test with fixture under test path */
#define g_test_add(testpath, Fixture, tdata, fsetup, ftest, fteardown) \