Installing and Using GoogleTest with Visual Studio


Important note: This blog post is one of the top hits on Google for “googletest visual studio”. It is however quite old, and might no longer reflect the best way to use GoogleTest with Visual Studio. 

Google C++ Testing Framework (aka. GoogleTest) is a unittesting framework for C++. This post describes how to install it, and set it up in your project. I am using GoogleTest 1.6.0 here, but other versions should be similar. The instructions provided are for Visual Studio 2010, but 2012 should be exactly the same.

Installation

First of all, download the latest version from the GoogleTest download page, and unzip it. Inside, you will find a directory called msvc, which contains the Visual Studio solutions:

In this directory, you will find two solutions, gtest.sln, and gtest-md.sln. Which one you want depends on whether you are using a static or dynamic runtime. If you are unsure which one to use, take a look in your existing solution:

If you are using the DLL version of the runtime, use the gtest-md.sln solution, otherwise use gtest.sln. Before you open the solution though, make sure it is not read only, as Visual Studio will want to convert it to your version:

Open the solution you want, agree to convert the solution. Make sure you build it both in Debug and Release versions. The resulting libraries end up in gtest-1.6.0\msvc\gtest\Debug and gtest-1.6.0\msvc\gtest\Release, respectively. This is a good time to copy the libraries to wherever you keep libraries for your projects. The files you will need are gtestd.lib and gtest_maind.lib from the Debug directory, and gtest.lib and gtest_main.lib from the Release directory. In addition, you need all the headers from gtest-1.6.0\include. (Of course, you could just copy the entire gtest-1.6.0 directory and not care about which files you need.)

Setup for Your Project

I suggest to use one test project per production project. This makes it easy to find the tests you are looking for. Also, if your code is nicely decoupled, you might be able to link just these two projects, and not your entire solution. This can speed up your “red-green-refactor” cycle considerably. Finally, this makes it easy to exclude your test code from the final binary you ship. Here is an example from my Kjeller Software Community presentation:

Set the following properties for your test project:

  • Make sure to set up the test project to use the same runtime library as your production project (MT / MTd / MD / MDd).
  • Add an additional include directory c:\wherever\you\put\gtest\include
  • Add an additional library directory c:\wherever\you\put\the\libs
  • Under Linker -> Input , add a dependency on gtest.lib for your Release configuration, and gtestd.lib for your Debug configuration. Unless you want to write your own main function that runs all the tests, also add a dependency on gtest_main.lib / gtest_maind.lib, respectively. This will add a main() method to your project which discovers and runs all the tests.
  • Under Properties -> Linker -> System, set SubSystem to Console, to keep the test-window open after the tests have run.

Also make sure that your test project depends on the production project:

And that’s it! Now you can start writing and running tests, but since the documentation already describes that pretty well, I will not go into that here. If you want to have a look at the code from my Kjeller Software Community demo, it is available on GitHub.

If you enjoyed this post, you can subscribe to my blog, or follow me on Twitter.

13 thoughts on “Installing and Using GoogleTest with Visual Studio

  1. Thanks for the nice overview.

    Even after getting the _VARIADIC_MAX=10 trick, it still took some work to build gtest-md in VS 2012.

    First there are build warnings because the project’s final outputs don’t match the expected $(TargetPath). I think you can ignore these, but I actually changed the projects to make the outputs match.

    Then there are problems if you choose Rebuild Solution, because some of the cleanup happens at the beginning of each project. First, gtest-md builds fine, and then gtest_main-md builds fine. At that point it attempts to build gtest_prod_test-md and gtest_unittest-md. But since it’s a _rebuild_ command, additional cleanup first happens in the output directory. This has the side effect of deleting gtest.lib (the main output from the first project). And thus the last two projects fail to link. Building (not rebuilding) the projects one at a time (in the proper order) solves the problem.

  2. Most excellent! Thank you. This should be part of the Google Test documentation as how to get up and running quickly with Visual Studio.

  3. Another great solution from the Internet that doesnt work and never could have possibly worked. Great job.

    1. I’m sorry you didn’t find anything to help you in this post. As mentioned in the first paragraph, the article is old and not up to date. I hope you were able to find more up-to-date information elsewhere.

  4. There’s more to do if you want 32 bit and 64 bit build modes to be possible. It seems to be 32 bit only.

Leave a comment