Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run threads in gtest

I'm starting implementing some tests using gtest. I have some methods, which request data from external, what takes some time. So I would like to do it in parallel using threads. For testing I made some simple example:

void TestThread(void) {
  ASSERT_EQ(1,2);
  boost::this_thread::sleep_for(boost::chrono::seconds(5));
  ASSERT_EQ(2,3);
}

TEST(MySuite, MyTest) {
  boost::thread myThread(TestThread);
  ASSERT_EQ(0,0);
  myThread.join();
}

int main(int argc, char** argv) {
  testing::InitGoogleTest(&argc, argv);

  return RUN_ALL_TESTS();
}

I would expect all asserts from TestThread, but the second one is never part of test result. Also the test runs less than a second. I guess, the 'boost::thread::join' does not work, but why?

Regards, Christian

like image 643
Christian Avatar asked Aug 10 '17 13:08

Christian


People also ask

Does Gtest run in parallel?

gtest-parallel is a script that executes Google Test binaries in parallel, providing good speedup for single-threaded tests (on multi-core machines) and tests that do not run at 100% CPU (on single- or multi-core machines).

Does Gtest include gMock?

gMock is bundled with googletest.


1 Answers

ASSERT_xxx() will abort the test if the assertion fails. EXPECT_xxx will not.

like image 109
Richard Hodges Avatar answered Sep 30 '22 14:09

Richard Hodges