CLion 2019.2 Help

Google Test support

Adding Google Test to your project

  1. Download Google Test from the official repository and extract the contents of googletest-master into an empty folder in your project (for example, Google_tests/lib).

    Alternatively, clone Google Test as a git submodule or use CMake to download it.

  2. Create a CMakeLists.txt file inside the Google_tests folder: right-click it in the project tree and select New | CMakeLists.txt.

    Customize the following lines and add them into your script:

    # 'Google_test' is the subproject name project(Google_tests) # 'lib' is the folder with Google Test sources add_subdirectory(lib) include_directories(${gtest_SOURCE_DIR}/include ${gtest_SOURCE_DIR}) # 'Google_Tests_run' is the target name # 'test1.cpp tests2.cpp' are source files with tests add_executable(Google_Tests_run test1.cpp tests2.cpp) target_link_libraries(Google_Tests_run gtest gtest_main)
  3. In your root CMakeLists.txt script, add the add_subdirectory(Google_tests) command to the end, then reload the project.

  4. When writing tests, make sure to add #include "gtest/gtest.h" at the beginning of every .cpp file with your tests code.

Generate menu for Google Test

Use Generate menu to save time on writing test code: in a file where you have gtest included, press Alt+Insert to see the list of code generation options.

When called from a fixture, this menu additionally includes SetUp Method and TearDown Method:

generate menu for tests

For fixture tests, code generation converts TEST() macros into the appropriate TEST_F(),TEST_P(), TYPED_TEST(), or TYPED_TEST_P() (see Typed Tests).

Google Test run/debug configuration

Although Google Test provides the main() entry, and you can run tests as regular applications, we recommend using the dedicated Google Test run/debug configuration. It includes test-related settings and let you benefit from the built-in test runner, which is unavailable if you run tests as regular programs.

  1. To create a Google Test configuration, go to Run | Edit Configurations, click icons general add and select Google Test from the list of templates.

  2. Specify the test or suite to be included in the configuration, or provide a pattern for filtering test names. Auto-completion is available in the fields to help you quickly fill them up:

    completion in configuration fields

    Set wildcards to specify test patterns, for example:

    pattern for tests

    In other fields of the configuration settings, you can set environment variables and command line options. For example, use Program arguments field to pass the --gtest_repeat flag and run a Google test multiple times:

    flags in program arguments

    The output will be:

    Repeating all tests (iteration 1) ... Repeating all tests (iteration 2) ... Repeating all tests (iteration 3) ...

  3. Save the configuration, and it's ready for Runartwork studio icons avd run or Debugicons actions startDebugger.

Running tests

In CLion, there are several ways to start a run/debug session for tests, one of which is using special gutter icons. These icons help quickly run or debug a single test or a whole suite/fixture:

gutter icons for tests

Gutter icons also show test results (when already available): success icons runConfigurations testState green2 or failure icons runConfigurations testState red2.

When you run a test/suite/fixture using gutter icons, CLion creates a temporary Google Test configuration, which is greyed out in the configurations list. To save a temporary configuration, select it in the Edit Configurations dialog and press icons actions menu saveall:

saving temporary test configuration

Exploring results

When you run tests, the results (and the process) are shown in the test runner window. This window includes:

  • progress bar with the percentage of tests executed so far,

  • tree view of all the running tests with their status and duration,

  • tests' output stream,

  • toolbar with the options to rerun failed icons runConfigurations testState red2 tests, export icons toolbarDecorator export or open previous results saved automatically artwork studio icons profiler toolbar clock, sort the tests alphabetically icons objectBrowser sorted to easily find a particular test, or sort them by duration icons runConfigurations sortbyDuration to understand which test ran longer than others.

test runner
Last modified: 27 November 2019