# 运行测试

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

> **TL;DR**
> 运行选定的测试或测试文件夹： `Ctrl+Shift+F10` (Windows), `⌃ ⇧ R` (macOS), `⌃ ⇧ F10` (IntelliJ IDEA Classic (macOS)), `⌃ ⇧ R` (macOS System Shortcuts), `Ctrl+Shift+F10` (XWin), `Ctrl+Shift+F10` (GNOME), `Ctrl+Shift+F10` (KDE), `Ctrl+Shift+F10` (Emacs), `Ctrl+Shift+F10` (Sublime Text), `Ctrl+Shift+F10` (Sublime Text (macOS)), `Ctrl+Alt+F5` (Visual Studio), `⌃ ⌥ F5` (Visual Studio (macOS)), `Alt+Shift+F6` (NetBeans), `Ctrl+Shift+F10` (Eclipse), `⌃ ⇧ R` (Eclipse (macOS))
>
>
>
> 停止当前测试会话： `Ctrl+F2` (Windows), `⌘ F2` (macOS), `⌘ F2` (IntelliJ IDEA Classic (macOS)), `⌃ ⇧ .` (macOS System Shortcuts), `Ctrl+F2` (XWin), `Ctrl+F2` (GNOME), `Ctrl+2` (KDE), `Ctrl+F2` (Emacs), `Ctrl+F2` (Sublime Text), `Ctrl+F2` (Sublime Text (macOS)), `Shift+F5` (Visual Studio), `⇧ F5` (Visual Studio (macOS)), `Shift+F5` (NetBeans), `Ctrl+F2` (Eclipse), `⌘ F2` (Eclipse (macOS))

IntelliJ IDEA 提供运行、重新运行和调试测试的操作。 可以根据要测试的作用域，在 IDE 的不同位置调用这些操作：

* [编辑器](#run-tests)：单个测试或测试类。

* [结构工具窗口](#run_tests_from_structure)：单个测试类中的一个或多个测试。

* [项目工具窗口](#run-tests)：目录或软件包下的所有测试。

* [Run 微件](#run-with-options)：符合运行配置中指定条件的测试。

* [提交工具窗口](#run_tests_from_commit_tw)：包含未提交更改的测试类。

> **Note:**
> 本主题介绍如何在 IntelliJ IDEA 中使用专用操作运行测试。 如果想了解如何使用特定构建工具运行测试，请参阅以下主题：
>
>
>
> * [在 Maven 项目中进行测试](work-with-tests-in-maven.html)
>
> * [在 Gradle 项目中进行测试](work-with-tests-in-gradle.html)

Procedure: 直接在文件或文件夹中运行测试

如果您的测试在开始前不需要任何特定操作，并且您不想配置其他选项，例如 [代码覆盖率](code-coverage.html)，您可以使用以下选项运行它们：

* 将文本光标置于测试类以运行该类中的所有测试，或置于测试方法，并按 `Ctrl+Shift+F10` (Windows), `⌃ ⇧ R` (macOS), `⌃ ⇧ F10` (IntelliJ IDEA Classic (macOS)), `⌃ ⇧ R` (macOS System Shortcuts), `Ctrl+Shift+F10` (XWin), `Ctrl+Shift+F10` (GNOME), `Ctrl+Shift+F10` (KDE), `Ctrl+Shift+F10` (Emacs), `Ctrl+Shift+F10` (Sublime Text), `Ctrl+Shift+F10` (Sublime Text (macOS)), `Ctrl+Alt+F5` (Visual Studio), `⌃ ⌥ F5` (Visual Studio (macOS)), `Alt+Shift+F6` (NetBeans), `Ctrl+Shift+F10` (Eclipse), `⌃ ⇧ R` (Eclipse (macOS))。 或者，点击测试类或测试方法旁边的 ![](https://resources.jetbrains.com/help/img/idea/2026.3/app-client.expui.run.run.svg) 图标，然后从列表中选择   运行 '<test name>'  。

测试状态不同，对应的边栏图标也会有所变化：

* ![](https://resources.jetbrains.com/help/img/idea/2026.3/app-client.expui.actions.runAll.svg) 边框图标表示一组测试。

* ![](https://resources.jetbrains.com/help/img/idea/2026.3/app-client.expui.run.run.svg) 装订区域图标标记新测试。

* ![](https://resources.jetbrains.com/help/img/idea/2026.3/app-client.expui.gutter.runSuccess.svg) 边栏图标标记成功的测试。

* ![](https://resources.jetbrains.com/help/img/idea/2026.3/app-client.expui.gutter.runError.svg) 装订线图标标记失败的测试。

![使用装订区域图标运行测试](https://resources.jetbrains.com/help/img/idea/2026.3/run-test.png)

* 要运行文件夹中的所有测试，请在   项目   工具窗口中选择此文件夹并按下 `Ctrl+Shift+F10` (Windows), `⌃ ⇧ R` (macOS), `⌃ ⇧ F10` (IntelliJ IDEA Classic (macOS)), `⌃ ⇧ R` (macOS System Shortcuts), `Ctrl+Shift+F10` (XWin), `Ctrl+Shift+F10` (GNOME), `Ctrl+Shift+F10` (KDE), `Ctrl+Shift+F10` (Emacs), `Ctrl+Shift+F10` (Sublime Text), `Ctrl+Shift+F10` (Sublime Text (macOS)), `Ctrl+Alt+F5` (Visual Studio), `⌃ ⌥ F5` (Visual Studio (macOS)), `Alt+Shift+F6` (NetBeans), `Ctrl+Shift+F10` (Eclipse), `⌃ ⇧ R` (Eclipse (macOS)) 或从上下文菜单中选择   在 '文件夹' 中运行测试  。

Procedure: 使用 Run 小部件运行测试

当您运行测试时，IntelliJ IDEA 会创建一个临时运行配置。 您可以保存临时运行配置、更改其设置、与团队其他成员共享。 有关更多信息，请参见 [运行/调试配置](run-debug-configuration.html)。

1. 创建一个新的 [run configuration](run-debug-configuration.html)或者保存一个临时的。

2. 请使用主工具栏上的运行小部件选择您要运行的配置。

3. 点击 ![](https://resources.jetbrains.com/help/img/idea/2026.3/app-client.expui.run.run.svg) 或按 `Shift+F10` (Windows), `⌃ R` (macOS), `⇧ F10` (IntelliJ IDEA Classic (macOS)), `⌥ ⇧ R` (macOS System Shortcuts), `Shift+F10` (XWin), `Shift+F10` (GNOME), `Shift+F10` (KDE), `Shift+F10` (Emacs), `Shift+F10` (Sublime Text), `Shift+F10` (Sublime Text (macOS)), `Ctrl+F5` (Visual Studio), `⌃ F5` (Visual Studio (macOS)), `F6` (NetBeans), `Alt+Shift+X` (Eclipse), `⌘ ⇧ F11` (Eclipse (macOS))。

![运行测试的运行/调试配置](https://resources.jetbrains.com/help/img/idea/2026.3/run-run-config-tests.png)

Procedure: 从结构运行测试

在 [结构

工具窗口](viewing-structure-of-a-source-file.html#structure-tool-window)中，可以选择类中的一个或多个测试方法来运行。 在这种情况下，IDE 还会使用这些方法创建一个临时运行配置，可以 [保存](run-debug-configuration.html#save-as-permanent)并编辑。

1. 在   结构   工具窗口中，右键点击一个或多个测试方法，然后选择 ![](https://resources.jetbrains.com/help/img/idea/2026.3/app-client.expui.gutter.run.svg)   运行 '方法 名称'  （ `Ctrl+Shift+F10` (Windows), `⌃ ⇧ R` (macOS), `⌃ ⇧ F10` (IntelliJ IDEA Classic (macOS)), `⌃ ⇧ R` (macOS System Shortcuts), `Ctrl+Shift+F10` (XWin), `Ctrl+Shift+F10` (GNOME), `Ctrl+Shift+F10` (KDE), `Ctrl+Shift+F10` (Emacs), `Ctrl+Shift+F10` (Sublime Text), `Ctrl+Shift+F10` (Sublime Text (macOS)), `Ctrl+Alt+F5` (Visual Studio), `⌃ ⌥ F5` (Visual Studio (macOS)), `Alt+Shift+F6` (NetBeans), `Ctrl+Shift+F10` (Eclipse), `⌃ ⇧ R` (Eclipse (macOS))）。

![从工具窗口运行代码](https://resources.jetbrains.com/help/img/idea/2026.3/run-from-structure2.png)

该工具窗口允许将测试方法添加到现有运行配置（适用于 JUnit 和 TestNG 测试），其中测试作用域是通过 [模式](run-debug-configuration-junit.html#testKind)指定的。 可以添加来自多个不同类的方法来创建测试套件。

2. 在   结构   工具窗口中右键点击某个方法，然后选择：

* 如果只有一个具有 模式 测试作用域的配置，则选择 添加到临时套件：<Configuration_name> 。

* 如果有多个具有 模式 测试作用域的配置，则选择 添加到 JUnit 模式套件 (JUnit) / 添加到 Temp Suite (TestNG)。 在这种情况下，会出现一个弹出窗口，您可以在其中选择目标配置。

![从工具窗口运行代码](https://resources.jetbrains.com/help/img/idea/2026.3/run-from-structure.png)

Procedure: 从提交工具窗口运行测试

如果对测试类进行了更改，并希望在提交前运行这些测试，可以从 [提交](commit-and-push-changes.html#commit) 工具窗口执行此操作。 可以选择运行单个测试类、多个类或目录、模块或更改列表下所有未提交的测试类。

1. 转到   提交   工具窗口（ `Alt+0` (Windows), `⌘ 0` (macOS), `⌘ 0` (IntelliJ IDEA Classic (macOS)), `⌘ 0` (macOS System Shortcuts), `Alt+0` (XWin), `Alt+0` (GNOME), `Alt+0` (KDE), `Alt+0` (Emacs), `Alt+0` (Sublime Text), `⌘ 0` (Sublime Text (macOS)), `Alt+0` (Visual Studio), `Alt+0` (Visual Studio (macOS)), `Alt+0` (NetBeans), `Alt+0` (Eclipse), `⌘ 0` (Eclipse (macOS))）。

2. 右键点击测试类、目录、模块或更改列表，然后选择 ![运行图标](https://resources.jetbrains.com/help/img/idea/2026.3/app-client.expui.run.run.svg)   运行“<Test Class 名称>”  。

> **Tip:**
> 要选择多个项目，请按住 `Ctrl` (Windows), `Ctrl` (macOS), `Ctrl` (IntelliJ IDEA Classic (macOS)), `Ctrl` (macOS System Shortcuts), `Ctrl` (XWin), `Ctrl` (GNOME), `Ctrl` (KDE), `Ctrl` (Emacs), `Ctrl` (Sublime Text), `Ctrl` (Sublime Text (macOS)), `Ctrl` (Visual Studio), `Ctrl` (Visual Studio (macOS)), `Ctrl` (NetBeans), `Ctrl` (Eclipse), `Ctrl` (Eclipse (macOS))，同时点击每个项目。

![在提交工具窗口的上下文菜单中运行操作](https://resources.jetbrains.com/help/img/idea/2026.3/run-test-from-commit-tw.png)

这样，IntelliJ IDEA 会为你的测试选择创建并启动一个临时运行配置。 如果以后需要重新运行相同的测试，可以在运行微件中 [保存此配置](run-debug-configuration.html#save-as-permanent)，然后 [自定义其设置](list-of-run-debug-configurations.html)。

> **Tip:**
> 要保留运行配置的选项卡，即使执行另一个运行配置，也可以 [将其固定](run-tool-window.html#pin_tabs)。

在 IntelliJ IDEA 运行完您的测试后，它会在   运行   工具窗口中显示结果，并在该运行配置的选项卡上显示。 有关分析测试结果的详细信息，请参阅 [探索测试结果](viewing-and-exploring-test-results.html)。

![测试显示在运行配置选项卡中](https://resources.jetbrains.com/help/img/idea/2026.3/tests-in-run-configuration-tab.png)

右侧的控制台显示当前测试会话的输出。 测试结果列表上方的测试结果工具栏为您提供了几个有用的选项。

![测试结果工具栏](https://resources.jetbrains.com/help/img/idea/2026.3/ij_testResultsToolbar.png)

## 提交后运行测试

当您想检查更改是否会破坏代码时，可以通过运行测试作为提交检查来实现。

> **Note:**
> 此功能仅适用于 [Git](using-git-integration.html) 和 [Mercurial](using-mercurial-integration.html)。

Procedure: 设置测试配置

1. 按 `Alt+0` (Windows), `⌘ 0` (macOS), `⌘ 0` (IntelliJ IDEA Classic (macOS)), `⌘ 0` (macOS System Shortcuts), `Alt+0` (XWin), `Alt+0` (GNOME), `Alt+0` (KDE), `Alt+0` (Emacs), `Alt+0` (Sublime Text), `⌘ 0` (Sublime Text (macOS)), `Alt+0` (Visual Studio), `Alt+0` (Visual Studio (macOS)), `Alt+0` (NetBeans), `Alt+0` (Eclipse), `⌘ 0` (Eclipse (macOS)) 打开   提交   工具窗口，然后点击   显示提交选项   ![设置按钮](https://resources.jetbrains.com/help/img/idea/2026.3/app.expui.general.settings.svg)。

2. 在   高级提交检查  菜单下，   运行配置  选项旁，点击   选择配置  并选择您要运行的配置。 这可以是您的构建工具提供的测试配置，例如，  `gradle test` 或项目中的单个测试类。

![提交前检查菜单](https://resources.jetbrains.com/help/img/idea/2026.3/run-tests-before-commit-0.png)

在您设置好测试配置后，每次提交时都会运行指定的测试。

## 停止测试

Procedure:

在运行配置的选项卡中，使用测试结果工具栏上的以下选项：

* 点击 ![停止按钮](https://resources.jetbrains.com/help/img/idea/2026.3/app-client.expui.run.stop.svg) 或按 `Ctrl+F2` (Windows), `⌘ F2` (macOS), `⌘ F2` (IntelliJ IDEA Classic (macOS)), `⌃ ⇧ .` (macOS System Shortcuts), `Ctrl+F2` (XWin), `Ctrl+F2` (GNOME), `Ctrl+2` (KDE), `Ctrl+F2` (Emacs), `Ctrl+F2` (Sublime Text), `Ctrl+F2` (Sublime Text (macOS)), `Shift+F5` (Visual Studio), `⇧ F5` (Visual Studio (macOS)), `Shift+F5` (NetBeans), `Ctrl+F2` (Eclipse), `⌘ F2` (Eclipse (macOS)) 以立即终止该进程。

![停止运行测试](https://resources.jetbrains.com/help/img/idea/2026.3/ij_stop_running_tests.png)

* 点击 ![退出按钮](https://resources.jetbrains.com/help/img/idea/2026.3/app.expui.general.exit.svg)优雅地终止进程，允许关闭钩子运行。

![优雅地终止运行测试](https://resources.jetbrains.com/help/img/idea/2026.3/ij_terminate_tests_gracefully.png)

## 重新运行测试

Procedure: 重新运行单个测试

* 在   运行   工具窗口的运行配置选项卡上右键点击测试，然后选择   运行 '测试名称'  。

![重新运行单个测试](https://resources.jetbrains.com/help/img/idea/2026.3/ij_rerun_one_test.png)

Procedure: 重新运行会话中的所有测试

* 点击测试结果工具栏上的 ![重新运行按钮](https://resources.jetbrains.com/help/img/idea/2026.3/app.expui.run.rerun.svg)或按 `Ctrl+F5` (Windows), `⌘ R` (macOS), `⌃ F5` (IntelliJ IDEA Classic (macOS)), `⌘ R` (macOS System Shortcuts), `Ctrl+F5` (XWin), `Ctrl+F5` (GNOME), `Ctrl+5` (KDE), `Ctrl+F5` (Emacs), `Ctrl+F5` (Sublime Text), `⌘ R` (Sublime Text (macOS)), `Ctrl+Alt+R` (Visual Studio), `⌘ ⌥ R` (Visual Studio (macOS)), `Ctrl+F5` (NetBeans), `Ctrl+F5` (Eclipse), `Ctrl+F5` (Eclipse (macOS))以重新运行会话中的所有测试。

![重新运行所有会话测试](https://resources.jetbrains.com/help/img/idea/2026.3/ij_rerun_all_tests.png)

Procedure: 重新运行失败的测试

可以重新运行并调试上次测试运行中失败的测试，而无需重新运行已通过的测试。

* 要重新运行失败的测试，请转到   运行   工具窗口的工具栏，然后点击 ![重新运行失败测试图标](https://resources.jetbrains.com/help/img/idea/2026.3/intellij.platform.ide.expui.run.restartFailedTests.svg)   重新运行失败的测试  。

![重新运行失败的测试](https://resources.jetbrains.com/help/img/idea/2026.3/ij_rerun_failed_tests.png)

* 按住 `Shift` (Windows), `Shift` (macOS), `Shift` (IntelliJ IDEA Classic (macOS)), `Shift` (macOS System Shortcuts), `Shift` (XWin), `Shift` (GNOME), `Shift` (KDE), `Shift` (Emacs), `Shift` (Sublime Text), `Shift` (Sublime Text (macOS)), `Shift` (Visual Studio), `Shift` (Visual Studio (macOS)), `Shift` (NetBeans), `Shift` (Eclipse), `Shift` (Eclipse (macOS)) 并点击 ![](https://resources.jetbrains.com/help/img/idea/2026.3/app-client.expui.run.restartFailedTests.svg) 选择是否要再次   运行   失败的测试或   调试   它们。

您可以配置 IDE 以触发在先前测试运行期间被忽略或未开始的测试以及失败的测试。 点击测试结果工具栏上的 ![设置按钮](https://resources.jetbrains.com/help/img/idea/2026.3/app.expui.general.settings.svg)并启用   将未启动的测试包括到重新运行的失败项中。  选项。

默认情况下，IntelliJ IDEA 会重新运行失败的测试，以及原本计划运行但未启动的测试（例如，因为测试运行提前停止）。

> **Tip:**
> 如果使用专用的 [JUnit](run-debug-configuration-junit.html) 或 [TestNG](run-debug-configuration-testng.html) 运行配置来运行测试，可以配置这些操作，使其仅重新运行失败的测试。 为此，请转到   运行   工具窗口的工具栏，选择  `![三个垂直 DOTS 图标](https://resources.jetbrains.com/help/img/idea/2026.3/intellij.platform.ide.expui.general.moreVertical.svg) 更多 | ![齿轮图标](https://resources.jetbrains.com/help/img/idea/2026.3/intellij.platform.ide.expui.general.settings.svg) 测试运行程序设置` ，然后清除   将未启动的测试包含到重新运行的失败测试中   选项。

Procedure: 自动重新运行测试

在 IntelliJ IDEA 中，您可以启用类似自动测试的运行器：当前运行配置中的任何测试在您更改相关源代码后会自动重新启动。

1. 点击 ![](https://resources.jetbrains.com/help/img/idea/2026.3/app-client.expui.actions.rerunAutomatically.svg)   自动重新运行  可在测试结果工具栏上启用类似于 autotest 的运行程序。

![自动重新运行选项](https://resources.jetbrains.com/help/img/idea/2026.3/ij_rerun_test_automatically.png)

2. 此外，您可以在编辑器中启用自动测试弹窗。 它表明类似自动测试的运行器已启用。 点击工具栏上的 ![](https://resources.jetbrains.com/help/img/idea/2026.3/app-client.actions.more.svg)   更多   图标并启用   在编辑器中显示自动测试状态  。

如果启用了类似自动测试的运行器，编辑器中会显示   自动测试开启   ![](https://resources.jetbrains.com/help/img/idea/2026.3/app-client.expui.actions.rerunAutomatically.svg)弹窗。 点击弹出窗口中的 ![](https://resources.jetbrains.com/help/img/idea/2026.3/app-client.expui.actions.rerunAutomatically.svg) 以禁用测试的自动重新运行。

![编辑器中的自动测试弹窗](https://resources.jetbrains.com/help/img/idea/2026.3/ij_autotest_popup.png)

## 调试失败的测试

如果您不知道测试失败的原因，可以调试它。

Procedure:

1. 在编辑器中，点击您想要设置断点的行的边距。

您可以根据希望暂停程序的位置使用不同类型的断点。 有关更多信息，请参见 [断点](using-breakpoints.html)。

2. 右键点击 ![](https://resources.jetbrains.com/help/img/idea/2026.3/app-client.expui.gutter.runError.svg) 槽图标并选择   调试 '测试名称'  。

测试将以调试模式重新运行。 之后，测试将会被暂停，允许您 [检查其当前状态](examining-suspended-program.html)。

您可以 [逐步执行测试](stepping-through-the-program.html)以详细分析其执行情况。

![使用边栏图标调试测试](https://resources.jetbrains.com/help/img/idea/2026.3/debug-test.png)

## See also

### 过程

[Testing in Maven projects](work-with-tests-in-maven.html) [Testing in Gradle projects](work-with-tests-in-gradle.html) [Debugging code in IntelliJ IDEA](debugging-code.html)

### 参考

[运行工具窗口](run-tool-window.html)

