[ Version ( since = "2.38" ) ]
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 (void)
if (g_test_subprocess ())
my_object_new (1000000);

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

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

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

Example: Run a case in a subprocess:

public static int main (string[] args) {
Test.init (ref args);
Test.bug_base ("");

Test.add_func ("/valadoc/driver-0.14.x", () => {
Test.trap_subprocess ("/valadoc/driver-0.14.x/subprocess", 0, 0);

// Optional checks & asserts:

// Check whether the last fork passed/succeeded:
if (Test.trap_has_passed ()) {
// Whatever

// Check whether GTest killed the fork due to a timeout
if (Test.trap_reached_timeout ()) {
Test.message ("timeout reached!!");

// Make sure the last forked test passed/succeeded:
Test.trap_assert_passed ();

// Make sure the forked output does match the following patterns:
// See GLib.PatternSpec for details
Test.trap_assert_stdout ("*warning:*");
Test.trap_assert_stderr ("*error:*");

// Make sure the forked output does not match the following patterns:
Test.trap_assert_stdout_unmatched ("*debug:*");
Test.trap_assert_stderr_unmatched ("*critical-error:*");

// Because of the '/subprocess' in the name, this test will
// not be run by the () call below.
Test.add_func ("/valadoc/driver-0.14.x/subprocess", () => {
// Our testcase:
// ...
print ("warning: unexpected token: ==\n");
stderr.printf ("error: unexpected token: ==\n");
// ..

// use assert () and friends to verify your code
// assert (false) marks a test as "not-passed"
}); ();
return 0;

valac --pkg glib-2.0 GLib.Test.trap_subprocess.vala

./GLib.Test.trap_subprocess --verbose



Test to run in a subprocess


Timeout for the subprocess test in micro seconds.


Flags to modify subprocess behaviour.

Namespace: GLib.Test
Package: glib-2.0