# Maven 测试

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

在 Maven 项目中，您可以按照在任何其他项目中使用 [创建](create-tests.html)和 [运行](performing-tests.html)测试的相同方式，使用 [默认 IntelliJ IDEA test runner](delegate-build-and-run-actions-to-maven.html#delegate_to_maven)。

Procedure: 运行一个简单的 JUnit 测试

1. 打开您的 Maven 项目。

2. 在编辑器中创建或打开一个测试类，然后点击装订区域中的 ![运行按钮](https://resources.jetbrains.com/help/img/idea/2026.3/app.runConfigurations.testState.run_run.svg) 运行，或从上下文菜单中选择其他选项，例如 ![“带覆盖率运行”选项](https://resources.jetbrains.com/help/img/idea/2026.3/app.expui.run.runWithCoverage.svg)   使用覆盖率运行测试  。

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

结果显示在   运行  工具窗口中。

如需了解有关创建或运行常规测试的更多信息，请参阅 [Testing](performing-tests.html)部分。

您可以 [委托](delegate-build-and-run-actions-to-maven.html#run_debug_with_maven)测试执行给 Maven。

您还可以在运行 JUnit 或 TestNg 测试时传递 [Maven Surefire plugin](http://maven.apache.org/surefire/maven-surefire-plugin/) 参数，并在运行集成测试时传递 [Maven Failsafe plugin](http://maven.apache.org/surefire/maven-failsafe-plugin/) 参数。 默认情况下，Maven surefire 插件在 super POM 中声明，但您可以在项目的 POM 中调整其设置。

Procedure: 运行测试

1. 打开   Maven   工具窗口。

2. 在   生命周期  节点下，选择   test  。

![Maven 工具窗口](https://resources.jetbrains.com/help/img/idea/2026.3/maven_lifecycle_test_goal.png) 请注意，在此阶段中指定在 [Maven surefire plugin](http://maven.apache.org/surefire/maven-surefire-plugin/) 中的目标将被激活，并且项目或模块中的所有测试都将运行。

Procedure: 运行单个测试

如果要运行单个测试而不是项目中声明的所有测试，请使用 Maven  `-Dtest=TestName test`  命令为单个测试创建 Maven 运行配置。 运行配置将会保存在   运行配置   节点下。

1. 在   Maven   工具窗口中，   生命周期   节点下，右键点击   test   目标。

2. 在上下文菜单中，选择   修改运行配置  。

3. 在打开的对话框中，指定包含要运行测试的工作目录，并在   命令行  字段中指定一个阶段（自动指定）和  `-Dtest=TestName test` 命令。

![运行/调试配置](https://resources.jetbrains.com/help/img/idea/2026.3/maven_run_config_for_test.png) 点击   OK  。

4. 打开   运行配置   节点，然后双击您的配置以运行。

![已保存的 Run configuration](https://resources.jetbrains.com/help/img/idea/2026.3/maven_saved_config.png) Maven 运行测试并在   运行  工具窗口中显示结果。

Procedure: 跳过测试

您可以跳过运行测试，例如，当您只想编译项目而不希望等待 Maven 完成测试执行时。

> **Tip:**
> 在 IntelliJ IDEA 中  skip tests  操作是  `-DskipTests=true`  Maven 命令的实现。

1. 点击 ![Maven 设置](https://resources.jetbrains.com/help/img/idea/2026.3/app-client.expui.general.settings.svg)图标，在   Maven  工具窗口中打开  `Maven` 设置，并从左侧的选项中选择   运行器  。

> **Tip:**
> 在 [Maven](maven-projects-tool-window.html)工具窗口中，使用 ![切换略过测试模式](https://resources.jetbrains.com/help/img/idea/2026.3/app.expui.run.showIgnored.svg)来切换   跳过测试  模式。

2. 在   运行器  页面上，选择   跳过测试  并点击   OK  。

![Maven 设置 | 运行程序](https://resources.jetbrains.com/help/img/idea/2026.3/maven_settings_skipTests.png) IntelliJ IDEA 会停用   生命周期   节点下的   test   目标。

![Maven 工具窗口](https://resources.jetbrains.com/help/img/idea/2026.3/maven_tool_win_skipTest.png) 当您执行其他目标时，会在   运行   工具窗口显示一条通知测试被跳过的适当消息。

![运行工具窗口](https://resources.jetbrains.com/help/img/idea/2026.3/maven_run_tool_win_notify.png)

## 使用 Maven 调试测试

您可以调试由 Maven 执行的测试。 例如，您想调试在流水线中运行的测试，那么您可以分叉进程并使用 Maven 命令远程调试它。

如需了解更多信息，请参阅 [Maven 文档](https://maven.apache.org/surefire/maven-surefire-plugin/examples/debugging.html)和 [远程调试](tutorial-remote-debug.html)过程。

Procedure: 调试测试

1. 在您的 Maven 项目中，打开   运行/调试配置  对话框。

2. 添加一个新的   远程 JVM 调试  配置。

3. 在右侧的选项中，添加名称，如果需要可以更改端口（默认是  `8000` ），选择模块类路径，然后点击   OK  。

![调试配置](https://resources.jetbrains.com/help/img/idea/2026.3/mvn_debug_config.png)

4. 在需要的地方设置 [break points](using-breakpoints.html)。

5. 按 `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)) 两次以打开 [运行任何内容](running-anything.html) 窗口。 请输入 [Maven 命令](https://maven.apache.org/surefire/maven-surefire-plugin/examples/debugging.html#forked-tests) 来分叉测试。 默认使用 5005 端口进行处理。 不过，您可以使用以下命令更改端口并在本地主机上运行：

```
mvn -Dmaven.surefire.debug="-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=localhost:8000" test

```

![本地终端](https://resources.jetbrains.com/help/img/idea/2026.3/local_teminal_mvnDebug.png)

您可以查看可以使用的其他 [Maven commands](https://maven.apache.org/surefire/maven-surefire-plugin/examples/debugging.html#non-forked-tests)。 例如，若您不希望 fork 调试过程时使用什么。

您可以在   运行  工具窗口中检查正在运行的进程。

![运行工具窗口](https://resources.jetbrains.com/help/img/idea/2026.3/run_tool_window_mvn.png)

6. 开始调试过程，方法是点击主工具栏上创建的 ![调试按钮](https://resources.jetbrains.com/help/img/idea/2026.3/app.actions.startDebugger.svg) 调试配置。

在   调试   工具窗口中检查结果。

![调试控制台选项卡](https://resources.jetbrains.com/help/img/idea/2026.3/debug_console.png)

随着代码的执行，它将在您设置的断点处暂停。

