Before playing with the Google Test library, it’s necessary to install it.
This is what this tutorial is made of, a bunch of elements in order to set up it.
In our example, we are going to use Visual Studio 2017, so on Windows, without any Command-Line Interface (CLI) nor the RUN_ALL_TESTS() macro.
First of all
Two tools are necessary to realise this Google Test tutorial:
- Visual Studio
- Google Test
Visual Studio
We admit you have already downloaded Visual Studio 2017, but if not you can always do it.
In our case it’s the Community version:
- Visual Studio 2017 Community: https://www.visualstudio.com/downloads/
Google Test
Of course, we need the Google Test framework.
For our tutorial we are going to use the 1.8.0 version.
To know exactly which version you use, I recommend using the Google Test official web page from GitHub and pick the version you want (in our case the 1.8.0 release):
- Google Test 1.8.0: https://github.com/google/googletest/releases
For our tutorial I choose to install Google Test in the following directory:
- C:\dev\c++\mylib\googletest-release-1.8.0
Projects
From Visual Studio, we are going to create a solution with 3 different projects.
So from the New Project window, we’ll have for the first project:
- Name: BadprogProject
- Location: C:\dev\c++\vs
- Solution name: Solution_1
For the second project:
- Name: BadprogProjectTests
- Location: C:\dev\c++\vs****Solution_1
For the third project:
- Name: GoogleTestFramework
- Location: C:\dev\c++\vs****Solution_1
Thus the solution is named Solution_1 and the 3 projects are:
- BadprogProject
- BadprogProjectTests
- GoogleTestFramework
As you can see, the first is the classic project, the second the testing project and the last the Google Test framework.
All projects will be without the Precompiled header option.
The reason here is that with this option the code won’t work properly, MSVC asking for some files such as stdafx.h even on extern libraries like Google Test.
And as we don’t want it, we unchecked the Precompiled header option on every project.
BadprogProject
This project is a Win32 Console Application with Additional options without Precompiled header (unchecked).
BadprogProjectTests
This project is a Win32 Console Application with Additional options wihtout Precompiled header (unchecked).
GoogleTestFramework
This project is a Win32 Console Application with Application type such as Static libraryand with Additional options without Precompiled header (unchecked).
Macros
Creating macros
As we have 3 projects using the same library (Google Test), we will create 2 macros: one for the Google Test home and the other for their includes.
From Visual Studio > View > Other Windows > Property Manager.
You should see now the 3 projects with 4 differents folders:
- Debug | Win32
- Debug | x64
- Release | Win32
- Release | x64
For this tutorial we will use only the Release | Win32 version, so free to you to use others if you want.
From the project of your choice (GoogleTestFramework for example).
Click the white triangle on the left side of Release | Win32 in order to display all parameters.
Inside you should see something like this:
- Microsoft.Cpp.Win32.user
- Whole Program Optimization
- Application
- Unicode Support
- Core Windows Libraries
So let’s take the first in the list (Microsoft.Cpp.Win32.user) and right click it > Properties.
The corresponding Property pages have appeared.
On the left, click Common Properties > User Macros.
Now on the right pane you click the Add Macro button.
The Add User Macro window has appeared.
Two text inputs must be filled:
- Name: GOOGLE_TEST_HOME
- Value: C:\dev\c++\mylib\googletest-release-1.8.0\googletest
Then check the option Set this macro as an environment variable in the build environment.
Click OK.
This macro is now added in the macro list.
Let’s add another one for the include folder:
- Name: GOOGLE_TEST_INCLUDE
- Value: C:\dev\c++\mylib\googletest-release-1.8.0\googletest\include
Once again check the option Set this macro as an environment variable in the build environment.
Now our 2 macros are ready, don’t forget to click Apply > OK.
Using macros
Let’s go back to our projects, from Visual Studio > View > Solution Explorer.
We are going to set each project with the new macros.
So right click the BadprogProject project then: Properties > Configuration Properties > C/C++ > General.
If you don’t see the C/C++ sub menu in the Configuration Properties, then you can instead use the VC++ Directories > Include Directories.
On the right side there is an Additional Include Directories parameter.
Click it and on the right you should see a drop down menu with an unique option: <Edit…>.
Click it.
The Include Directories have appeared.
On the bottom right, you have the Macros» button, click it.
It should open a list of current macros used in your Visual Studion environment.
You can check that our new macros are present, search from the letter G to see them.
Unfortunately it’s impossible to directly copy them on the left.
So it was just to check that our macros are saved.
OK, on the left side, click the tiny folder icon to add a new include directory and type:
$(GOOGLE_TEST_HOME)
Then once again type:
$(GOOGLE_TEST_INCLUDE)
Don’t forget to add the parenthesis otherwise it won’t work.
On the Evaluated valuepane (just under where you type the macros) you should see the full paths appear.
We have now our 2 include directories set.
Click OK to finish.
Then Apply > OK.
Do the same for the BadprogProjecTests.
And for the GoogleTestFramework, instead of using the C/C++ properties, use the following:
Right click GoogleTestFramework > Properties > VC++ Directories > On the right, select the Include Directories, and add the macros.
Adding some files
We have to add some files to the 3 projects.
BadprogProject
Right click > Add > New Item… > Header File (.h) > Calculation.h (as name) > Add.
Inside, type the following code:
// BadproG.com
#ifndef BADPROG_PROJECT_CALCULATION_H_
#define BADPROG_PROJECT_CALCULATION_H_
/**
* Adding the first parameter with the second.
*/
int add(const int first, const int second) {
return (first + second);
}
/**
* Substracting the first parameter with the second.
*/
int substract(const int first, const int second) {
return (first - second);
}
/**
* Multiplying the first parameter by the second.
*/
int multiply(const int first, const int second) {
return (first * second);
}
/**
* Dividing the first parameter by the second.
*/
int divide(const int first, const int second) {
return (first / second);
}
#endif
BadprogProjectTests
Delete everything and add the following code snippet to BadprogProjectTests.cpp:
// BadproG.com
#include "gtest/gtest.h"
#include "../BadprogProject/Calculation.h"
// adding
TEST(Test_Calculation, Adding)
{
EXPECT_EQ(5, add(2, 3));
EXPECT_EQ(-7, add(-2, -3));
}
// substracting
TEST(Test_Calculation, Substracting)
{
EXPECT_EQ(-1, substract(2, 3));
EXPECT_EQ(-7, substract(-2, -3));
}
// multiplying
TEST(Test_Calculation, Multiplying)
{
EXPECT_EQ(6, multiply(2, 3));
EXPECT_EQ(-7, multiply(-2, -3));
}
// dividing
TEST(Test_Calculation, Dividing)
{
EXPECT_EQ(2, divide(10, 5));
EXPECT_EQ(-7, divide(15, -3));
}
GoogleTestFramework
Right click GoogleTestFramework > Add > Existing Item… > then select the file:
- C:\dev\c++\mylib\googletest-release-1.8.0\googletest\include\gtest\gtest.h.
Do the same for the 2 following files:
- C:\dev\c++\mylib\googletest-release-1.8.0\googletest\src\gtest_main.cc
- C:\dev\c++\mylib\googletest-release-1.8.0\googletest\src\gtest-all.cc
By adding the gtest_main.cc in our project we skip the use of RUN_ALL_TESTS() macro and same for the ::testing::InitGoogleTest(&argc, argv) function.
Building the solution
Before building the solution we have to:
-
Setting BadprogProjectTests as startup project.
-
Adding GoogleTestFramework as reference.
-
Right click BadprogProjectTests > Set as StartUp Project.
-
From BadprogProjectTests, right click the References submenu > Add Reference… > Select GoogleTestFramework > OK.
Building the solution
It’s now time to rebuild the solution > Right click the Solution_1 > Rebuild Solution.
Everything should work and the following message appear in the Output:
1>------ Rebuild All started: Project: GoogleTestFramework, Configuration: Debug Win32 ------
2>------ Rebuild All started: Project: BadprogProject, Configuration: Debug Win32 ------
2>stdafx.cpp
1>gtest_main.cc
2>BadprogProject.cpp
2>Generating Code...
2>BadprogProject.vcxproj -> C:\dev\c++\vs\Solution_1\Debug\BadprogProject.exe
2>BadprogProject.vcxproj -> C:\dev\c++\vs\Solution_1\Debug\BadprogProject.pdb (Partial PDB)
1>gtest-all.cc
1>Generating Code...
1>GoogleTestFramework.vcxproj -> C:\dev\c++\vs\Solution_1\Debug\GoogleTestFramework.lib
3>------ Rebuild All started: Project: BadprogProjectTests, Configuration: Debug Win32 ------
3>stdafx.cpp
3>BadprogProjectTests.cpp
3>Generating Code...
3>BadprogProjectTests.vcxproj -> C:\dev\c++\vs\Solution_1\Debug\BadprogProjectTests.exe
3>BadprogProjectTests.vcxproj -> C:\dev\c++\vs\Solution_1\Debug\BadprogProjectTests.pdb (Partial PDB)
========== Rebuild All: 3 succeeded, 0 failed, 0 skipped ==========
Testing our tests
Here we are, it’s time to see if all our work was useful.
From Visual Studio > Debug > Start Without Debugging.
The cmd.exe should appear with all RUN (in green) and FAILED (in red) tests.
And the following display:
Running main() from gtest_main.cc
[==========] Running 4 tests from 1 test case.
[----------] Global test environment set-up.
[----------] 4 tests from Test_Calculation
[ RUN ] Test_Calculation.Adding
c:\dev\c++\vs\solution_1\badprogprojecttests\badprogprojecttests.cpp(10): error: Expected: -7
To be equal to: add(-2, -3)
Which is: -5
[ FAILED ] Test_Calculation.Adding (1 ms)
[ RUN ] Test_Calculation.Substracting
c:\dev\c++\vs\solution_1\badprogprojecttests\badprogprojecttests.cpp(17): error: Expected: -7
To be equal to: multiply(-2, -3)
Which is: 6
[ FAILED ] Test_Calculation.Substracting (1 ms)
[ RUN ] Test_Calculation.Multiplying
c:\dev\c++\vs\solution_1\badprogprojecttests\badprogprojecttests.cpp(24): error: Expected: -7
To be equal to: multiply(-2, -3)
Which is: 6
[ FAILED ] Test_Calculation.Multiplying (1 ms)
[ RUN ] Test_Calculation.Dividing
c:\dev\c++\vs\solution_1\badprogprojecttests\badprogprojecttests.cpp(31): error: Expected: -7
To be equal to: add(15, -3)
Which is: 12
[ FAILED ] Test_Calculation.Dividing (0 ms)
[----------] 4 tests from Test_Calculation (4 ms total)
[----------] Global test environment tear-down
[==========] 4 tests from 1 test case ran. (5 ms total)
[ PASSED ] 0 tests.
[ FAILED ] 4 tests, listed below:
[ FAILED ] Test_Calculation.Adding
[ FAILED ] Test_Calculation.Substracting
[ FAILED ] Test_Calculation.Multiplying
[ FAILED ] Test_Calculation.Dividing
4 FAILED TESTS
Press any key to continue . . .
Our those FAILED are normal.
Indeed, we passed wrong arguments in some our tests in order to test if the tests worked good.
Conclusion
An interesting way to learn a bit more how unit testing work with Google Test and Visual Studio.
Feel free now to change and adapt your tests on your needs.
Congratulations if you read until this point, you did it, good job!