CLion 2017.2 Help

Google Test Support

Basics

Google Test is Google's cross platform C++ testing framework supported in CLion. Google Test support in CLion includes:

Getting started with Google Test

To begin with the Google Test just pass the following procedure:

  1. Download Google Test.
  2. Add Google Test to your project. The recommended way to do that is to add sources to your project, point to the include directories and link gtest (or gmock, as you prefer) with your test targets in CMake like that:
    /help/img/idea/2017.2/cl_GoogleCMakeLists.png
That's it.
Important note: on some platforms you need additional flags like:
  • On Windows, in case of Cygwin use the GNU dialect of the C++ language for your project:
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++0x")
  • On macOS, in case of Google Test v1.6.0 you’ll need to add the following lines to your project’s CMakeLists.txt file:
    if (APPLE) add_definitions(-DGTEST_USE_OWN_TR1_TUPLE) add_definitions(-D__GLIBCXX__) endif (APPLE)

Creating a Google Test run/debug configuration

Though you can simply run the Google Test target from your project as an ordinary configuration in CLion, to take advantage of the built-in test runner, create specific Google Test configuration (one or more). For every target linked with gtest or gmock it can be done automatically when you run a test/set of tests, or explicitly.

To create a Google Test run/debug configuration automatically, do one of the following:
  • In the editor, place caret on any line of a test code and run the test from the context menu or by pressing Ctrl+Shift+F10. CLion creates the related run/debug configuration automatically, as in example:
    /help/img/idea/2017.2/cl_GoogleTest_Any.png
    You can rerun it later, when needed.
  • In the project view, select a test source file (you can also place the caret in that file code in the editor or on a fixture name). In the context menu, select Run All in ... or press Ctrl+Shift+F10 to run the test. CLion automatically creates and saves the following configuration, which you can reuse later:
    /help/img/idea/2017.2/cl_GoogleTest_All.png

To create the Google Test run/debug configuration manually, do the following:

  • In the main menu, select Run | Edit Configurations. In the dialog that opens:
    1. Click add.png on the toolbar or press Alt+Insert. Select Google Test from the list.
    2. For the new Google Test run/debug configuration:
      • Specify its name in the Name text box. This name will be shown in the list of the available run/debug configurations.
      • Select the Suite/Test radio button to run a particular or all the tests of a suite. Select Pattern radio button to run all the tests of a particular pattern.
      • Specify the suite name in the suite text box. Note, that this option is available only when the Suite/Test radio button is selected.
      • Select the desired test from the Test drop down list. Note, that this option is available only when the Suite/Test radio button is selected.
      • Specify the pattern name in the Pattern text box. Note, that this option is available only when the Pattern radio button is selected.
      • In the Target field, select the desired target from the list of available targets.
      • In the Configuration field, select the desired configuration type from the list of available types. You can use configuration types other than provisioned by IDE. Refer to Changing Configuration Page page for details.
      • Specify common run/debug configuration parameters.
      • Apply the changes and close the dialog box.
      /help/img/idea/2017.2/cl_GoogleTest_New.png

To update an existing configuration:
In the main menu, select Run | Edit Configurations. In the dialog that opens:

  • Expand the Google Test node and select the desired configuration.
  • Update the required fields and apply changes.
Last modified: 26 October 2017

See Also