Is it possible to run each testcase in a separate process other than a single process?
It seems that gtest
itself does not intend to support this feature.
As I did a test and I found all the testcases are run in the same thread. Here is the said code snippet:
TEST(TestSuitNameA, TestCaseName)
{
std::cout << std::this_thread::get_id() << std::endl;
}
TEST(TestSuitNameB, TestCaseName)
{
std::cout << std::this_thread::get_id() << std::endl;
}
Here is a hack that you can use: force each test to die at the end.
Say you want to test a function called MyFunction
, you can then wrap it in another function called MyFunctionTester
which exits with code 0 if the test passes and 1 otherwise. Then in your test, use EXPECT_EXIT
.
Here is a quick example:
int MyFunction(int a){
return a+1;
}
void MyFunctionTester(int a){
auto actual = MyFunction(a);
std::cout << "actual: " << actual << std::endl;
_exit(actual);
}
TEST(TestSuitNameA, TestCaseName)
{
GTEST_FLAG_SET(death_test_style, "threadsafe");
std::cout << " thread id: " << std::this_thread::get_id() << std::endl;
EXPECT_EXIT(MyFunctionTester(1), testing::ExitedWithCode(2), "");
}
TEST(TestSuitNameB, TestCaseName)
{
GTEST_FLAG_SET(death_test_style, "threadsafe");
std::cout << " thread id: " << std::this_thread::get_id() << std::endl;
EXPECT_EXIT(MyFunctionTester(2), testing::ExitedWithCode(3), "");
}
Live example here: https://godbolt.org/z/WrWz67jhG
Please notice that this is really a hack and far from ideal, but it seems to work.
See here for more info on death tests.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With