December 19, 2017

Google Test Basic Tutorial

  1. Goal
  2. Pre-requisites
  3. Google Test
  4. Getting Started
    1. Populating the files
  5. Writing Tests
  6. Linking and Compiling and Running
  7. Ending


The goal of this tutorial is to get you up and running with Google Test through the examples given. This will be more of a walkthrough integrating Google Test into your project.


  • Some basic proficiency in C++
  • Some basic understanding of CMake
  • Some understanding of git and github

Google Test

Have this tab open for documentation. You'll need it.

Getting Started

We will want the following directory structure:

├── CMakeLists.txt
├── include
│   └── inc.h
├── libs
│   ├── CMakeLists.txt
│   └── googletest
├── src
│   ├── CMakeLists.txt
│   └──
└── tests
    ├── CMakeLists.txt

Populating the files

Create your directories include, libs, src, and tests. Then populate them with the following:


cmake_minimum_required(VERSION 3.1)



The library that we will be making with be very simple, consisting only of a single absolute value generic function. Because of good practice, we will enclose said function inside of an aptly named namespace.


#ifndef INC_H
#define INC_H

namespace lib {
    template<typename T>
    T abs(T);



#include "inc.h"

namespace lib {
    template<typename T>
    T abs (T v) {
        return v < 0? -v: v;




add_library(slib ${src})

Now, let's add Google Test as a git submodule. Change directory to libs, and execute the following:

git submodule add [email protected]:google/googletest.git googletest
git submodule update --init --recursive

After adding it as a submodule, we will need to link it to our existing project. Add to your root CMakeLists.txt file:


Insert the following into libs/CMakeLists.txt:


Writing Tests

Let's start with the tests/CMakeLists.txt:


add_executable(slib_test ${src})


# Link with google test libraries as well as simple-lib
target_link_libraries(slib_test gtest)
target_link_libraries(slib_test gtest_main)
target_link_libraries(slib_test slib)

# Your milage may vary with the COMMAND
    NAME atest
    COMMAND build/tests/slib_test

For all this to work, we will need to use add_subdirectory in the main CMakeLists.txt file, so make sure you do that.


For your tests/, it will basically be the same, since we are using Google Test. According to the documentation, you should have the following in that file:

#include "gtest/gtest.h"

int main(int argc, char** argv) {
    ::testing::InitGoogleTest(&argc, argv);

    return RUN_ALL_TESTS();

To test the single function that this library provides, we create and populate tests/ with the following:

#include "gtest/gtest.h"
#include "inc.h"

using lib::abs;

TEST(AbsFunction, Functionality) {
    ASSERT_EQ(abs(50), 50);
    ASSERT_EQ(abs(-50), 50);
    ASSERT_EQ(abs(0), 0);

Other tests are set as an exercise to the reader.

Linking and Compiling and Running

CMake projects are easy to link, compile, and run, given you have done your CMakeLists.txt files correctly.

mkdir build
cd build/

cmake ..
make test

And there you go! Since we are running the tests through CMake, it only shows up as one test. If one would wish to see more detailed results of tests, you would have to run the test executable directly.


Which would (hopefully) give the output:

[==========] Running 2 tests from 1 test case.
[----------] Global test environment set-up.
[----------] 2 tests from AbsFunction
[ RUN      ] AbsFunction.Functionality
[       OK ] AbsFunction.Functionality (0 ms)
[ RUN      ] AbsFunction.DifferentTypes
[       OK ] AbsFunction.DifferentTypes (0 ms)
[----------] 2 tests from AbsFunction (0 ms total)

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

Extra test added, of course.

In my opinion, it gives much prettier output. Though, if you have other methods of testing, make test would work nicely as well.


Here is a link to the tested project. Have a look!

Tags: testing tutorial c++