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.
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.