Is it possible to mark GLib tests as XFAILing?

Let’s say I have something like this:

#include <glib.h>
#include <locale.h>

static void
test_xfail ()
{
        g_critical ("This should output a critical, but still not fail the test");

        /* This should be nonfatal even though it fails. */
        g_assert_cmpint (1, ==, 0);
}

int
main(int argc, char * argv[])
{
        setlocale (LC_ALL, "");

        g_test_init (&argc, &argv, NULL);

        g_test_add_func ("/xfail", test_xfail);

        return g_test_run ();
}

A critical and an assertion in a test. Assume it’s flaky and almost always you don’t hit these and the test passes. Is it possible to mark this test somehow as expected or “OK” to fail? g_test_incomplete() looks like it might be this but it’s not.

Or is my best option to move it out to its own .c file and have the people running the test ignore failures for that suite?

You should look at g_test_subprocess(), which is how GLib tests that things fail according to plan.

Cheers. I was hoping to avoid touching the original test too much, and I think that’s possible here. We can do something like this I think:

#include <glib.h>
#include <locale.h>

static void
test_xfail ()
{
        g_critical ("This should output a critical, but still not fail the test");

        /* This should be nonfatal even though it fails. */
        g_assert_cmpint (1, ==, 0);

        g_critical ("And we should see this one because that g_assert_cmpint was nonfatal");
}

static gboolean
log_fatal_func (const char *log_domain,
                GLogLevelFlags log_level,
                const gchar *message,
                gpointer user_data)
{
        return FALSE;
}

static void
test_xfail_subprocess ()
{
        if (g_test_subprocess ()) { 
                g_test_log_set_fatal_handler (log_fatal_func, NULL);
                g_test_set_nonfatal_assertions ();
                test_xfail ();
                return;
        } 

        g_test_trap_subprocess (NULL, 0, G_TEST_SUBPROCESS_INHERIT_STDIN | 
                                         G_TEST_SUBPROCESS_INHERIT_STDOUT | 
                                         G_TEST_SUBPROCESS_INHERIT_STDERR);
}

int
main (int argc, char * argv[])
{
        setlocale (LC_ALL, "");

        g_test_init (&argc, &argv, NULL);

        g_test_add_func ("/xfail", test_xfail_subprocess);

        return g_test_run ();
}