GoogleTest installation and introduction

GoogleTest is one of the C++ testing frameworks. It is built following xUnit architecture, so you should be able to start quickly if you have had any previous experience with other testing suites. If you don’t, it’s high time to start testing.

GoogleTest Installation

We will use latest GitHub release (1.8.0). You need to download the ZIP and build the code. Then we make a static library out of it.

wget https://github.com/google/googletest/archive/release-1.8.0.zip --no-check-certificate
unzip release-1.8.0 && rm release-1.8.0
mkdir -p googletest-release-1.8.0/googletest/lib/
cd googletest-release-1.8.0/googletest/lib/
g++ -isystem ../include -I../ -pthread -c "../src/gtest-all.cc"
ar -rv libgtest.a gtest-all.o
cd -

Now we are ready to build code. We just have to include the lib.

Main file to run all the tests

We want a file which will run all the registered tests. (We will see in a moment how to create tests).

Also, we need a simple makefile to build our tests. The only special thing is to wire sources with previously compiled code and headers.

GTEST_DIR=googletest-release-1.8.0/googletest
GTEST_LIB=$(GTEST_DIR)/lib/libgtest.a

CPPFLAGS =-std=c++14 -Wall -Wextra -Werror -Wformat-nonliteral -Winit-self -Wno-nonportable-include-path
CPPFLAGS += -pthread -isystem $(GTEST_DIR)/include/


all: build

build: main.o
 $(CXX) -o tests $^ $(GTEST_LIB) $(CPPFLAGS)

%.o: %.cpp
 $(CXX) -c -o $@ $< $(CPPFLAGS)

Now we build and run tests:

$ make build
g++ -o tests main.o .../libgtest.a -std=c++14 ... -pthread -isystem .../googletest/include/

And finally we can run the tests:

$ ./tests
[==========] Running 0 tests from 0 test cases.
[==========] 0 tests from 0 test cases ran. (0 ms total)
[ PASSED ] 0 tests.

We have only created a simple framework and no tests were registered. No test failed, so everything is OK.

The simplest test

We define a test using provided macros. We give a name to every test and specify in which test case it belongs:

EXPECT_EQ checks for equality.

Now we add our file to Makefile dependencies and rebuild. You will see the function got executed and did not fail:

$ ./tests
[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from ExampleTest
[ RUN ] ExampleTest.First
[ OK ] ExampleTest.First (0 ms)
[----------] 1 test from ExampleTest (0 ms total)

[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (0 ms total)
[ PASSED ] 1 test.

Tests with fixtures

Now we create a class representing a test case. Just as xUnit, we have a SetUp and TearDown methods, which will be called before every method in this suite.

Test method declaration is the same, except for the _F suffix. The framework will take care of preparing the environment for us every time:

$ ./tests
[==========] Running 3 tests from 2 test cases.
[----------] Global test environment set-up.
[----------] 2 tests from ExampleClass
[ RUN ] ExampleClass.Size
[ OK ] ExampleClass.Size (0 ms)
[ RUN ] ExampleClass.Swap
[ OK ] ExampleClass.Swap (0 ms)
[----------] 2 tests from ExampleClass (0 ms total)

[----------] 1 test from ExampleTest
[ RUN ] ExampleTest.First
[ OK ] ExampleTest.First (0 ms)
[----------] 1 test from ExampleTest (0 ms total)

[----------] Global test environment tear-down
[==========] 3 tests from 2 test cases ran. (0 ms total)
[ PASSED ] 3 tests.

Further reading

Be sure to check other cool features of the framework, like testing for program termination codes and working with display API!

Leave a Reply

Your email address will not be published. Required fields are marked *