Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grouping googletest unit tests by categories

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.

like image 916
walrii Avatar asked Sep 26 '12 20:09

walrii


People also ask

What is Test_f in Gtest?

TEST_F(TestFixtureName, TestName) { ... statements ... } Defines an individual test named TestName that uses the test fixture class TestFixtureName . The test suite name is TestFixtureName .

What is gMock?

gMock is a library (sometimes we also call it a “framework” to make it sound cool) for creating mock classes and using them. It does to C++ what jMock/EasyMock does to Java (well, more or less).

How do I run all test cases in Visual Studio?

To run all the tests in a default group, choose the Run icon and then choose the group on the menu. Select the individual tests that you want to run, open the right-click menu for a selected test and then choose Run Selected Tests (or press Ctrl + R, T).


2 Answers

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
like image 174
Torsten Avatar answered Sep 27 '22 18:09

Torsten


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.
like image 41
NiladriBose Avatar answered Sep 27 '22 18:09

NiladriBose