Command-line options to force googletest to run in single thread
Asked Answered
L

3

8

I have a suite of unit tests that is managed by googletest. These tests are run in multiple threads by default, even when I use --gtest_filter=foo.test so that it only runs a single test. This is causing ambiguity about the cause of a bug I'm trying to hammer out.

How can multithreaded testing be turned off in googletest?

Levine answered 29/3, 2015 at 22:4 Comment(0)
N
8

There's no commandline switch for single/multi-threading. libgtest is built either single-threading or multi-threading.

To make it single-threading, build gtest with ./configure --with-pthreads=no, then link your unit test app without -pthread

If you only want single threading sometimes, then make a no-pthreads build of libgtest, call it something else, and link it when you want.

Nab answered 30/3, 2015 at 14:56 Comment(0)
E
6

If you are building googletest with cmake, you can use the cmake option gtest_disable_pthreads to control support for threading. For example:

$ mkdir build && cd build
$ cmake .. -Dgtest_disable_pthreads=ON

The resulting output should not show:

-- Looking for pthread.h
-- Looking for pthread.h - found
-- Looking for pthread_create
-- Looking for pthread_create - found
-- Found Threads: TRUE  

Then you can run make as usual.

Engdahl answered 24/6, 2016 at 23:40 Comment(0)
I
5

Another option short of rebuilding with multi-threading disabled is to simply create a test fixture for tests that cannot run concurrently. Then in the SetUp() and TearDown() methods, lock and unlock a mutex respectivey.

Be sure to use a mutex that exists outside of the test fixture, as the fixture is created and torn down for every test.

std::mutex g_singleThread;

class SingleThreadedTests
{
protected:
   virtual void SetUp() override
   {
      g_singleThread.lock();
   }

   virtual void TearDown() override
   {
      g_singleThread.unlock();
   }
};

TEST_F(SingleThreadedTests, MyTest)
{
   // ...
}
Irresoluble answered 2/4, 2019 at 18:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.