Run Google Tests for Your C++ Projects

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,

Inside the optimized folder clone repository from Google,

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

Create 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,

Inside the lib folder perform,

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

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

#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.

In your terminal run,

you can run your program,

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,

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.

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).

You can perform again,

and run your test,

The expected output,

Thank you for reading.



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