Google Test and Visual Studio

As you may surmise from the title, this post discusses integrating Google Test into Visual Studio. It will not discuss why you might want to use Google Test rather than Microsoft Test; I leave that to you to decide.

This post was originally written for Visual Studio 2015. This post was updated on July 30, 2017 to use Visual Studio 2017 version 15.5 or later. Included in the changes are the use of Test Adapter for Google Test rather than Google Test Adapter. The post was updated to also place the code to be tested in a .cpp file rather than just in a header file, as at least one person had problems adapting the instructions to his case.

To fully use Google Test with Visual Studio 2017, you need two components:

  • Google Test (and Google Mock, which is included in the download), and
  • A test runner.

Installing and Using Google Test

To use Google Test, you should build a static library for Google Test as a separate project in the solution for the code you want to test. You should also create a separate project containing the unit tests.

Installing Google Test

You need to perform this step only once:

  1. Download Google Test from github. Place it in its own directory, I chose C:\googletest, so if you place it in a different location, make the appropriate changes in the instructions given in the rest of this post.

Using Google Test

Perform the following steps for each solution that you use Google Test with:

  1. If you have not already generated a solution for your code, do so now. For this post, I will create a solution for a static library called MyProject: In Visual Studio, select the File->New->Project… menu item. This opens the New Project dialog shown below:
    googletest1
    Select Static Library, then set the name to MyProject, and the location to where you save your projects. Click the OK button to create the MyProject solution and project.
  2. Add Google Test to the solution: Add a new static library to the solution; call it GTest.
  3. In the Visual Studio Solution Explorer, select the GTest project. Add the files gtest_main.cc and gtest-all.cc from C:\googletest\googletest\src as Existing Items. DO NOT copy the files to GTest project directory.
  4. As with all software projects, you must determine if you will be sharing the code for your solution with others. If you are, or you intend to use Google Test in multiple solutions, then I recommend setting user-wide settings in Visual Studio as outlined in this step; otherwise, you can set the include directories directly (also specified in this step).
    You must add …\googletest and …\googletest/googletest to the project’s Include Directories.  You do so using one of the following methods (not both!):
    (a) Create a User Macro that contains the location of your googletest directory. You need to do this only once for each configuration that you build. The settings are used for every solution and project that you build from now on. See User-Wide Settings in Visual Studio 2015 for how to do this.
    I created a macro called GTEST that points to C:\googletest\googletest, then added the following to the Include Directories:
    . $(GTEST)
    . $(GTEST)/include
    (b) Add c:\googletest\googletest and c:\googletest\googletest\include to the Include Directories for the GTest project. You must perform this step for every project and every configuration in every solution that uses Google Test.
  5. In Solution Explorer, move the mouse cursor over the GTest project name and click the right mouse button to open the context menu. Select the Properties menu item to display the Properties Pages. Select C/C++->Precompiled Headers->Precompiled Header. Open the dropdown box for that property and select Not Using Precompiled Headers. Click on the OK button to apply that setting and close the dialog box.
  6. Now build the GTest project. There should be no build errors in the GTest project.
  7. If you performed step 4(a), above, and not step 4(b), open Windows Explorer and navigate to the directory containing the GTest project. Open the GTest.vcxproj file in a text editor; WordPad works well. Near the bottom of the file you will see the following lines:
     <ClCompile Include="..\..\..\..\..\..\googletest\googletest\src\gtest-all.cc" />
     <ClCompile Include="..\..\..\..\..\..\googletest\googletest\src\gtest_main.cc" />

    Change these lines to:

     <ClCompile Include="$(GTEST)\src\gtest-all.cc" />
     <ClCompile Include="$(GTEST)\src\gtest_main.cc" />

    Save the file and rebuild the project. There should be no errors.

  8. You need some code to test. For my simple MyProject project, I added a file called arithmetic.h that contains the following:
    #pragma once
    int square(const int value);

    and a file called arithmetic.cpp that contains the following:

    #include "stdafx.h"
    #include "arithmetic.h"
    int square(const int value)
    {
        return value * value;
    }

    I have included stdafx.h because I did not change the MyProject properties to not use precompiled headers. If you do not want to use precompiled headers in your code, then make sure that the Precompiled Header property for your code project is set as we did in step 5, above, for GTest.

  9. Add a new Windows Console Application project for the unit tests. I suggest that you name the project after the project containing the code under test and append Test or Tests (e.g. MyProjectTests). Set the Precompiled Header property to Not Using Precompiled Headers.
  10. In the Solution Explorer, select the References item for your tests project. Select the Add Reference… menu item from either the context menu or the Project dropdown menu. This opens the Add Reference dialog. Select the Solution item under Projects in the dialog. Now select both GTest and the project containing the code that you will be testing. For MyProject, the dialog will look like this:googletest2
    Click the OK button.
  11. Open the MyProjectTests.cpp file and replace any text in that file with the following code:
    #include "gtest/gtest.h"
    #include "../MyProject/arithmetic.h"
    
    TEST(SquareTests, Square)
    {
        EXPECT_EQ(0, square(0));
        EXPECT_EQ(-4, square(-2));
    }

    This code tests the square function that we created in arithmetic.h, above. If you performed step 4(b), above, you must repeat that step for your tests project.

  12. Rebuild the solution.
  13. Set MyProjectTests as the startup project. Start the program without debugging (using the Start Without Debugging menu item in the Debug dropdown menu, or by pressing Ctrl-F5). This will open a console window and run the tests. The output should look like this:googletest3
    Note that there is a failed test. The lines between [RUN ] SquareTests.Square and [ FAILED ] SquareTests.Square (3 ms) give both the location of the failed test and the expected and actual values. This indicates that either the code being tested is in error or the test itself is in error. In this case, it is the test that is in error. We will correct this later.

Installing  A Test Runner

As currently configured, Visual Studio and Google Test allow you to run tests, but you must look at every error and manually go to the test that errors. The MS Testing Framework is integrated into a test explorer. It would be nice if Google Test could also use the test explorer.

This is where test runners come in. There are two test runners for Google Test that are available as Visual Studio extensions:

  1. Test Adapter for Google Test; and,
  2. Google Test Adapter.

Test Adapter for Google Test is a fork of Google Test Adapter, and is installed automatically by Visual Studio Installer when you install the Desktop development with C++ workload (since Visual Studio 2017 version 15.5), so you might as well use that adapter.

Using Test Adapter for Google Test

  1. If the Test Explorer window is not open, open it by selecting the Test->Windows->Test Explorer menu item.
  2. For MyProject, the Test Explorer window looks like this:
    googletest2
    Note the single test called Square under SquareTests. If several tests are created, then all tests will be listed.
  3. To execute tests, select one of the Run items in the Test Explorer window, or one of the Test->Run-> menu items. Here is the result:
    googletest3.png
    Note the link to the stack trace. Selecting the stack trace item takes you to the test that failed.
  4. The square of -2 is 4, not -4, so the test is in error, not the square function. Correcting this bug, then building the solution again and rerunning the test gives:
    googletest4
    indicating that the test now passes.

That’s it. You can now continue to add more tests and more code to test.

You should also see the Test Adapter for Google Test documentation on GitHub for information on additional capabilities.

This post was updated on August 1, 2017 to add the following section.

Running Tests Automatically As Part of Build Process

Visual Studio Enterprise Edition

The following only works if you are using Visual Studio Enterprise Edition:

  1. Do either of the following, as they are equivalent:
    (a) Select the Test->Test Settings->Run Tests After Build menu item.
    (b) In Test Explorer, click the Run Tests After Build button at the top of the window. This button is shown in the image below. It is the one with the yellow background indicating that it is selected.
    runtestsafterbuild
  2. Rebuild the solution. The tests should run.

Other Editions of Visual Studio

Even though the menu item and button mentioned for the Enterprise Edition may be active, they only work for the Enterprise Edition. To run tests in other editions of Visual Studio, do the following:

  1. Open the properties dialog for the project containing the tests.
  2. Select Build Events->Post-Build Event.
  3. Set the Configuration that you want the tests to run for.
  4. Set the Platform that you want to tests to run for.
  5. For the command line, enter the location and name of the test executable {“$(TargetDir)MyProjectTests.exe” in this case}. Include the characters at both ends of the command above. Enter a description, such as Run Unit Tests. This text appears in the Build Output window when the tests are run. See the output text in step 7, below. The text is displayed in red, to highlight it; in the Build Output it will be black, just like all of the other text. Make sure that Use In Build is set to Yes.
  6. The properties dialog should now look similar to this:
    runtestspostbuild
    Click the OK Button to close the dialog.
  7. Rebuild the solution. The Output window will contain:
    1>------ Rebuild All started: Project: MyProject, Configuration: Debug x64 ------
    2>------ Rebuild All started: Project: GTest, Configuration: Debug x64 ------
    2>stdafx.cpp
    1>stdafx.cpp
    2>gtest-all.cc
    1>arithmetic.cpp
    1>MyProject.vcxproj -> C:\Users\jimor_000\Documents\Visual Studio 2017\Projects\MyProject\x64\Debug\MyProject.lib
    2>gtest_main.cc
    2>Generating Code...
    2>GTest.vcxproj -> C:\Users\jimor_000\Documents\Visual Studio 2017\Projects\MyProject\x64\Debug\GTest.lib
    3>------ Rebuild All started: Project: MyProjectTests, Configuration: Debug x64 ------
    3>stdafx.cpp
    3>MyProjectTests.cpp
    3>MyProjectTests.vcxproj -> C:\Users\jimor_000\Documents\Visual Studio 2017\Projects\MyProject\x64\Debug\MyProjectTests.exe
    3>Run Unit Tests
    3>Running main() from gtest_main.cc
    3>[==========] Running 1 test from 1 test case.
    3>[----------] Global test environment set-up.
    3>[----------] 1 test from SquareTests
    3>[ RUN      ] SquareTests.Square
    3>c:\users\jimor_000\documents\visual studio 2017\projects\myproject\myprojecttests\myprojecttests.cpp(7): error : Expected equality of these values:
    3>  -4
    3>  square(-2)
    3>    Which is: 4
    3>[  FAILED  ] SquareTests.Square (1 ms)
    3>[----------] 1 test from SquareTests (1 ms total)
    3>
    3>[----------] Global test environment tear-down
    3>[==========] 1 test from 1 test case ran. (2 ms total)
    3>[  PASSED  ] 0 tests.
    3>[  FAILED  ] 1 test, listed below:
    3>[  FAILED  ] SquareTests.Square
    3>
    3> 1 FAILED TEST
    3>C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\VC\VCTargets\Microsoft.CppCommon.targets(133,5): error MSB3073: The command ""C:\Users\jimor_000\Documents\Visual Studio 2017\Projects\MyProject\x64\Debug\MyProjectTests.exe"
    3>C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\VC\VCTargets\Microsoft.CppCommon.targets(133,5): error MSB3073: :VCEnd" exited with code 1.
    3>Done building project "MyProjectTests.vcxproj" -- FAILED.
    ========== Rebuild All: 2 succeeded, 1 failed, 0 skipped ==========

    The build process is not linked to the Test Explorer window, so the contents of that window are not updated. You must check the Build Output window to determine if any tests failed.

  8. If any tests fail, run all tests in the Test Explorer window. When you do this, the following message box is displayed:
    builderrors
    This is displayed because running the tests was part of the build process for the test project. If the tests ran, then the test program was created. You can therefore safely click on the Yes button to run the tests.
Advertisements

17 thoughts on “Google Test and Visual Studio

  1. Thanks for providing a tutorial on the Google Test Adapter! I hope it’s fine if I link this page from the Google Test Adapter homepage…

    Here’s a minor suggestion for improvement: the tutorial refers to the default naming scheme for test executables; this can however be overridden by configuring a test discovery regex, either through VS Options or (preferably) through a solution settings file to be shared via source control.

    One other note: Google Test Adapter is by now a complete rewrite (and much more feature complete), but has started as a C# port of the Google Test Runner by Markus Lindqvist.

    Like

    • Thank you for this contribution, it is very help. 2 remarks however:

      — I think step 2 has to be repeated as part of step 8 (otherwise the GTest includes are not available in the test project with the test code).
      I had a lot of compiler errors, which disappeared after asserting step 2 within step 8.

      — when building the solution (which contains those 3 projects) the following error occurs (copied from my output pane):
      “The following errors and warnings occured during test discovery (enable debug mode for more information):
      ERROR: Couldn’t find the msdia.dll to parse *.pdb files. You will not get any source locations for your tests.
      Warning: Could not find source location for test SquareTests.Square
      Cannot discover test extensions installed by NuGet. The NuGet service is not available. Tests may not be found due to missing test adapter extensions.”

      I installed GTA (succesfully) using Tools->Extensions and Updates… and the consecutive actions as stated above…..

      These errors/warnings do not seem to impede the test execution however….. the tests pass fine, debugger is launched….
      Are those errors relevant? What must be done to prevent them? What’s is missing in my configuration/setup( I thought the NuGet facilty was installed as part of the “overall” GTA tooling)?

      Regards,
      Herman

      Like

      • Hi Herman. Thanks for the input.
        (1) While step 2 (now step 4) made reference to adding c:\googletest\googletest and c:\googletest\googletest\include directly to the Include Directories, the rest of the post was written assuming user-wide settings. I have now corrected this by specifying what needs to be done in both cases.
        (2) msdia.dll is a file that is installed as part of Visual C++. The only thing I can think of is that it was not properly installed when you installed Visual Studio.

        Hope this helps.

        Like

      • Hi usingcpp,

        thanks for your reaction and the clarifications. In the meantime I’ve installed another VS version (2013, in addition to the 2017 version I installed earlier) and contrary to the 2017 version I performed a complete installation of all “default” modules. The “msdia.dll msg” didn’t occur anymore, which also solves the problem for the 2017 version because apparently the msdia.dll is included in the full installation and is also picked up by the 2017 environment thereafter. So the full installation solved the issue for me as you already suggested.

        Regards,
        Herman

        Like

  2. This is very helpful.

    In step 6 where it says “Near the bottom of the file you will see the following lines:
    Change these lines to:”, it doesn’t actually say what we should see or what we should change it to. What should go there?

    Also, should the test results show up in Visual Studio 2015’s Test Results and Test Runs windows? I’m not seeing them there.

    Like

    • Thanks for pointing this out, Richard. I have corrected this.
      I think the lines disappeared when I changed the theme for this blog. At least that is what I am blaming it on. 🙂

      Once you install GoogleTestAdapter, the results should show up in the Test Explorer window as shown in the post.

      Like

  3. Pingback: Writing Unit Tests | Jim's Adventures in Programming

    • I just repeated the process as outlined in the post up to step 12 of Using Google Test and everything works as indicated. The one difference with VS 15.3.3 (maybe 15.3.0 as well) is that in step 2, you must select Visual C++ ->Windows Desktop->Windows Desktop Wizard.
      Other than that, everything seems the same. You said that you could not make it build. At what step did you have the problem?
      The only references to error 455 that I could find are related to VB and VBA.

      Like

    • I have just updated the post for use with VS2017 version 15.5 or later. For version 15.3.3, you can install Test Adapter for Google Test (or Google Test Adapter) using the Tools->Extensions and Updates… menu item. Alternatively, you could update VS2017 to the latest version.

      Like

  4. Pingback: Unit Testing Framework for C/C++ – சிந்தனை சிதறல்கள்

  5. This works only because you put your production code in header file. In case you would like to test a class (which is declared in .h and defined in .cpp) it doesn’t work. There are LNK2019 problems.. Do you know how to fix it?

    Like

Leave a Reply

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

WordPress.com Logo

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

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s