How to install and use GTest on Ubuntu

Install GTest on Ubuntu

# install packages
sudo apt-get install build-essential sudo apt-get install libgtest-dev sudo apt-get install cmake
# build gtest
cd /usr/src/gtest sudo cmake CMakeLists.txt sudo make # copy libraries to destination directory sudo cp lib/*.a /usr/lib



Create file sum.h with class to be unit-tested

#include <gtest/gtest.h>

class sum
int add(int a, int b)
return a + b;


Create file tests.cpp

#include <gtest/gtest.h>
#include "sum.h"

TEST( test_class_sum, test_sum )
sum sum_object;
ASSERT_EQ(sum_object.add(2, 2), 4);

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

Make and run tests

Create file CMakeLists.txt

cmake_minimum_required(VERSION 2.6) 
find_package(GTest REQUIRED) include_directories(${GTEST_INCLUDE_DIRS}) add_executable(runTests tests.cpp) target_link_libraries(runTests ${GTEST_LIBRARIES} pthread)

Build and run

cmake CMakeLists.txt

Test results

[==========] Running 1 test from 1 test suite.
[----------] Global test environment set-up.
[----------] 1 test from test_class_sum
[ RUN ] test_class_sum.test_sum
[ OK ] test_class_sum.test_sum (0 ms)
[----------] 1 test from test_class_sum (0 ms total)
[----------] Global test environment tear-down
[==========] 1 test from 1 test suite ran. (0 ms total)
[ PASSED ] 1 test.

How to install and use BOOST on Ubuntu

Getting started with Google Test (GTest) on Ubuntu

One thought on “How to install and use GTest on Ubuntu

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s