11

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.

walrii
  • 3,472
  • 2
  • 28
  • 47

2 Answers2

8

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
Torsten
  • 21,726
  • 5
  • 24
  • 31
  • I like that Your GROUP_TEST_F formalizes the naming convention. It improves using filtering. I still wish there was something better than filtering. – walrii Oct 02 '12 at 01:12
  • 4
    Be careful, [test names should not contain underscore](https://github.com/google/googletest/blob/master/googletest/docs/primer.md#simple-tests) – Jo Ham Feb 11 '19 at 15:27
  • How do you use the GROUP_TEST_F? When is NO_GROUP_TESTS defined and when is it not? And what are the "\" for? The makro is not working at all for me. I changed it to `#define GROUP_TEST_F(GroupName, TestBase, TestName) #ifdef NO_GROUP_TESTS TEST(TestBase, TestName) #else TEST(TestBase, GroupName_TestName) #endif` But if I use the GROUP_TEST_F, the test result looks like this "GroupName.GroupName_TestName" – snicz Dec 02 '20 at 12:44
2

The only way to run subset of tests in a single test executable is --gtest_filter. There are two workarounds to executing say integration tests and unit tests

  1. Use a naming convention like Integration.Testname and Unit.Testname. In addition to that I would also maintain script files like RunIntegration.bat and RunUnit.bat to run from my build automation scripts for different scenarios.
  2. Maintain deferent test executables for integration and unit or other categories. In visual studios in will have separate projects for each.
NiladriBose
  • 1,849
  • 2
  • 16
  • 31
  • I like the idea of separate executables, but that would either mean segregating the categories into separate source files or using #defines and regenerate .o files. Neither is very palatable. – walrii Oct 02 '12 at 01:07
  • I probably don't understand your comment. Why do you thing segregationg into separate test files is a bad idea. As your test suite grows it will be advantageous to modularise test , created deep namespaces etc. if your concern is about reuse/ code duplication then you could start using common test fixture between test type http://code.google.com/p/googletest/wiki/V1_6_Primer#Test_Fixtures:_Using_the_Same_Data_Configuration_for_Multiple_Te – NiladriBose Oct 02 '12 at 12:26
  • I definitely believe in segregating tests into separate files. But if I have a file with the tests for X, I don't like splitting off one test into a separate file just because it is in the "long-running" category. Using separate executables for categories would require that or recompiling the same file with different #defines. – walrii Oct 02 '12 at 13:52
  • Ok got you about the recompilation which would mean an additional build step. Thoug you might have tried it already instead of using #defines to parameteise i would look at http://code.google.com/p/googletest/wiki/V1_6_AdvancedGuide#Value_Parameterized_Tests to avoid rewriting tests , so the same test can be used for long rumming or short running based on a parameter. – NiladriBose Oct 03 '12 at 11:01
  • I do use value and type parameterized tests. But different categories are not simply different parameters on other tests. – walrii Oct 04 '12 at 00:59