Testing C++ with GoogleTest: installation and Makefile
General reference: GoogleTest Primer.
Installation details are in the README. To sum up:
$ mkdir mybuild # Create a directory to hold the build output. $ cd mybuild $ cmake ${GTEST_DIR} # Generate native build scripts. $ make $ cp libgtest.a /usr/local/lib $ cp libgtest_main.a /usr/local/lib
A bare minimum google test is:
#include <gtest/gtest.h> TEST(MathTest, TwoPlusTwoEqualsFour) { EXPECT_EQ(2 + 2, 4); } int main(int argc, char **argv) { ::testing::InitGoogleTest( &argc, argv ); return RUN_ALL_TESTS(); }
and compiles with
g++ -I ${GTEST_DIR}/include/ mytest.cpp ${LIBRARIES} -lpthread -o a.out
Wrapping up everything in a nice makefile yields:
TARGET = network TEST = check CC = g++ CFLAGS = -g -Wall LIBS = -lpthread DEPS = file1.h file2.h OBJ = file1.o file2.o GTEST = gtest-1.7.0/include LIBGTEST = /usr/local/lib/libgtest_main.a /usr/local/lib/libgtest.a TESTDIR = tests %.o: %.cpp $(DEPS) $(CC) -c -o $@ $< $(CFLAGS) $(TARGET): $(OBJ) main.o $(CC) -o $@ $^ $(CFLAGS) $(TEST): $(OBJ) $(CC) -o $@ $^ $(CFLAGS) -I $(GTEST) $(TESTDIR)/*.cc $(LIBGTEST) $(LIBS) ./$(TEST) clean: rm -rf *.o *.gch rm -f $(TARGET) $(TEST)