Unit testing is very important to learn if you want to develop big software’s. There are many unit testing frameworks for c++ code and Google test is one of bests. I have already written one articles on Google test visual studio configuration and I ask you to see that first if haven’t already. Because in this article, I will carry on with the visual studio solution that I started in the previous article of google test. I am going to show you a simple example of how to start testing c++ code with the help of google test.
Google Test – Example
So, lets start by opening the solution that we made in the last article. Again what we made was a static library project of google test and UnitTests project as shown in the image below.
Now we must another project which contains the main code that we want to test. To do that, right click on Solution > Add > New Project and create a Win32 console application having name MyCode (with an empty project selected) as shown below.
Now we have three projects in our solution. In the project MyCode, we should have some code that execute in normal condition. To add some code, you need to make following following files in MyCode Project.
MathsMean.h
// MathsMean.h
#ifndef _MATHS_MEAN_H_
#define _MATHS_MEAN_H_
class MathsMean
{
public:
double Mean(int a, int b, int c, int d);
};
#endif
MathsMean.cpp
// MathsMean.cpp
#include "MathsMean.h"
double MathsMean::Mean(int a, int b, int c, int d)
{
return (a + b + c + d) / 4;
}
MathsMeanTest.cpp
#include "MathsMean.h"
#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
MathsMean* myMean = new MathsMean();
cout << myMean->Mean(3, 6, 2, 4);
return 0;
}
The above three files will run independently without unit tests as well (if Mycode project is set as startup project). There is a file MathsMeanTest.cpp, this is used to run this program. But other two files MathsMean.h and MathMean.cpp are performing the main functionality of the calculation mean. So, its obvious that the code which is performing the main functionality, should be testing with Google test.
You need to rebuild your MyCode project, if there are any error or not after adding these files. If you are done with no errors, you need to work with UnitTests project now. Right click on UnitTests project and go to its properties. In the properties, you have to go to VC++ Directories > Include Directories and add the path of your MyCode project in it as shown in the image below.
You need to give the path MyCode project in additional include directories as well as shown below.
After doing all this, click ok apply to save the changes in the properties. Now you need to do another thing, is to add a reference to MyCode project in Unit Tests Project. To do that, right click on UnitTests project > Add > Reference and select both the StaticLib and MyCode project in it as shown in the image below.
Note: I am using visual studio 2015. If you are using previous versions of visual studio, build might show some errors in the Output window while building UnitTests project. To resolve those errors, you need to again go to the properties of your project > C/C++ > Preprocessor > Preprocessor Definitions and add _VARIADIC_MAX=10 as shown in the image below.
Now we have setup our UnitTests project with all the references to our main code and Google test static library. We can start write unit tests code now in our UnitTests project. First of all, make a new cpp file in UnitTests project by right clicking on it > Add > New Item and name this cpp file MyTest.cpp. When it is added to your project, add the following code in this file.
#include <limits.h>
#include "gtest/gtest.h"
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
This is the main function that will detect all the tests in this project automatically and run those tests. After adding this code, you need to rebuild whole solution and verify there shouldn’t be any error. Now make a new cpp file in UnitTests project and name this file as MyMeanTest.cpp. In this file, we need to write some our fixtures and test cases. So, add the following code in MyMeanTest.cpp.
#include <limits.h>
#include "gtest/gtest.h"
#include "MathsMean.h"
class MultiplyTest : public testing::Test {
public :
MathsMean* myMean;
protected:
virtual void SetUp()
{
myMean = new MathsMean();
}
virtual void TearDown()
{
delete myMean;
}
};
TEST_F(MultiplyTest, findMean)
{
EXPECT_EQ(5.0, MultiplyTest::myMean->Mean(5, 2, 6, 7));
}
I have written the test fixture with two parameters. First parameter must be the class name of fixture and second can be any name to identify the test case name. Inside this text fixture EXPECT_EQ (check if both values are equal or not), I am checking Mean function by giving random integer values to check its equality with 5.0. We need to first make our UnitTests project as start up project. Now if you run our solution as Start without debugging, it will show the result some this like this.
This is the result that showing the test case have been passed and there was only one test case with the name MutiplyTest.findMean. We can have more test cases as well without test fixtures. For writing tests without fixtures, we use TEST, not TEST_F. And first parameter can be any name (rather than the test class name). In this case, you need to declare and initialize classes variables inside the tests. So, If any test case become false, that will be shown with red error in the result (cmd) window.
There are other important testing functionalities of Google test like death tests, fatal assertions, non fatal assertions, scoped trace etc. I would suggest you to read more on google test features from Google test samples.








