Grouping googletest unit tests by categories

c++, googletest, unit-testing

Solution

One of way use gtest_filter option and use naming convention for tests (as you describe in question).

TEST_F(Foo, SlowRunning_test1) {...}
TEST_F(Foo, BugRegression_test1) {...}
TEST_F(Foo, SlowRunningBugRegression_test1) {...}

Other way use separate binaries/executable for any type of test. This way have some limitations because gtest use static autoregistration so if you include some source file - all tests implemented in this source file would be included to generated binary/executable.

By my opinion first method better. Additionally, i would implemented new test registration macro for make my life easier:

#define GROUP_TEST_F(GroupName, TestBase, TestName) \
#ifdef NO_GROUP_TESTS \
   TEST_F(TestBase, TestName) \
#else \
   TEST_F(TestBase, GroupName##_##TestName) \
#endif

Problem

Can googletest unit tests be grouped by categories? For example "SlowRunning", "BugRegression", etc. The closest thing I have found is the --gtest_filter option. By appending/prepending category names to the test or fixture names I can simulate the existence of groups. This does not allow me to create groups that are not normally run. If categories do not exist in googletest, is there a good or best practice workaround? Edit: Another way is to use the --gtest_also_run_disabled_tests. Adding DISABLED_ in front of tests gives you exactly one conditional category, but I feel like I'm misusing DISABLED when I do it.

Original source