IEEE.org

Run Google Tests for Your C++ Projects

Markus Buchholz
4 min readJan 8, 2023

--

Software testing is the process of evaluating and confirming that a software application or product using software modules performs as intended. Testing has advantages such as bug detection, decreasing development costs, and finally affects better performance.

The following article describes simple steps allowing you to run GoogleTest in your C++ project.

The source code for this article you will find on my GitHub.

I will describe the method where the source files (googletest) are located on your machine.

For simplicity, I will depict the setup you can find on my GitHub,

mkdir optimizer

Inside the optimized folder clone repository from Google,

cd optimizer
git clone https://github.com/google/googletest

Create additional folders which will include your C++ files,

mkdir -p build src lib tests

Create CMakeFiles.txt,

touch CMakeFiles.txt

For a better understanding of how to configure CMakeLists.txt, I recommend an outstanding repository,

Your final folder optimizer tree will look as follows,

Now we will create a simple C++ program that multiplies numbers.

Inside the src folder perform,

touch main.cpp product.cpp

Inside the lib folder perform,

touch product.h

In main.cpp file we create object p and call two methods.

#include <iostream>
#include "product.h"


int main (){

Product p;

auto c1 = p.computeA(10,20);
auto c2 = p.computeB(10,20,30);
std::cout << "c1=" << c1 << " c2=" << c2 << "\n";

}

Files product.cpp and product.h can be depicted as follows,

//product.h
#pragma once

class Product
{

public:
int computeA(int, int);
int computeB(int, int, int);
};
//product.cpp
#include "product.h"

int Product::computeA(int a, int b)
{

return a * b;
}

int Product::computeB(int a, int b, int c)
{

return a * b * c;
}

In order to build our project (without GoogleTest) we have to prepare a simple CMakeList.txt file that we created before.

# $ cmake --version
cmake_minimum_required(VERSION 3.5)

# Set the project name
project (optimizer)

# add header files in the lib
include_directories(lib)

# Create a sources variable with a link to all cpp files to compile
set(SOURCES
src/main.cpp
src/product.cpp
)

# Add an executable with the above sources
add_executable(run_opt ${SOURCES})

target_include_directories(run_opt
PRIVATE
${PROJECT_SOURCE_DIR}/lib
)

In your terminal run,

cd build
cmake ..
make ..

you can run your program,

./run_opt

Now we will include GoogleTest in our project and create a simple test that tests our C++ program.
The tests we are going to run against our program we specify in an additional file we can create in the tests folder,

cd tests
touch utest-product.cpp

After creating the file we can create a test that will check the performance of our software. All available tests you will find here. I limited the test to logical comparison.

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

// https://google.github.io/googletest/reference/assertions.html

namespace
{
TEST(PersonTestSuite, functionA)
{
Product p;
EXPECT_EQ(p.computeA(10, 20), 200);
}
TEST(PersonTestSuite, functionB)
{
Product p;
EXPECT_EQ(p.computeB(10, 20, 30), 6000);
}

TEST(PersonTestSuite, not_equal_functionB)
{
Product p;
EXPECT_NE(p.computeB(10, 20, 30), 5000);
}

TEST(PersonTestSuite, fun_B_greater_funA)
{
Product p;
EXPECT_GT(p.computeB(10, 20, 30), p.computeA(10, 10));
}

}

If you consider the content of the file, you will notice the test compares the output from the methods which are called by GTest program.

In order to build the program which tests your C++ files (your program) you will have to add certain flags to your CMakeList.txt file (which you created before — I added self-explainable comments).

# $ cmake --version
cmake_minimum_required(VERSION 3.5)

# Set the project name
project (optimizer)

# add googletest subdirectory
add_subdirectory(googletest)

# this is so we can #include <gtest/gtest.h>
include_directories(googletest/include)

# add header files in the lib
include_directories(lib)

# Create a sources variable with a link to all cpp files to compile
set(SOURCES
src/main.cpp
src/product.cpp
)

# Add an executable with the above sources
add_executable(run_opt ${SOURCES})


# Set the directories that should be included in the build command for this target

target_include_directories(run_opt
PRIVATE
${PROJECT_SOURCE_DIR}/lib
)

enable_testing()
add_executable(run_opt_test
src/product.cpp tests/utest-product.cpp
)

target_link_libraries(run_opt_test GTest::gtest_main)

include(GoogleTest)
gtest_discover_tests(run_opt_test )

You can perform again,

cd build
cmake ..
make ..

and run your test,

./run_opt_test

The expected output,

Thank you for reading.

--

--