# TestNG

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

从本文中，您将学习如何为您的项目设置 TestNG，创建测试并运行它们，以查看您的代码是否正常运行。 它只包含使您入门的基本步骤。

如果您想了解更多关于 TestNG 的信息，请参阅 [官方文档](https://testng.org)。 要了解有关 IntelliJ IDEA 测试功能的更多信息，请参阅本节中的其他主题。

您可以选择使用 Maven 或 Gradle 按照此页面上的步骤操作。

> **Note:**
> IntelliJ IDEA 包含 TestNG 7.1.0

## 将 TestNG 添加到您的项目

Maven:

Procedure:

1. 在项目根目录中打开  `pom.xml` 。

> **Tip:**
> 若要快速定位到某个文件，请按 `Ctrl+Shift+N` (Windows), `⌘ ⇧ O` (macOS), `⌘ ⇧ N` (IntelliJ IDEA Classic (macOS)), `⌘ ⇧ O` (macOS System Shortcuts), `Ctrl+Shift+N` (XWin), `Ctrl+Shift+N` (GNOME), `Ctrl+Shift+N` (KDE), `Ctrl+X, Ctrl+F` (Emacs), `Ctrl+P` (Sublime Text), `⌘ P` (Sublime Text (macOS)), `Ctrl+Shift+T` (Visual Studio), `⌘ ⇧ T` (Visual Studio (macOS)), `Alt+Shift+O` (NetBeans), `Ctrl+Shift+R` (Eclipse), `⌘ ⇧ R` (Eclipse (macOS)) 并输入其名称。

2. 在  `pom.xml`  中，按 `Alt+Insert` (Windows), `⌘ N` (macOS), `⌃ N` (IntelliJ IDEA Classic (macOS)), `⌘ N` (macOS System Shortcuts), `Alt+Insert` (XWin), `Alt+Insert` (GNOME), `Alt+Insert` (KDE), `Alt+Insert` (Emacs), `Alt+Insert` (Sublime Text), `⌘ N` (Sublime Text (macOS)), `Alt+Insert` (Visual Studio), `⌘ ⌃ N` (Visual Studio (macOS)), `Alt+Insert` (NetBeans), `Alt+Insert` (Eclipse), `⌘ N` (Eclipse (macOS)) 并选择   依赖  。

3. 在打开的对话框中，在搜索字段中输入  `testNG` 。

找到  `org.testng:testng`  依赖项，在搜索结果中选择其版本，然后点击   添加  。

4. 将依赖项添加到  `pom.xml`  后，按 `Ctrl+Shift+O` (Windows), `Ctrl+Shift+O` (macOS), `Ctrl+Shift+O` (IntelliJ IDEA Classic (macOS)), `Ctrl+Shift+O` (macOS System Shortcuts), `Ctrl+Shift+O` (XWin), `Ctrl+Shift+O` (GNOME), `Ctrl+Shift+O` (KDE), `Ctrl+Shift+O` (Emacs), `Ctrl+Shift+O` (Sublime Text), `Ctrl+Shift+O` (Sublime Text (macOS)), `Ctrl+Shift+O` (Visual Studio), `Ctrl+Shift+O` (Visual Studio (macOS)), `Ctrl+Shift+O` (NetBeans), `Ctrl+Shift+O` (Eclipse), `Ctrl+Shift+O` (Eclipse (macOS))，或在   Maven   工具窗口中点击 ![重新导入所有 Maven 项目](https://resources.jetbrains.com.cn/help/img/idea/2026.3/app-client.expui.general.refresh.svg) 以导入更改。

> **Tip:**
> 有关使用 Maven 项目的更多信息，请参阅 [Maven](maven-support.html)。

Gradle:

Procedure:

1. 在项目根目录中打开  `build.gradle` 。

> **Tip:**
> 若要快速定位到某个文件，请按 `Ctrl+Shift+N` (Windows), `⌘ ⇧ O` (macOS), `⌘ ⇧ N` (IntelliJ IDEA Classic (macOS)), `⌘ ⇧ O` (macOS System Shortcuts), `Ctrl+Shift+N` (XWin), `Ctrl+Shift+N` (GNOME), `Ctrl+Shift+N` (KDE), `Ctrl+X, Ctrl+F` (Emacs), `Ctrl+P` (Sublime Text), `⌘ P` (Sublime Text (macOS)), `Ctrl+Shift+T` (Visual Studio), `⌘ ⇧ T` (Visual Studio (macOS)), `Alt+Shift+O` (NetBeans), `Ctrl+Shift+R` (Eclipse), `⌘ ⇧ R` (Eclipse (macOS)) 并输入其名称。

2. 在  `build.gradle`  中，按 `Alt+Insert` (Windows), `⌘ N` (macOS), `⌃ N` (IntelliJ IDEA Classic (macOS)), `⌘ N` (macOS System Shortcuts), `Alt+Insert` (XWin), `Alt+Insert` (GNOME), `Alt+Insert` (KDE), `Alt+Insert` (Emacs), `Alt+Insert` (Sublime Text), `⌘ N` (Sublime Text (macOS)), `Alt+Insert` (Visual Studio), `⌘ ⌃ N` (Visual Studio (macOS)), `Alt+Insert` (NetBeans), `Alt+Insert` (Eclipse), `⌘ N` (Eclipse (macOS)) 并选择   添加 Maven 工件依赖  。

3. 在打开的对话框中，在搜索字段中输入  `testNG` 。

找到  `org.testng:testng`  依赖项，在搜索结果中选择其版本，然后点击   添加  。

4. 将依赖项添加到  `build.gradle`  后，按 `Ctrl+Shift+O` (Windows), `Ctrl+Shift+O` (macOS), `Ctrl+Shift+O` (IntelliJ IDEA Classic (macOS)), `Ctrl+Shift+O` (macOS System Shortcuts), `Ctrl+Shift+O` (XWin), `Ctrl+Shift+O` (GNOME), `Ctrl+Shift+O` (KDE), `Ctrl+Shift+O` (Emacs), `Ctrl+Shift+O` (Sublime Text), `Ctrl+Shift+O` (Sublime Text (macOS)), `Ctrl+Shift+O` (Visual Studio), `Ctrl+Shift+O` (Visual Studio (macOS)), `Ctrl+Shift+O` (NetBeans), `Ctrl+Shift+O` (Eclipse), `Ctrl+Shift+O` (Eclipse (macOS))，或在   Gradle   工具窗口中点击 ![重新导入所有 Gradle 项目](https://resources.jetbrains.com.cn/help/img/idea/2026.3/app-client.expui.general.refresh.svg) 以导入更改。

> **Tip:**
> 有关使用 Gradle 项目的更多信息，请参阅 [Gradle](gradle.html)。

IntelliJ 构建工具:

Procedure:

1. 在主菜单中，转到  `文件 | 项目结构` （ `Ctrl+Alt+Shift+S` (Windows), `⌘ ;` (macOS), `⌘ ;` (IntelliJ IDEA Classic (macOS)), `⌘ ⇧ Comma` (macOS System Shortcuts), `Ctrl+Alt+Shift+S` (XWin), `Ctrl+Alt+Shift+S` (GNOME), `Ctrl+Alt+Shift+S` (KDE), `Ctrl+Alt+Shift+S` (Emacs), `Ctrl+Alt+Shift+S` (Sublime Text), `⌘ ;` (Sublime Text (macOS)), `Ctrl+Alt+Shift+S` (Visual Studio), `⌘ ;` (Visual Studio (macOS)), `Ctrl+Alt+Shift+S` (NetBeans), `Ctrl+Alt+Shift+S` (Eclipse), `⌘ ;` (Eclipse (macOS))）。

2. 在   项目设置   下，选择   库   并点击 ![新建项目库按钮](https://resources.jetbrains.com.cn/help/img/idea/2026.3/app.expui.general.add.svg)  `| 来源于 Maven` 。

3. 在打开的对话框中，指定必要的库工件，例如：  `org.testng:testng:6.14.3` 。

4. 应用更改并关闭对话框。

> **Tip:**
> 以上过程展示了手动添加依赖项的方法。 如果您刚开始编写测试，IntelliJ IDEA [会自动检测是否缺少依赖并提示您添加它](testing.html#add-testing-libraries)。

## 创建测试

> **Tip:**
> 相比手动创建测试类，您可以使用 [专用意图操作](create-tests.html#create-test)，它可以为您创建新类并填充测试方法。

Procedure: 创建一个新的 TestNG 类

1. 在   项目   工具窗口 ( `Alt+1` (Windows), `⌘ 1` (macOS), `⌘ 1` (IntelliJ IDEA Classic (macOS)), `⌘ 1` (macOS System Shortcuts), `Alt+1` (XWin), `Alt+1` (GNOME), `Alt+1` (KDE), `Alt+1` (Emacs), `Alt+1` (Sublime Text), `⌘ 1` (Sublime Text (macOS)), `Ctrl+Alt+L` (Visual Studio), `⌘ ⌥ L` (Visual Studio (macOS)), `Ctrl+1` (NetBeans), `Alt+1` (Eclipse), `Alt+1` (Eclipse (macOS))) 中，右键点击 [Test Sources Root](content-roots.html#folder-categories) ![测试源根目录](https://resources.jetbrains.com.cn/help/img/idea/2026.3/app.modules.testRoot.svg) 内要创建新测试类的软件包。

2. 从上下文菜单中选择  `新建 | Java 类` 。

3. 在   创建新类  对话框中，命名新类并点击   OK  。

4. 在编辑器中编写您的测试类代码。 在必要时使用 TestNG 注释。 例如，您可能想要注释整个 class 或单个 methods：

```JAVA
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

@Test()
public class SampleTest {
    @DataProvider
    public Object[][] data() {
    return new String[][] {new String[] {"data1"}, new String[] {"data2"}};
}

@Test(dataProvider = "data")
    public void test(String d) {
    Assert.assertEquals("First Line\nSecond Line", "First Line\nSecond Line");
    }
}

```

## 运行测试

Procedure: 运行 TestNG 测试

* 要运行单个测试，请在装订线中点击 ![运行](https://resources.jetbrains.com.cn/help/img/idea/2026.3/app.runConfigurations.testState.run.svg) 并选择   运行  。

* 要运行测试类中的所有测试，请在测试类声明处点击 ![运行](https://resources.jetbrains.com.cn/help/img/idea/2026.3/app.runConfigurations.testState.run.svg) 并选择   运行  。

您可以在   运行   工具窗口中查看测试结果。

![在运行工具窗口中查看 TestNG 测试结果](https://resources.jetbrains.com.cn/help/img/idea/2026.3/testng_results_in_run_tool_window.png)

> **Tip:**
> 您还可以使用 Maven（请参阅 [Maven 测试](work-with-tests-in-maven.html)）或 Gradle（请参阅 [在 Gradle 中测试](work-with-tests-in-gradle.html)）运行您的测试。

Procedure: 运行测试并生成报告

在 IntelliJ IDEA 中，您可以添加 VM 选项，使用其他 JDK 或通过运行配置启用代码覆盖。 您可以为同一个测试类或测试套件创建多个不同设置的配置，并比较结果。

对于 TestNG，您可以配置 [listener](https://testng.org/testng_listeners.html)，它将为您生成报告。 如果您不想每次运行测试时都生成报告，创建一个单独的 run configuration 可能会有所帮助。

1. 在主菜单中，前往  `运行 | 编辑配置` 。

2. 如果之前没有创建过 TestNG 配置，请点击 ![添加新配置](https://resources.jetbrains.com.cn/help/img/idea/2026.3/app.expui.general.add.svg)，并从打开的列表中选择   TestNG  。

否则，请从列表中选择所需的现有配置。

3. 如果这是一个新配置，请为其命名。

4. 从   测试种类   列表中，选择您想要运行的测试范围。 下一个字段将相应更改，允许您选择特定的测试来运行。

5. 在   输出目录  字段中，您可以指定一个文件夹，IDE 将在其中放置报告。

默认情况下，IDE 会在项目根目录中创建一个新的  `test-output`  文件夹，但您可以使用其他文件夹或为不同的报告使用不同的文件夹。

6. 切换到   监听器   选项卡，点击 ![添加](https://resources.jetbrains.com.cn/help/img/idea/2026.3/app.expui.general.add.svg)，并添加  `EmailableReporter`  选项。 这将生成一份 HTML 报告。

7. 应用更改并关闭对话框。

![在运行配置中添加监听器](https://resources.jetbrains.com.cn/help/img/idea/2026.3/testng-add-reporter-listener.png)

8. 在工具栏上，确保已选择新创建的运行配置，并点击其旁边的 ![](https://resources.jetbrains.com.cn/help/img/idea/2026.3/app.runConfigurations.testState.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.cn/help/img/idea/2026.3/run-testng-with-reports.png)

> **Tip:**
> 有关 TestNG 运行配置中各选项的详细说明，请参阅 [运行/调试配置：TestNG](run-debug-configuration-testng.html)。

IDE 会在您在运行配置中指定的文件夹中创建一个包含测试运行结果的  `.html` 报告。 如果您尚未指定文件夹，将会创建一个带有报告的  `test-output` 文件夹。

要快速在浏览器中查看此文件，请在编辑器中打开该文件。 然后，前往  `查看 | 在浏览器中打开` 并选择一个浏览器。 您还可以使用内置预览来查看文件。 更多信息请参阅 [预览 HTML 文件的输出](editing-html-files.html#ws_html_preview_output)。

![查看 TestNG HTML 报告](https://resources.jetbrains.com.cn/help/img/idea/2026.3/view-testng-html-report.png)

## 测试套件

在 TestNG 中，测试套件是一组旨在测试应用程序某个特定功能或特性的测试。 这是一个包含一组相关测试用例的容器。 可以通过将多个测试用例分组或组合多个测试套件来创建测试套件。

TestNG 不允许在测试代码或主要测试源代码中定义测试套件。 这就是为什么可以在包含测试用例信息、包含测试用例的类或包以及测试参数的 XML 或 YAML 文件中定义测试套件。

Procedure: 创建 XML 测试套件

您可以在  `.xml`  或  `[.yaml](#yaml)`  文件中指定测试套件。 如果您要使用 YAML，请确保在构建文件中 [添加相应的依赖项](#yaml)。

1. 右键点击   项目   工具窗口 ( `Alt+1` (Windows), `⌘ 1` (macOS), `⌘ 1` (IntelliJ IDEA Classic (macOS)), `⌘ 1` (macOS System Shortcuts), `Alt+1` (XWin), `Alt+1` (GNOME), `Alt+1` (KDE), `Alt+1` (Emacs), `Alt+1` (Sublime Text), `⌘ 1` (Sublime Text (macOS)), `Ctrl+Alt+L` (Visual Studio), `⌘ ⌥ L` (Visual Studio (macOS)), `Ctrl+1` (NetBeans), `Alt+1` (Eclipse), `Alt+1` (Eclipse (macOS))) 中的项目根文件夹，选择  `新建 | 文件`  并输入文件名，例如  `testng.xml` 。

2. 请在文件中填写有关您测试的信息。 如需更多信息，请参阅 [testng.xml](https://testng.org/#_testng_xml)。

Procedure: 使用 YAML 进行测试套件

TestNG [支持使用 YAML 定义测试套件](https://testng.org/#_yaml)，然而，它默认不包含 YAML 解析器。 如果您想使用 YAML 文件，请添加  `snakeyaml` 依赖项。 这个版本应与您使用的 TestNG 版本相对应。

1. 打开构建文件（  `pom.xml`  或  `build.gradle` ），按 `Alt+Insert` (Windows), `⌘ N` (macOS), `⌃ N` (IntelliJ IDEA Classic (macOS)), `⌘ N` (macOS System Shortcuts), `Alt+Insert` (XWin), `Alt+Insert` (GNOME), `Alt+Insert` (KDE), `Alt+Insert` (Emacs), `Alt+Insert` (Sublime Text), `⌘ N` (Sublime Text (macOS)), `Alt+Insert` (Visual Studio), `⌘ ⌃ N` (Visual Studio (macOS)), `Alt+Insert` (NetBeans), `Alt+Insert` (Eclipse), `⌘ N` (Eclipse (macOS))，然后选择   添加依赖  。

2. 在打开的工具窗口中，在搜索字段中输入  `snakeyaml` 。

在搜索结果中找到必要的依赖项，选择其版本，并点击其旁边的   添加  。

3. 按 `Ctrl+Shift+O` (Windows), `Ctrl+Shift+O` (macOS), `Ctrl+Shift+O` (IntelliJ IDEA Classic (macOS)), `Ctrl+Shift+O` (macOS System Shortcuts), `Ctrl+Shift+O` (XWin), `Ctrl+Shift+O` (GNOME), `Ctrl+Shift+O` (KDE), `Ctrl+Shift+O` (Emacs), `Ctrl+Shift+O` (Sublime Text), `Ctrl+Shift+O` (Sublime Text (macOS)), `Ctrl+Shift+O` (Visual Studio), `Ctrl+Shift+O` (Visual Studio (macOS)), `Ctrl+Shift+O` (NetBeans), `Ctrl+Shift+O` (Eclipse), `Ctrl+Shift+O` (Eclipse (macOS)) 或点击出现在编辑器右上角的通知，以加载更改。

4. 右键点击   项目   工具窗口 ( `Alt+1` (Windows), `⌘ 1` (macOS), `⌘ 1` (IntelliJ IDEA Classic (macOS)), `⌘ 1` (macOS System Shortcuts), `Alt+1` (XWin), `Alt+1` (GNOME), `Alt+1` (KDE), `Alt+1` (Emacs), `Alt+1` (Sublime Text), `⌘ 1` (Sublime Text (macOS)), `Ctrl+Alt+L` (Visual Studio), `⌘ ⌥ L` (Visual Studio (macOS)), `Ctrl+1` (NetBeans), `Alt+1` (Eclipse), `Alt+1` (Eclipse (macOS))) 中的项目根文件夹，选择  `新建 | 文件`  并输入文件名，例如  `testing.yaml` 。

Procedure: 运行测试套件

要运行 TestNG 测试套件，请为此套件创建一个运行配置：

1. 在主菜单中，前往  `运行 | 编辑配置` 。

2. 在左侧窗格中，点击 ![添加新配置](https://resources.jetbrains.com.cn/help/img/idea/2026.3/app.expui.general.add.svg)，然后从打开的列表中选择   TestNG  。 请命名新配置。

3. 从   测试种类   列表中选择   套件  。

套件   字段变得可用。

4. 在   套件  字段中，点击 ![浏览](https://resources.jetbrains.com.cn/help/img/idea/2026.3/app.actions.menu-open.svg)并指定配置测试套件的 XML 或 YAML 文件的路径。

5. 在   输出目录  字段中，您可以指定一个文件夹，IDE 将在其中放置输出文件，例如测试报告。

默认情况下，IDE会在项目根目录中创建一个新的  `test-output` 文件夹，但您可以使用其他文件夹或为不同的输出使用不同的文件夹。

6. 应用更改并关闭对话框。

![创建配置以运行 TestNG suite](https://resources.jetbrains.com.cn/help/img/idea/2026.3/testng-run-configuration-suite.png)

7. 在工具栏上，确保已选择新创建的运行配置，并点击其旁边的 ![运行](https://resources.jetbrains.com.cn/help/img/idea/2026.3/app.runConfigurations.testState.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.cn/help/img/idea/2026.3/run-testng-suite.png)

> **Tip:**
> 有关 TestNG 运行配置中各选项的详细说明，请参阅 [运行/调试配置：TestNG](run-debug-configuration-testng.html)。

## See also

### 外部链接

[TestNG documentation](https://testng.org)

