gtestutils: Allow failing a test with a printf-style message

This allows a pattern like

    g_test_message ("cannot reticulate splines: %s", error->message);
    g_test_fail ();

to be replaced by the simpler

    g_test_fail_printf ("cannot reticulate splines: %s", error->message);

with the secondary benefit of making the message available to TAP
consumers as part of the "not ok" message.

Signed-off-by: Simon McVittie <smcv@collabora.com>
This commit is contained in:
Simon McVittie
2021-08-05 11:42:22 +01:00
parent 26fbd14954
commit a076dbcb68
5 changed files with 66 additions and 0 deletions

View File

@@ -49,6 +49,12 @@ test_fail (void)
g_test_fail ();
}
static void
test_fail_printf (void)
{
g_test_fail_printf ("this test intentionally left failing");
}
static void
test_incomplete (void)
{
@@ -123,6 +129,10 @@ main (int argc,
{
g_test_add_func ("/fail", test_fail);
}
else if (g_strcmp0 (argv1, "fail-printf") == 0)
{
g_test_add_func ("/fail-printf", test_fail_printf);
}
else if (g_strcmp0 (argv1, "all-non-failures") == 0)
{
g_test_add_func ("/pass", test_pass);

View File

@@ -1177,6 +1177,26 @@ test_tap (void)
g_clear_error (&error);
g_ptr_array_unref (argv);
g_test_message ("fail with message");
argv = g_ptr_array_new ();
g_ptr_array_add (argv, (char *) testing_helper);
g_ptr_array_add (argv, "fail-printf");
g_ptr_array_add (argv, "--tap");
g_ptr_array_add (argv, NULL);
g_spawn_sync (NULL, (char **) argv->pdata, NULL,
G_SPAWN_STDERR_TO_DEV_NULL,
NULL, NULL, &output, NULL, &status,
&error);
g_assert_no_error (error);
g_spawn_check_wait_status (status, &error);
g_assert_error (error, G_SPAWN_EXIT_ERROR, 1);
g_assert_nonnull (strstr (output, "\nnot ok 1 /fail-printf - this test intentionally left failing\n"));
g_free (output);
g_clear_error (&error);
g_ptr_array_unref (argv);
g_test_message ("all");
argv = g_ptr_array_new ();
g_ptr_array_add (argv, (char *) testing_helper);