trap_subprocess


Description:

public void trap_subprocess (string test_path, uint64 usec_timeout, TestSubprocessFlags test_flags)

Respawns the test program to run only test_path in a subprocess.

This can be used for a test case that might not return, or that might abort.

If test_path is null then the same test is re-run in a subprocess. You can use subprocess to determine whether the test is in a subprocess or not.

test_path can also be the name of the parent test, followed by "`/subprocess/`" and then a name for the specific subtest (or just ending with "`/subprocess`" if the test only has one child test); tests with names of this form will automatically be skipped in the parent process.

If usec_timeout is non-0, the test subprocess is aborted and considered failing if its run time exceeds it.

The subprocess behavior can be configured with the TestSubprocessFlags flags.

You can use methods such as trap_assert_passed, trap_assert_failed, and trap_assert_stderr to check the results of the subprocess. (But note that trap_assert_stdout and trap_assert_stderr cannot be used if test_flags specifies that the child should inherit the parent stdout/stderr.)

If your `main ()` needs to behave differently in the subprocess, you can call subprocess (after calling init) to see whether you are in a subprocess.

The following example tests that calling `my_object_new(1000000)` will abort with an error message.

  static void
test_create_large_object_subprocess (void)
{
if (g_test_subprocess ())
{
my_object_new (1000000);
return;
}

// Reruns this same test in a subprocess
g_test_trap_subprocess (NULL, 0, 0);
g_test_trap_assert_failed ();
g_test_trap_assert_stderr ("*ERROR*too large*");
}

int
main (int argc, char **argv)
{
g_test_init (&argc, &argv, NULL);

g_test_add_func ("/myobject/create_large_object",
test_create_large_object);
return g_test_run ();
}

Parameters:

test_path

Test to run in a subprocess

usec_timeout

Timeout for the subprocess test in micro seconds.

test_flags

Flags to modify subprocess behaviour.


Namespace: GLib.Test
Package: glib-2.0