Search code examples
c++googletest

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


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.


Solution

  • 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.