August 10, 2020

327 words 2 mins read

HowTo - GTest + Visual Studio

HowTo - GTest + Visual Studio

During upgrade of old Visual Studio solutions you can run into a problem, that old Google Test code is not compilable anymore. How to fix it? It is much easier than you expect.

I saw multiple times in multiple different projects, that Google Test library is just committed to the project repository. For example, it was the case in ETViewer project.

In most of the cases, Google Test code copy is not maintained and not regularly updated. It was added once in the past and used for a while. What happens next? When old project is migrated to new Visual Studio, the following happens:

Google Test Compilation errors

Error message is the following: > Error C4996: ‘std::tr1’: warning STL4002: The non-Standard std::tr1 namespace and TR1-only machinery are deprecated and will be REMOVED. You can define _SILENCE_TR1_NAMESPACE_DEPRECATION_WARNING to acknowledge that you have received this warning

Of course, you can try to suppress this warning, switch to older C++ standard, manually update Gtest and do a lot of other workarounds to fix the problem. But there is an easy and nice solution.

Modern Visual Studio versions provide in-box integrations with Google Test.

How to use it? Just follow the steps below:

Step 1: Remove all Google Test code from your repository

Make sure that there were no custom behavior changing patches on it and remove it completely.

Step 2: Install Google Test Adapter component

Go to Visual Studio Installer and make sure “Google Test Adapter” component is installed.

If not, go to “Individual components” and select it for installation.

Google Test Adapter installation

After that you will be able to create Google Test projects.

Step 3: Create Google Test project

Now let’s create a new project:

Create Google Test project

Project is there, you can use it in your solution.

Step 4: Move your test code to new project

Now you can move your test code from old projects to new one and use it. Visual Studio will handle all the dependencies for you.


Image credits: