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.
Two tools are necessary to realise this Google Test tutorial:
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:
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):
For our tutorial I choose to install Google Test in the following directory:
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:
For the second project:
For the third project:
Thus the solution is named Solution_1 and the 3 projects are:
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.
This project is a Win32 Console Application with Additional options without Precompiled header (unchecked).
This project is a Win32 Console Application with Additional options wihtout Precompiled header (unchecked).
This project is a Win32 Console Application with Application type such as Static library and with Additional options without Precompiled header (unchecked).
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:
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:
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:
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:
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.
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 value pane (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.
We have to add some files to the 3 projects.
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
// 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)); }
Right click GoogleTestFramework > Add > Existing Item... > then select the file:
Do the same for the 2 following files:
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.
Before building the solution we have to:
1. Right click BadprogProjectTests > Set as StartUp Project.
2. From BadprogProjectTests, right click the References submenu > Add Reference... > Select GoogleTestFramework > OK.
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 ==========
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.
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!
Comments
Christian Soltenborn (not verified)
Saturday, September 30, 2017 - 7:39pm
Permalink
Great tutorial! I'm only
Great tutorial! I'm only missing a hint on the Visual Studio extensions which integrate Google Test tests into Visual Studio's test explorer.
In particular, have a look at https://github.com/csoltenborn/GoogleTestAdapter
Disclaimer: I'm the author of that extension.
Gillian Grayson (not verified)
Thursday, November 22, 2018 - 9:44pm
Permalink
Thank you for such great
Thank you for such great extension!
Can you please help me with this situation:
I have Microsoft Visual Studio 2017 Community Edition and Intel Parallel Studio XE 2019 above it.
With standart Visual C++ compiler I can use gtest very easily - just added your adapter for Google Test.
I want to use gtest with Intel Compiler in Visual Studio. By default it is impossible - there is no gtest.
How to link excisting gtest adapter to Intel Compiler?
Christian (not verified)
Friday, January 4, 2019 - 11:40am
Permalink
Can you please open an
Can you please open an according issue on GTA's GitHub page, i.e., https://github.com/csoltenborn/GoogleTestAdapter? I just saw this by chance...
Mi-K
Sunday, October 1, 2017 - 8:03pm
Permalink
Thank you Christian.
Thank you Christian.
I had another tutorial about Google Test and I talked a bit about your adapter: https://www.badprog.com/c-google-test-generating-the-gtest-lib-from-gtest-sln-with-visual-studio.
Very good extension.
Anjali (not verified)
Friday, August 2, 2019 - 9:29am
Permalink
I followed the same steps but
I followed the same steps but build is failing for me.
Generating Code...
Done building project "GoogleTestFramework.vcxproj" -- FAILED.
Done building project "BadprogProjectTests.vcxproj" -- FAILED.
========== Build: 1 succeeded, 2 failed, 0 up-to-date, 0 skipped ==========
Mi-K
Friday, August 2, 2019 - 12:47pm
Permalink
Hello Anjali,
Hello Anjali,
Have a look to the Error list.
What does it say?
JG (not verified)
Tuesday, March 3, 2020 - 7:16am
Permalink
Thanks for the useful article
Thanks for the useful article. I try to setup googletest in VS 2017 as your article.
I succeed to follow your instructions. But, when I declare a class of testing source, LNK 2019 occurs.
For example
#include "gtest/gtest.h"
#include "MyTest.h"
TEST(test, test) {
MyTest test; // error: LNK2019
}
Could you help my problem?
Add new comment