image of banner
Back

Quick start with Google Test

Hello world

Compilation

  • Download the archive with the code and unpack it.
  • 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:

Quick start with Google Test - Infopulse - 558305

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

  • Open Solutions you are going to test. Add a new project (C++console application).
  • 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.Quick start with Google Test - Infopulse - 334896Quick start with Google Test - Infopulse - 907066
  • 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. Start the testing project. Observe the following:Quick start with Google Test - Infopulse - 079857

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

We have a solution to your needs. Just send us a message, and our experts will follow up with you asap.

Please specify your request

Thank you!

We have received your request and will contact you back soon.