Freigeben über


How to use Google Test for C++ in Visual Studio

Applies to: yesVisual Studio noVisual Studio for Mac

Note

This article applies to Visual Studio 2017. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

In Visual Studio 2017 and later, Google Test is integrated into the Visual Studio IDE as a default component of the Desktop Development with C++ workload. To verify that it's installed on your machine, open the Visual Studio Installer. Find Google Test under the list of workload components:

Install Google Test

Add a Google Test project in Visual Studio 2017

  1. In Solution Explorer, right-click on the solution node and choose Add > New Project.
  2. In the left pane, choose Visual C++ > Test and then choose Google Test Project in the center pane.
  3. Give the test project a name and choose OK.

New Google Test Project

Configure the test project

In the Test Project Configuration dialog that is displayed, you can choose the project you want to test. When you choose a project, Visual Studio adds a reference to the selected project. If you choose no project, then you need to manually add references to the project(s) you want to test. When choosing between static and dynamic linking to the Google Test binaries, the considerations are the same as for any C++ program. For more information, see DLLs in Visual C++.

Configure Google Test Project

Set additional options

From the main menu, choose Tools > Options > Test Adapter for Google Test to set additional options. For more information about these settings, see the Google Test documentation.

Google Test Project settings

Add include directives

In your test .cpp file, add any needed #include directives to make your program's types and functions visible to the test code. Typically, the program is up one level in the folder hierarchy. If you type #include "../" an IntelliSense window will pop up and enable you to select the full path to the header file.

Add #include directives

Write and run tests

You're now ready to write and run Google Tests. For information about the test macros, see the Google Test primer. For information about discovering, running, and grouping your tests by using Test Explorer, see Run unit tests with Test Explorer.

See also

Write unit tests for C/C++