Google Test Support
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:
- Download Google Test.
- 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
(orgmock
, as you prefer) with your test targets in CMake like that:
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.
- 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: 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:
To create the Google Test run/debug configuration manually, do the following:
- In the main menu, select
- Click on the toolbar or press Alt+Insert. Select Google Test from the list.
- 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.
- Specify common run/debug configuration parameters.
- Apply the changes and close the dialog box.
. In the dialog that opens: - Click on the toolbar or press Alt+Insert. Select Google Test from the list.
To update an existing configuration:
In the main menu, select . In the dialog that opens:
- Expand the Google Test node and select the desired configuration.
- Update the required fields and apply changes.