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

 

Example Program

SP Test 1

Add.h

This header file declares a function Add which will be tested in our test file.

 

SP Test 2

Add.cc

Add.cc defines the function declared in Add.h

 

SP Test 3

Test.cc

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, TwoAndTwo.

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

Then, main initializes the testing environment and runs all tests.

 

Compilation

g++ -I(path to googletest)/include/ -I(.h directory) -c Test.cc Add.cc
./a.out
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.

SP Test 4

 

If we change the source code to reflect a test that fails, we get a different output.

SP Test 5

UNTRUE example that will fail: Assert that Add(2,2) is equal to 5.

 

SP Test 6

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 testing directory.

The following shows how you can adjust your CMakeLists.txt file to allow for automatic testing in a large framework.

In CMakeLists.txt, insert the following:

SP Test 7

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 cmake/gtest.cmake:

SP Test 8

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.

SP Test 9

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!