Google Test Framework

In my recent past at my office place I have heard a lot about unit testing methodology. The amount of emphasis put on unit testing in software engineering was beyond my expectation when I first heard about it. However, very soon I have understood the importance of it, more so for automated testing. Unit testing the code written in C# is quite straight forward, mainly because of the framework provided by Visual Studio. However, things get little complicated when working on unit tests for C++, even more complicated when working on Linux. Well, the above statement was thought when I was just starting with unit testing.

Today, my topic to talk about is going to be Google Test and the emphasis here is the how I got it to work the very first time. There are several unit testing framework available for C++ with decent documentation. Hands down, one among them is Google Test framework. The complete source can be downloaded right from Github (git@github.com:google/googletest.git).

https://github.com/google/googletest/blob/master/googletest/docs/Primer.md

What made me write this blog is: the process to get started with unit testing on Google test for beginners.

Naive Approach

This approach does not make use of the Make utility, making the process little tedious. But this can give a good overview of the process of building the tests. I will also soon put up a process involving Make and CMake.

Here we go:

  1. Download the google-test source from the following link (git@github.com:google/googletest.git).

  2. For the sake of this tutorial I would be considering a convention for directory paths. After unzipping, create a directory for lib in X/googletest/ (X/googletest/gtest/lib). INFO: “include” should also be found in the same level.

  3. Navigate to X/googletest/gtest/lib and execute $ cmake .. && make.

  4. Create a new environment variable to store this prefix path (X/googletest/gtest). This can be done with the following command: $ export GTESTDIR=X/googletest/gtest

  5. The google tests must be written into a separate file and should be compiled using the following command: $g++ [] -I $GTESTDIR/include -L $GTESTDIR/lib -lgtest -lpthread

  6. The above command will create an executable, which when run will give results.

Example:

TODO

Written on March 18, 2018