简体   繁体   中英

Boost.Test and Forking

I'm using Boost.Test for Unit testing and am currently running various mock servers in separate threads which get launched from within each test. In order to more accurately test my code the mock server's should really be in separate processes.

I was thinking about doing something along these lines:

MY_TEST()
if (fork() == 0) {
    runMockServer();  // responds to test requests or times out, then returns
    exit(0);
}
// Connect to MockServ and Run actual test here
END_TEST()

but I'm worried that this will screw up the testing framework.

Is this safe? Has anyone done something like this?

I'm using Boost 1.34.1 on Ubuntu 8.04 if that matters.

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 eg 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);
  }

}

This doesn't really sound like unit-testing for what you want to achieve. Though I don't see why it would not be safe. You may have a race condition where your unit test connects to the MockServ if it isn't ready yet, but that is easily solvable.

I've never done something like this directly, but I have written unit tests for libraries that fork/exec child processes and it works flawlessly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM