I have used the Boost Unit Test library in similar circumstances with positive results. I wanted to have automatic tests to see if a library worked as it should when forking. Although it was also closer to a system test in my case I'm all for using available tools if they achieve what you want.
One obstacle to overcome though is to signal errors from the child process without using boost assert macros. If e.g. BOOST_REQUIRE
would be used it would prematurely abort the test and any subsequent tests would be executed in both parent and child processes. I ended up using the process exit code to signal error to the waiting parent process. However, don't use exit()
as boost have atexit()
hooks that signal errors in the child process even if there are none. Use _exit()
instead.
The setup I used for tests was something like this.
BOOST_AUTO_TEST_CASE(Foo)
{
int pid = fork();
BOOST_REQUIRE( pid >= 0 );
if( pid == 0 ) // child
{
// Don't use Boost assert macros here
// signal errors with exit code
// Don't use exit() since Boost test hooks
// and signal error in that case, use _exit instead.
int rv = something();
_exit(rv);
}else{ // parent
// OK to use boost assert macros in parent
BOOST_REQUIRE_EQUAL(0,0);
// Lastly wait for the child to exit
int childRv;
wait(&childRv);
BOOST_CHECK_EQUAL(childRv, 0);
}
}