Tag Archives: C++

VS2012 + googletest

Introduction

Microsoft Visual Studio 2012 introduces a native C++ unit testing tool. It is definitely good thing because we don’t have to write write c++/cli code merely for testings. Still, there is a case when we cannot use VS tools for compatibility reason; in order to make a software platform independent, we’d better to make its testings codes platform independent too. There are many platform independent unit testing frameworks for C++. Among them, I use googletest for this article.

Using googletest with VS2012

Download and build

  • Download latest googletest (I downloaded gtest-1.6.0.zip)
  • Unzip it and open \msvc\gtest.sln. It is VS2003 solution so it requires upgrade.
  • Change number of variadic template parameters (in VS2012, 5 is the default value!). Add _VARIADIC_MAX=10 to PROJECT > Properties > Configuration Properties > C/C++ > Preprocessor > Preprocessor Definitions.
  • Press F7 and build solution both for Debug and Release mode.

Create your project

  • Add references to the built libraries to your project. For example, if the include files and built libraries are located on (your solution directory)\external\gtest-1.6.0\include and \lib. Add $(SolutionDir)external\gtest-1.6.0\include to VC++Directory > Include Directories and $(SolutionDir)external\gtest-1.6.0\lib to VC++Directory > Include Directories respectively, and add *.lib to Linker > Input.
  • Add your test code, e.g.

 Integrate googletest to VS2012 Test Explorer

  • If you don’t see Test Explorer, click TEST > Windows > Test Explorer.
  • Install googletest adapter plugin, and tests in your solution is automatically detected and listed in the Test Explorer window.
  • Click Run All or run selected tests.

VS2012 code coverage tool with googletest

Without any settings, the code coverage tool detects internal code of googletest. The easiest way to avoid it is to wrap your code into a namespace (e.g. mynamespace::*), then tell the code coverage tool not to include the code outside the namespace. 

  • Create .runsettings file in your solution directory (use the template in this page).
  • Add the following snippet to <Functions></Functions>.

  • Click TEST > Test Settings > Select Test Settings File, and load your .runsettings file.