Pages

GTest on Ubuntu

The Google C++ Testing Framework (better known as Google Test, or even GTest), has been designed to be as platform independent as possible. There are only a few little difference in setting it up on any specific environment, as you could see comparing this post with the one I wrote some time ago on how to set GTest up for Windows, and this one, focused on Linux (in a Debian flavor).

First step is always the same: go to Google code and download the Google Test zipped archive.

Do you already have cmake at hand? The Google Test package provides a few specific building files for some environments, the other ones are generated on the fly by this tool. Have a look at the official CMake site for more information. Here we'll be happy enough getting cmake from the standard repository:
sudo apt-get install cmake
Once you have unzipped the GTest package, you should see in its base directory a file named CMakeLists.txt, we'll pass it to cmake to generate a proper Makefile tailored on the current system characteristics.
cmake CMakeLists.txt
Do you see a newly generated Makefile? Good, make it up. As result you should get (among the other stuff) a couple of static libraries: libgtest.a and libgtest_main.a

I symlink-ed them in /usr/local/lib, then I symlink-ed the include/gtest directory to /usr/local/include. Now I am ready to write a tiny test application.

Here is the Makefile that I am going to use:
MY_NAME := gt
MY_SRCS := $(wildcard *.cpp)
MY_OBJS := ${MY_SRCS:.cpp=.o}

MY_INCLUDE_DIRS := /usr/local/include
MY_LIBRARY_DIRS := /usr/local/lib
MY_LIBRARIES := gtest

CXXFLAGS += $(foreach includedir,$(MY_INCLUDE_DIRS),-I$(includedir))
CXXFLAGS += -Wall -g -pthread
LDFLAGS += $(foreach librarydir,$(MY_LIBRARY_DIRS),-L$(librarydir))
LDLIBS += $(foreach library,$(MY_LIBRARIES),-l$(library))

.PHONY: all clean

all: $(MY_NAME)

$(MY_NAME): $(MY_OBJS)
    $(LINK.cc) -o $(MY_NAME) $(MY_OBJS) $(LDLIBS)

clean:
    @- rm -rf $(MY_OBJS) $(MY_NAME)
Notice that among the C++ flags it has been specified -pthread, since GTest requires the POSIX thread library. If you forget to specify it, you would get a bunch of undefined reference to pthreads symbols.

I reused the source code as defined in my old post about GTest on Windows that, as expected, works smoothly in this new context. The only (really minor) change that I applied is in the main function. Here I want to always run the tests and then give the control to the normal application execution path:
int main(int argc, char* argv[])
{
    Tester(argc, argv).run();

    std::cout << "Normal program behavior" << std::endl;
}
If you run it, you would get the output from GTest, where a test (TestIncrease.NormalBehavior) succeedes and another one (TestIncrease.ErrorTest) fails.

Alternatively, you could remove the C++ main function source from the build, and use instead the standard bootstrap functionality that GTest makes available. To do that, just add gtest_main among the libraries in the MY_LIBRARIES list.

Gary (thank you!) suggests me to stress a couple of points, very useful if you are new in this area:
  • Indentation in Makefile is done with tabs!
  • In the UNIX family of Operating Systems, the file separator is the slash ('/'), be careful not to use the backslash ('\')!

No comments:

Post a Comment