Infopulse - Expert Software Engineering, Infrastructure Management Services
Send message Request a call
Send message Please fill in this quick form and we will send you a free quote shortly.
* Required fields
Request a call Please fill in this quick form and we will call you back shortly.
* Required fields
Subscribe to Infopulse Newsletter Please fill in this quick form to be among the first to receive our updates.
* Required fields
Send an email to Volodymyr Korniichuk Please fill in this quick form to contact our expert directly.
* Required fields
Read the rest of the Case Study Don't miss the most interesting part of the story!
Submit this quick form to see the rest and to freely access all case studies on our website.
* Required fields

Quick start with Google Test

Google Test is a framework from Google for unit-testing of C++ code. Its overall architecture somewhat resembles conventional boost::test and CppUnit, however, it’s slightly different in details (I think for to its advantage). I’ll describe briefly Hello world in Google Test, pointing out some potential issues you can face while using Google Test in developing for Visual Studio.

Compilation

  1. Download the archive with the code and unpack it.
  2. There are two files gtest.sln and gtest-md.sln in folder guest -1.6.0/msvc. These are Solution Visual Studio files. They differ in compilation options: gtest.sln compiles a code with an /MT key, and gtest-md.sln with an /MD key. If you don’t know what these keys are used for, you can read, for example, here or here. You should compile the same variant as in the project you are going to test. It is important lest you have a lot of odd linker errors. You can check the keys used in your project here:

Core property pages

Google Test code can be successfully compiled with Visual Studio 2008/2010 (I haven’t tried any other versions). At the end you get files gtestd.lib\gtest.lib (for debug and release configurations). That’s the whole compilation.

Hello world

  1. Open Solutions you are going to test. Add a new project (C++console application).
  2. Add to this project a dependency on libraries compiled on the second stage gtestd.lib\gtest.lib, the path to Google Test include-folder, dependencies for the projects in you solution that you are going to test.UnitTest property pages, inputUnitTest property pages, General
  3. Write the following code in the main file of testing project:
        #include "stdafx.h"
        #include "gtest/gtest.h"
        
        class CRectTest : public ::testing::Test {
        };
        
        TEST_F(CRectTest, CheckPerimeter) 
        {
        CSomeRect rect;
        rect.x = 5;
        rect.y = 6;
        ASSERT_TRUE(rect.GetPerimeter() == 22);
        }
        
        int main(int argc, char **argv) {
        ::testing::InitGoogleTest(&argc, argv);
        return RUN_ALL_TESTS();
        }
        
    

    Here, we test a rectangular for the accuracy of circumference calculation. Notice how convenient it is: there is no need both to register each test in the main function and write testing methods in include files.

  4. 4. Start the testing project. Observe the following:Observe the following

Problems:

Number 1

Don’t go wrong selecting the solution to be compiled in stage two. If you make an error and forget, then it will be practically impossible to find the error.

Number 2

If you plan to place the main testing application in different projects, you will face one tricky issue. The point is that Google unit tests are in fact compile-time classes and Visual C++ compiler with a bug inside will simply exclude these classes in the course of compilation. To avoid the bug, use the method described here.

Number 3

Remember to add the tested compile-class libraries not only to Dependencies of the tested project, but also to References, otherwise there will be linking errors.

Additional references

FAQ

Framework advanced use

Test-start plug-in for Visual Studio

Share this blog article:
Subscribe to our Newsletter