Setting up Google Test

Its official name is Google C++ Testing Framework but it is commonly known as Google Test. It is very close to other C++ Unit Test frameworks I have used before, all of them inspired by JUnit. The nice thing about the Google version is that is well supported and available for a number of different environments.

I am about to use it on Windows 7 - Visual C++ 2010.

Here is the few steps I have done to have it running:

I've got the 1.5.0 version (currently the latest) from google code (in my case 1.5), and unzipped it on my machine.

From gtest-1.5.0\msvc I have opened the gtest solution.

I have generated gtest.lib and gtestd.lib. Actually, there is a mistake in debug configuration, causing warning MSB8012. But it is quite easy to solve this issue: in the project properties, go to Configuration Properties - General - Target Name, and set it to "$(ProjectName)d" - the final "d" was missing.

Moreover, pay attention to the fact that: Configuration Properties - C/C++ - Code Generation - Runtime Library is set to "Multi-threaded" (/MT for release and /MTd for debug configuration). Your application and gtest library should have the same setting, otherwise you are bound to have nasty errors.

Finally I have my gtest libraries compiled with no warning.

Next step is creating a simple application to check if everything is working fine.

In the project properties (C/C++ - General - Additional Include Directories) I have put the path to the gtest header files, in (Linker - General - Additional Library Directories) the path to the gtest libs, and (Linker - Input - Additional Dependencies) the gtest library itself.

My project has just one function, defined in the simple header file:
#pragma once

extern int increase(int value);

And here is its implementation:
#include "simple.h"

int increase(int value)
  return ++value;

I created then a tiny class for test setup and usage:
#pragma once
#include "gtest\gtest.h"

class Tester
  Tester(int argc, char** argv) { testing::InitGoogleTest(&argc, argv); }

  int run() { return RUN_ALL_TESTS(); }

The advantage of having such a class is that it gets more difficult to forget calling the InitGoogleTest() function that has always to be called before running a test case.

Then, I created a couple of test cases:
#include "gtest\gtest.h"
#include "simple.h"

TEST(TestIncrease, NormalBehaviour)
  EXPECT_EQ(10, increase(9));

TEST(TestIncrease, ErrorTest)
  EXPECT_EQ(9, increase(9));

The second one, obviously wrong, is just to see what happens in case of failure.

I'm finally ready to write the main for my application:
#include <iostream>
#include "Tester.h"

int main(int argc, char* argv[])
  Tester t(argc, argv);
  if(int err =
    return err;

  std::cout << "Normal execution" << std::endl;
  return 0;
The idea is that first thing the test unit is run. If there are errors, we return just after testing completion, otherwise the normal execution is carried on. Actually, in this example the "normal execution" is just a message print to console.

Usually would be better to pause the execution of the application before ending - to have a chance to see the feedback when executed from the IDE, but I decided to use Guitar, the Google Unit Test Application Runner. A little useful application that takes in input the google test application and display the test result.

No comments:

Post a Comment