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.
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-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
Release versions. The resulting libraries end up in
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
gtest_maind.lib from the
Debug directory, 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
- Add an additional library directory
Linker -> Input, add a dependency on
Debugconfiguration. Unless you want to write your own main function that runs all the tests, also add a dependency on
gtest_maind.lib, respectively. This will add a
main()method to your project which discovers and runs all the tests.
Properties -> Linker -> System, set
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.