Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

googletest: How can I include all of my tests in one executable?

Tags:

c++

googletest

I am using the google test framework for C++. Following the documentation and examples, I end up with a separate executable for every test ".cc" file that I create. Is there a way to create a single executable that will call all of my unit tests?

I would like to put my project in a CI tool that reports on test status, so I would like to have a single XML input file instead of many.

The meat of my make file looks like this:

class1_unittest.o : $(USER_TEST_DIR)/class1_unittest.cc $(USER_DIR)/class1.h $(GTEST_HEADERS)
    $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $(USER_TEST_DIR)/class1_unittest.cc

class1_unittest : class1.o day.o class1_unittest.o gtest_main.a
     $(CXX) $(CPPFLAGS) $(CXXFLAGS) -pthread $^ -o $(PROJECT_BIN)/$@

class2_unittest.o : $(USER_TEST_DIR)/class2_unittest.cc $(USER_DIR)/class2.h $(GTEST_HEADERS)
    $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $(USER_TEST_DIR)/class2_unittest.cc

class2_unittest : class2.o day.o class2_unittest.o gtest_main.a
    $(CXX) $(CPPFLAGS) $(CXXFLAGS) -pthread $^ -o $(PROJECT_BIN)/$@
like image 939
RepDetec Avatar asked Sep 03 '25 02:09

RepDetec


1 Answers

You just need to include all your test files in a single target in your Makefile, and define main(), either in one of the test files or in a separate main.cc which will need to be included in the target also.

As the docs explain, the various versions of the TEST() macro implicitly register their tests with Google Test. This means that you then can have a standalone main.cc which only contains:

#include "gtest/gtest.h"
int main(int argc, char **argv) {
  ::testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}
like image 94
Fraser Avatar answered Sep 04 '25 15:09

Fraser