# CTest

> For the complete documentation index, see [llms.txt](https://www.jetbrains.com/zh-cn/help/clion/llms.txt).

[CTest](https://cmake.org/cmake/help/latest/manual/ctest.1.html) 是一个作为 CMake 一部分分发的测试自动化工具。

> **Note:**
> CTest 支持的最低版本是 3.15.。
>
>
>
> CLion 使用位于工具链的  `cmake`  可执行文件同一文件夹中的  `ctest`  可执行文件。 不支持选择任意的  `ctest`  可执行文件。

## CTest 运行/调试配置

当您打开一个使用 CTest 的项目时，CLion 会检测测试并为其自动创建运行/调试配置。 此配置称为   所有测试  ，并基于   CTest 应用程序   模板。

您可以在  `编辑配置`  对话框中编辑自动创建的配置，并添加更多相同类型的配置。

![为 CTest 自动创建的配置](https://resources.jetbrains.com/help/img/idea/2026.3/cl_ctest_alltests_config.png)

Procedure: 创建 CTest 配置

1. 在主菜单中，转到  `运行 | 编辑配置` ，点击 ![](https://resources.jetbrains.com/help/img/idea/2026.3/app.expui.general.add.svg) 并从模板列表中选择   CTest 应用程序  ：

![添加 CTest 应用程序配置](https://resources.jetbrains.com/help/img/idea/2026.3/cl_ctest_addconfig.png)

2. 提供配置名称并指定设置。

* 在 要运行的测试列表 字段中，选择要包含的测试。 您可以在 可用测试列表 中使用智能搜索以便于导航： ![CTest 应用程序中可用测试的列表](https://resources.jetbrains.com/help/img/idea/2026.3/cl_ctest_config_testlist.png) 如果您使用的 `ctest` 版本低于 3.15.，或者项目中未检测到任何测试，CLion 会通知您。 ![测试检测失败警告](https://resources.jetbrains.com/help/img/idea/2026.3/cl_ctest_nodetect_warning.png) 在这种情况下，请检查 `设置 | 构建、执行、部署 | 工具链` 中的 CMake 版本，并确保在 `CMakeLists.txt` 文件中启用了 CTest 测试。 > **Note:** > 要使用 [带正则表达式的 CTest 选项](https://cmake.org/cmake/help/v3.7/manual/ctest.1.html#options) 筛选测试列表，请使用 CTest 参数 字段。

* 在 CTest 参数 字段中，您可以提供其他标志。 例如，指定 `--parallel` 以并行运行测试（如果您的测试支持）。

配置设置的其他字段在 [参考页面](ctest-application.html) 中有描述。

Procedure: 运行 CTest 测试

要运行或调试测试，请使用以下选项之一：

* 在切换器中选择配置并点击工具栏图标之一，或使用相应的快捷键。

![配置切换器和工具栏](https://resources.jetbrains.com/help/img/idea/2026.3/cl_ctest_configswitcher.png)

* 打开  `CMakeLists.txt`  文件并找到与所需测试对应的  `add_test`  命令。 点击其旁边的左侧边栏图标，并从选项列表中选择：

![测试的边栏菜单](https://resources.jetbrains.com/help/img/idea/2026.3/cl_ctest_gutter.png)

当您使用边栏图标运行测试时，CLion 会创建一个 [临时](run-debug-configuration.html) CTest 应用程序配置，该配置在配置列表中显示为灰色。 要保存它，请在  `编辑配置`  对话框中选择它并按 ![](https://resources.jetbrains.com/help/img/idea/2026.3/app.expui.general.save.svg)：

![保存临时配置](https://resources.jetbrains.com/help/img/idea/2026.3/cl_ctest_savetempconfig.png)

Procedure: 调试 CTest 测试

* 如果为 CTest 测试启动调试会话，CLion 将在调试模式下运行该测试本身，而不是启动  `ctest`  子进程。

对于多测试配置，系统会提示您从列表中选择一个测试：

![用于调试的测试列表](https://resources.jetbrains.com/help/img/idea/2026.3/cl_ctest_debug_testlist.png)

Procedure: 动态分析

* 您可以使用动态分析工具对测试进行分析，以消除运行时问题。 CLion 与 [Valgrind Memcheck](memory-profiling-with-valgrind.html)、 [Google Sanitizers](google-sanitizers.html)、 [CPU Profiler](cpu-profiler.html) 和 [Code Coverage](code-coverage-clion.html) 集成，为您提供可视化输出和便捷功能，帮助您处理结果。 有关更多信息，请参阅相应的文章。

![测试边栏菜单中的性能分析](https://resources.jetbrains.com/help/img/idea/2026.3/cl_ctest_gutter_profile.png)

> **Note:**
> 与调试情况不同，动态分析适用于  `ctest`  下的执行。

Procedure: 查看结果

* 当您运行测试时，CLion 会在内置的 [测试运行窗口](viewing-and-exploring-test-results.html) 中显示结果和过程。 此窗口包括：

* 进度条 ，显示已执行测试的百分比，

* 树视图 ，显示所有正在运行的测试及其状态和持续时间，

* 控制台 输出 流，

* 工具栏 ，提供用于重新运行失败的 ![](https://resources.jetbrains.com/help/img/idea/2026.3/app.runConfigurations.testState.red2.svg) 测试、导出 ![](https://resources.jetbrains.com/help/img/idea/2026.3/app.expui.general.export.svg) 或打开自动保存的先前结果 ![](https://resources.jetbrains.com/help/img/idea/2026.3/app.vcs.history.svg)、按字母顺序对测试排序 ![](https://resources.jetbrains.com/help/img/idea/2026.3/app.objectBrowser.sorted.svg) 以便轻松查找特定测试，或按持续时间对测试排序 ![](https://resources.jetbrains.com/help/img/idea/2026.3/app.runConfigurations.sortbyDuration.svg) 以了解哪些测试的运行时间比其他测试更长的选项，

* 上下文菜单中包含诸如 跳转到源 之类的操作，可导航到声明测试的 `CMakeLists.txt` 文件。

![测试运行窗口](https://resources.jetbrains.com/help/img/idea/2026.3/cl_ctest_testrunner.png)

Procedure: 跳过测试

* CTest 在某些情况下可以忽略测试失败。 要配置此项，请设置 [SKIP_RETURN_CODE](https://cmake.org/cmake/help/latest/prop_test/SKIP_RETURN_CODE.html#prop_test:SKIP_RETURN_CODE) 或 [SKIP_REGULAR_EXPRESSION](https://cmake.org/cmake/help/latest/prop_test/SKIP_REGULAR_EXPRESSION.html) 属性。 CLion 将检测这些属性，并在输出树中将测试标记为  已跳过 ：

![正在跳过测试](https://resources.jetbrains.com/help/img/idea/2026.3/cl_ctest_skip.png)

## See also

### 快速开始

[单元测试教程](unit-testing-tutorial.html) [快速 CMake 教程](quick-cmake-tutorial.html)

### 流程

[Run/Debug/Terminate Tests](performing-tests.html) [Explore Test Results](viewing-and-exploring-test-results.html)

### 参考

[CTest Application configuration](ctest-application.html)

