Google Test with Visual Studio (2010) – A Quick Start Guide

Google Test is the Google’s framework for writing C++ tests on variety of platforms. For more information you can go to Google Test project home. Google Test is not the only C++ testing framework. You can find full list of C++ testing frameworks here.

However setting up Google Code and writing the first test case might be little challenging for a first time user. This blog post will walk you through setting up and running your first test case using Google Test.

1. Downloading and build Google Test

Go to the Google Test home page : and select the second tab which is “Downloads”. Then you will be presented the available versions of Google Test. Select one and download.GoogleTestDownload

Once downloaded and extracted, you will find a “msvc” folder inside. In there you will find “gtest” visual studio solution file. Open that in Visual Studio. You will find  the following 4 projects.

gtestProjectStructureClean and build gtest and gtest_main projects.  For this scenario only a debug build is sufficient.

2. Create a project and write a program which you need to test.

Once you have built the gtest project , create a new project in Visual Studio. For demonstration purposes I made a simple calculator application. Calculator.h and Calculator.cpp is as follows.


class Calculator
int add(int a, int b);


#include "Calculator.h"

int Calculator::add(int a, int b)
return a+b;

Now starts the sticky part. Before getting your hands dirty I highly suggest you go through this article in Google Test documentation. If you are just lazy, never mind . Just continue with following steps.

At this point you have a method to test (add method in calculator class). Now you have to create the main class. There is a boilerplate main class provided by Google Test documentation. But since we are not going in depth in this stage following is the edited main class which will suit for our requirement.

#include "Calculator.h"
#include "gtest/gtest.h"

namespace {

class CalculatorTest : public ::testing::Test {


// Test Calculator::add method
TEST_F(CalculatorTest, CalculatorAddMethod) {
Calculator cal;
EXPECT_EQ(10, cal.add(6,4));



int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return 0;

3. Project Settings Configuration

Now you have to configure the following settings. Right click the project and select Properties.

3. 1 Additional include directories in C++

Configuration Properties -> C/C++ -> General -> Additional Include Directories -> Edit

add path of the include directory in the gtest folder you downloaded.

ConfigurationProperties13.2  Additional Library Directories in Linker

Configuration Properties -> Linker -> Additional Library Directories -> Edit -> Select the debug directory of the msvc folder in gtest downloaded folder.

ConfigurationProperties23.3 Additional Dependencies in Linker

Configuration Properties -> Linker -> Input ->Additional Dependencies -> Edit -> Add gtestd.lib and gtest_maind.lib.

ConfigurationProperties33.4  Runtime Library in C/C++

Configuration Properties -> C/C++ -> Code Generation ->Runtime Library -> Select “Multi-threaded Debug (/MTd)”

ConfigurationProperties44. Build and run

Now if you build and run the test, it should give you the following output.

PassingTestThis is how it will look like when all the tests are passed.

Now change the assertion line in main.cpp to following.

EXPECT_EQ(10, cal.add(6,6));

In this case our assertion is wrong, hence the test should fail. Now build and run this and you will get the following output.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s