How can I combine test filters in the unit testing framework Google Test?

Viewed 2016

I have several unit test cases, which I have written with the Google Test framework:

  1. Test class:

    class Test: public testing::Test
    {
    public:
      virtual void SetUp() {}
      virtual void TearDown() {}
    };
    
  2. Actual tests:

    TEST_F(Test, SubTest1)
    {
      // execute Test logic
    }
    
    TEST_F(Test, SubTest2)
    {
      // execute Test logic
    }
    
    TEST_F(Test, SubTest3)
    {
      // execute Test logic
    }
    

Assumed that I want to display only SubTest1 and SubTest3, what do I have to do? Important is, that I want to see at a central place (main method), which tests are actually executed.

I thought, that I can "stack" filters as in the following example, but this approach did not work:

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

  ::testing::GTEST_FLAG(filter) = "Test.SubTest1";
  ::testing::GTEST_FLAG(filter) = "Test.SubTest3";
  return RUN_ALL_TESTS();
}

→ The second filter removed the first one and only SubTest3 is executed.

2 Answers

I tried to find some official googletest reference, but I only found this article explaining the syntax of googletest filters. If you want to run testcases matching one of 2 different patterns, your filter should look like:

"FIRST_PATTERN:SECOND_PATTERN"

So, in your case:

  ::testing::GTEST_FLAG(filter) = "Test.SubTest1:Test.SubTest3";

You can also use wilcards ? and * and you can add exclusion patterns after - sign.

You can disable tests you don't want to run with DISABLED_ prefix (see documentation):

TEST_F(Test, DISABLED_SubTest2)
{
  // execute Test logic
}

The test will still be compiled, but GoogleTest will not run it.

GoogleTest provides a nice information after tests finish, how many of them were disabled.

You can also temporarily enable them with --gtest_also_run_disabled_tests flag.

Related