Edit It seems I might have taken the question title a bit too literally. Running tests simultaneously means "in parallel" to me.
Anyways, if you are happy to run suite2/case1
as well, you can just
MyProject.exe --run_test="suite1,suite2"
See it Live On Coliru too.
Old answer: What is wrong with running the two processes in parallel? By all means, uncomplicate!
However, if you insist, you can fork copies of the main process:
#include <sys/types.h>
#include <sys/wait.h>
#include <iostream>
static int relay_unit_test_main(std::vector<std::string> args);
int main()
{
if (int const child_pid = fork())
{
int exit_code = relay_unit_test_main({"--run_test=suite1"});
int child_status;
while (-1 == waitpid(child_pid, &child_status, 0));
if (!WIFEXITED(child_status)) {
std::cerr << "Child process (" << child_pid << ") failed" << std::endl;
return 1;
}
return exit_code? exit_code : WEXITSTATUS(child_status);
} else
{
return relay_unit_test_main({"--run_test=suite2"});
}
}
See it Live On Coliru
The function relay_unit_test_main
is really nothing more than a convenience wrapper around unit_test_main
that avoids meddling with argv[]
manually:
static bool init_function() { return true; }
static int relay_unit_test_main(std::vector<std::string> args)
{
std::vector<char const*> c_args;
c_args.push_back("fake_program_name");
std::transform(args.begin(), args.end(), std::back_inserter(c_args), std::mem_fn(&std::string::data));
c_args.push_back(nullptr);
return unit_test_main( &init_function, c_args.size()-1, const_cast<char**>(c_args.data()) );
}
This actually spawns a child process - and even tries to usefully combine the exit code information. Having a separate process prevents the problems that you'd get from using code that wasn't designed for multi-threaded use on different threads.
One caveat remains: if your program does static initializations before entry of main()
, and these use external resources (like, log files, e.g.) there might be conflicts. See