CLion 2020.2 Help

Catch2 support

Catch (Catch2) is a header-only testing framework.

Adding Catch2 to your project

  1. Download the latest version of catch.hpp header using the link from the documentation and copy it into your project tree.

  2. Include the header in your test files:

    #include "catch.hpp"

  3. In only one source file, precede the #include with either #define CATCH_CONFIG_MAIN or CATCH_CONFIG_RUNNER (use the latter if you want to supply your own main()).

Catch run/debug configuration

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

  2. Catch run/debug configuration

    Specify the configuration settings:

    • Set the configuration name in the Name field. This name will be shown in the list of the available run/debug configurations.

    • Select the Tags/Test option to run a test for the particular tags or all the tags. Select the Pattern option to run all the tests for a particular pattern.

    • Specify tags in the tags fields. This option is available only when the Tags/Test option is selected.

    • Select the desired test from the Test drop down list. Note, that this option is available only when one or more tags have been provided.

    • Specify the pattern name in the Pattern field. Note, that this option is available only when the Pattern option is selected.

    • In the Target field, select the desired target from the list of available targets.

  3. Save the configuration, and it's ready for Runicons.actions.execute.svg or Debugicons.actions.startDebugger.svg.

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.svg or failure icons.runConfigurations.testState.red2.svg.

When you run a test/suite/fixture using gutter icons, CLion creates a temporary Catch 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.svg:

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.svg tests, export icons.toolbarDecorator.export.svg or open previous results saved automatically artwork.studio.icons.profiler.toolbar.clock.svg, sort the tests alphabetically icons.objectBrowser.sorted.svg to easily find a particular test, or sort them by duration icons.runConfigurations.sortbyDuration.svg to understand which test ran longer than others.

test runner
Last modified: 22 October 2020