CLion 2021.2 Help

CTest support

CTest is a test automation tool distributed as part of CMake.

CTest run/debug configuration

When you open a project that uses CTest, CLion detects the tests and automatically creates a run/debug configuration for them. This configuration is called All Tests and is based on the CTest Application template.

You can edit the automatically created configuration and add more configurations of the same type in the Edit Configurations dialog.

The automatically created configuration for CTests

Create CTest configurations

  1. Go to Run | Edit Configurations, click Icons general add and select CTest Application from the list of templates:

    Adding a CTest Application configuration
  2. Provide the configuration name and specify the settings.

    • In the Test list to run field, select the tests to be included. You can use smart search within the List of Available Tests for easier navigation:

      List of the available tests in CTest Application

      CLion will notify you if the version of ctest you are using is less than 3.14, or if there were no tests detected in the project.

      Test detection fails warning

      In this case, check the version of CMake in Settings / Preferences | Build, Execution, Deployment | Toolchain and make sure CTest tests are enabled in your CMakeLists.txt files.

    • In the CTest arguments field, you can provide additional flags. For example, specify --parallel to run the tests in parallel if your tests support that.

    Other fields of the configuration settings are described on the reference page.

Launching CTest tests

To run or debug tests, use one of the following options:

  • Select the configuration in the switcher and click one of the toolbar icons, or use the corresponding shortcuts.

    Configuration switcher and toolbar

  • Open the CMakeLists.txt file and find the add_test command that corresponds to the desired test. Click the left-hand gutter icon next to it and select from the list of options:

    Gutter menu for tests

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

    Saving a temporary configuration

Debugging

If you start a debug session for a CTest test, CLion will run the test itself under debug instead of launching the ctest child process.

In case of a multi-test configuration, you will be prompted to select a single test from the list:

Lists of tests for debugging

Dynamic analysis

You can use the tools of dynamic analysis for your tests to eliminate issues at runtime. CLion integrates with Valgrind Memcheck, Google Sanitizers, CPU Profiler, and Code Coverage, providing them with visualized output and handy features to help you work with the results. Refer to the corresponding articles for more details.

Profiling in the gutter menu for tests

Exploring results

When you run tests, CLion shows the results and the process in the built-in 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,

  • console output stream,

  • toolbar with the options to rerun failed Icons run configurations test state red2 tests, export Icons toolbar decorator export or open previous results saved automatically Icons vcs history, sort the tests alphabetically Icons object browser sorted to easily find a particular test, or sort them by duration Icons run configurations sortby duration to understand which test ran longer than others,

  • context menu with actions like Jump to Source, which navigates you to the CMakeLists.txt file where the test is declared.

Test runner window
Last modified: 21 July 2021