Getting Started with Google Test on macOS

Google Test is a unit testing framework for C++. Let’s walk through how to get it installed and write some basic unit tests. Credits to Steven whose installation instructions I’ve condensed and added to here.

Part 1: Installation


  • Homebrew (run the following in your terminal):
xcode-select --installsudo xcodebuild -license accept

Then, restart your terminal and run:

ruby -e "$(curl -fsSL"
  • CMake:
brew install cmake
  • Python, which is most likely already installed on your system (run python -h to check this)

These instructions assume you are installing to your home directory, but you can install it anywhere you choose. Run each of these commands in your terminal.

cd ~/  
git clone
cd googletest
mkdir install
cd install
cmake ../ #creates a make file
make #compiles Google Test
sudo make install #installs Google Test
echo "export CPLUS_INCLUDE_PATH=/usr/local/include" >> ~/.bash_profileecho "export LIBRARY_PATH=/usr/local/lib" >> ~/.bash_profile

source ~/.bash_profile

Congrats! You’ve now installed Google Test.

Part 2: Writing Unit GTests and how to run them

Let’s create and test a simple C++ program that adds up the elements of an array. Create a folder in a location of your choice and write the following code in each file:


#include <iostream>double summer(double arr[] , int size)
double sum = 0;
for(int i = 0; i < size; i++)
sum += arr[i];
return sum;


#include <iostream>
//include the google test dependencies
#include <gtest/gtest.h>
//declare the function(s) that you are testing
double summer(double[], int);
//our first unit test
TEST(IntegerInputsSuite, simpleSum)
//first, set up any inputs to your
const int SIZE = 3;
double arr[SIZE] = {1, 2, 3};
//then, make an assertion to test
EXPECT_EQ(summer(arr, SIZE), 6) << "The sum is not correct";
TEST(IntegerInputsSuite, oneElement)
const int SIZE = 1;
double arr[SIZE] = {33};
EXPECT_EQ(summer(arr, SIZE), 33) << "The sum is not correct for array of size 1";
TEST(DoubleInputsSuite, simpleSum)
const int SIZE = 3;
double arr[SIZE] = {1.1, 1.1, 1};
EXPECT_EQ(summer(arr, SIZE), 3.2) << "The sum is not correct using double inputs";
int main(int argc, char **argv) {
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();

Let’s dissect our summerMain.cpp file. First, we need to include the Google Test dependencies. Next, when writing a test, we begin with TEST, then name our test suite (a group of tests) and then name our test case. In the first test above, we name our test suite IntegerTestSuite and out test case simpleSum.

Inside the TEST function, we need to set up any inputs to the function we are testing. Then, we make an assertion using EXPECT_EQ(). This function takes two arguments, and will evaluate each and check their equality. I have put my function that I’m testing first and the expected output second. You can also include a message that will be displayed if the test fails.

Finally, the main function on the last few lines will run the tests. It’s important to note that these are very simple tests and that there are many more available test functions in the GTest library, which you can optionally learn more about here.

Now let’s compile the program. You’ll use the following command each time you want to compile your code with the tests as you’re developing. Note that in the future, you’ll want to replace summer.cpp and summerMain.cpp with whatever files you are compiling. In addition, replace sumProgram with whatever you want the compiled program to be called.

g++ -std=c++0x summer.cpp summerMain.cpp -lgtest -lgtest_main -pthread -o sumProgram

This should create a compiled program called sumProgram. Let’s run it with the following command:


You should now see an output of your test results:

[==========] Running 3 tests from 2 test suites.
[----------] Global test environment set-up.
[----------] 2 tests from IntegerInputsSuite
[ RUN ] IntegerInputsSuite.simpleSum
[ OK ] IntegerInputsSuite.simpleSum (0 ms)
[ RUN ] IntegerInputsSuite.oneElement
[ OK ] IntegerInputsSuite.oneElement (0 ms)
[----------] 2 tests from IntegerInputsSuite (0 ms total)
[----------] 1 test from DoubleInputsSuite
[ RUN ] DoubleInputsSuite.simpleSum
[ OK ] DoubleInputsSuite.simpleSum (0 ms)
[----------] 1 test from DoubleInputsSuite (0 ms total)
[----------] Global test environment tear-down
[==========] 3 tests from 2 test suites ran. (0 ms total)
[ PASSED ] 3 tests.
An overview of how Test Driven Development works (credits)

Now that you’re able to write unit tests for your C++, you might also want to check out some of the basics of Test Driven Development. Happy testing!




Machine Learning Engineer. Interested in how ML/AI can be applied to both business and art.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How to Write a Command-Line Tool with Kotlin Multiplatform

Paying technical debt in the front-end


Creating Python Virtual Environment and Managing Dependency

Bulid a simple ML code to predict the marks on Docker container : task1SP2021

Hello 2022!

Running Jenkins Inside a Kubernetes Cluster

Dark Release

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alex Bussan

Alex Bussan

Machine Learning Engineer. Interested in how ML/AI can be applied to both business and art.

More from Medium

Two sum — Leetcode (easy)

Representative Of Thing (ROT) principle- (FP — OOP)

Cooked okra


Kruskal’s Algorithm using Greedy method