The Google testing environment is frequently used in software engineering projects as a means of ensuring the functionality of your code.
It is a framework that highlights the fundamentals of the iterative development work flow with the ability to confirm a set of tests as you are developing, thereby increasing the reliability of your final product and reducing time spent trouble shooting overall.
The main tests that the framework allows for is assertions. An assertion simply checks to see if something is true, if two statements are equal, or if something ran as expected.
Confirming the functionality of your code is about using these assertions to explore the problem space of whatever function or operation you are implementing.
This means checking the bound cases of inputs, unexpected inputs, and fringe cases of behavior. If these tests fail, you iteratively adjust your code and continue improving it.
The Google testing environment is generally comprised of three parts: your code, your test case, and the Google testing library.
Your code is generally contained in an external file which you
#include into the test file. Let’s check out an example on a Linux environment:
Download and build Google test at https://github.com/google/googletest
This header file declares a function
Add which will be tested in our test file.
Add.cc defines the function declared in
This file defines a test with the macro,
TEST. There are two names inside of the parentheses which specify the name for the series of tests,
AddTest, and the specific test name,
This is the output to the console when the test passes or fails (see below). An assertion is used to check equality between
4 and the call of the function
Add with arguments (2, 2).
main initializes the testing environment and runs all tests.
g++ -I(path to googletest)/include/ -I(.h directory) -c Test.cc Add.cc
g++ -pthread *.o libgtest.a
Google testing uses pthreading and
-pthread must be passed as a flag when linking. For more information on compiling, check out our other post here.
If we change the source code to reflect a test that fails, we get a different output.
This very verbose output makes it easy to understand which function caused the failure and what to do to fix it.
Google testing can very easily be integrated into a personal project or homework assignment to add an air of high quality and professionalism.
Integrating with CMake
In a large software development project, there are often many test files which are stored in a
The following shows how you can adjust your
CMakeLists.txt file to allow for automatic testing in a large framework.
CMakeLists.txt, insert the following:
This calls the Google testing module and specifies that all
.cc files in the
test directory should be run.
The Google testing module will download Google testing locally to your project. It is stored in a
cmake folder as
After setting up the environment and building, running the executable titled
ProjectName_test in the
build directory will yield the following result after printing out results for each test.
It should be noted that when using this framework the
main() function should be omitted from each test file.
This scalable framework will run all tests in the
test directory, allowing you to modularly make sure that each file is working.
I hope you enjoyed this Google testing tutorial, if you have further questions please post them below!