# TestNG

From this article, you will learn how to set up TestNG for your projects, create tests, and run them to see if your code is operating correctly. It contains just the basic steps to get you started.

If you want to know more about TestNG, refer to [the official documentation](https://testng.org). To learn more about testing features of IntelliJ IDEA, refer to other topics in this section.

You can choose to follow the steps on this page using Maven or Gradle.

> **Note:**
> IntelliJ IDEA comes bundled with TestNG 7.1.0

## Add TestNG to your project

Maven:

Procedure:

1. Open `pom.xml` in the root directory of your project.

> **Tip:**
> To quickly navigate to a file, press `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)), `Alt+Shift+O` (NetBeans), `Ctrl+Shift+T` (Visual Studio), `⌘ ⇧ T` (Visual Studio (macOS)), `Ctrl+Shift+R` (Eclipse), `⌘ ⇧ R` (Eclipse (macOS)) and enter its name.

2. In `pom.xml`, press `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` (NetBeans), `Alt+Insert` (Visual Studio), `⌘ ⌃ N` (Visual Studio (macOS)), `Alt+Insert` (Eclipse), `⌘ N` (Eclipse (macOS)) and select Dependency.

3. In the dialog that opens, type `testNG` in the search field.

Locate the `org.testng:testng` dependency, select its version in the search results, and click Add.

4. When the dependency is added to `pom.xml`, press `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` (NetBeans), `Ctrl+Shift+O` (Visual Studio), `Ctrl+Shift+O` (Visual Studio (macOS)), `Ctrl+Shift+O` (Eclipse), `Ctrl+Shift+O` (Eclipse (macOS)) or click ![Reimport All Maven Projects](https://resources.jetbrains.com.cn/help/img/idea/2026.2/app-client.expui.general.refresh.svg) in the Maven tool window to import the changes.

> **Tip:**
> For more information about working with Maven projects, refer to [Maven](maven-support.html).

Gradle:

Procedure:

1. Open `build.gradle` in the root directory of your project.

> **Tip:**
> To quickly navigate to a file, press `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)), `Alt+Shift+O` (NetBeans), `Ctrl+Shift+T` (Visual Studio), `⌘ ⇧ T` (Visual Studio (macOS)), `Ctrl+Shift+R` (Eclipse), `⌘ ⇧ R` (Eclipse (macOS)) and enter its name.

2. In `build.gradle`, press `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` (NetBeans), `Alt+Insert` (Visual Studio), `⌘ ⌃ N` (Visual Studio (macOS)), `Alt+Insert` (Eclipse), `⌘ N` (Eclipse (macOS)) and select Add Maven artifact dependency.

3. In the dialog that opens, type `testNG` in the search field.

Locate the `org.testng:testng` dependency, select its version in the search results, and click Add.

4. When the dependency is added to `build.gradle`, press `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` (NetBeans), `Ctrl+Shift+O` (Visual Studio), `Ctrl+Shift+O` (Visual Studio (macOS)), `Ctrl+Shift+O` (Eclipse), `Ctrl+Shift+O` (Eclipse (macOS)) or click ![Reimport All Gradle Projects](https://resources.jetbrains.com.cn/help/img/idea/2026.2/app-client.expui.general.refresh.svg) in the Gradle tool window to import the changes.

> **Tip:**
> For more information about working with Gradle projects, refer to [Gradle](gradle.html).

IntelliJ build tool:

Procedure:

1. In the main menu, go to `File | Project Structure` (`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` (NetBeans), `Ctrl+Alt+Shift+S` (Visual Studio), `⌘ ;` (Visual Studio (macOS)), `Ctrl+Alt+Shift+S` (Eclipse), `⌘ ;` (Eclipse (macOS))).

2. Under Project Settings, select Libraries and click ![the New Project Library button](https://resources.jetbrains.com.cn/help/img/idea/2026.2/app.expui.general.add.svg) `| From Maven`.

3. In the dialog that opens, specify the necessary library artifact, for example:  `org.testng:testng:6.14.3`.

4. Apply the changes and close the dialog.

> **Tip:**
> The procedure above shows the manual way of adding a dependency. If you just start writing tests, IntelliJ IDEA [will automatically detect
> if the dependency is missing and prompt you to add it](testing.html#add-testing-libraries).

## Create tests

> **Tip:**
> Instead of creating test classes manually, you can use a [dedicated intention action](create-tests.html#create-test) that can create new classes and fill them up with test methods for you.

Procedure: Create a new TestNG class

1. In the   Project tool window  (`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+1` (NetBeans), `Ctrl+Alt+L` (Visual Studio), `⌘ ⌥ L` (Visual Studio (macOS)), `Alt+1` (Eclipse), `Alt+1` (Eclipse (macOS))) , right-click the package inside the [Test Sources Root](content-roots.html#folder-categories) ![Test Sources Root](https://resources.jetbrains.com.cn/help/img/idea/2026.2/app.modules.testRoot.svg) in which you want to create a new test class.

2. Select `New | Java Class` from the context menu.

3. In the Create New Class dialog, name the new class and click OK.

4. In the editor, write the code for your test class. Use the TestNG annotations where necessary. For example, you may want to annotate the whole class or individual 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");
    }
}
```

## Run tests

Procedure: Run TestNG tests

* To run an individual test, click ![Run](https://resources.jetbrains.com.cn/help/img/idea/2026.2/app.runConfigurations.testState.run.svg) in the gutter and select Run.

* To run all tests in a test class, click ![Run](https://resources.jetbrains.com.cn/help/img/idea/2026.2/app.runConfigurations.testState.run.svg) against the test class declaration and select Run.

You can view test results in the Run tool window.

![Viewing TestNG test results in Run tool window](https://resources.jetbrains.com.cn/help/img/idea/2026.2/testng_results_in_run_tool_window.png)

> **Tip:**
> You can also run your tests with Maven (refer to [Testing in Maven](work-with-tests-in-maven.html)) or with Gradle (refer to [Testing in Gradle](work-with-tests-in-gradle.html)).

Procedure: Run tests and generate reports

In IntelliJ IDEA, you can add VM options, use another JDK, or enable code coverage using run configurations. You can create multiple configurations for the same test class or test suite with different settings and compare the results.

For TestNG, you can configure the [listener](https://testng.org/testng_listeners.html) that will generate reports for you. Creating a separate run configuration might be helpful in case you don't want to generate reports every time you run your tests.

1. In the main menu, go to `Run | Edit Configurations`.

2. If there are no previously created TestNG configurations, click ![Add New Configuration](https://resources.jetbrains.com.cn/help/img/idea/2026.2/app.expui.general.add.svg) and from the list that opens, select TestNG.

Otherwise, select the required exising configuration from the list.

3. If it is a new configuration, give it a name.

4. From the Test kind list, select the scope of tests that you want to run. The next field will change accordingly allowing you to select specific tests to run.

5. In the Output directory field, you can specify a folder in which the IDE will place the report.

By default, the IDE creates a new `test-output` folder in the project root directory, but you can use another folder or use different folders for different reports.

6. Switch to the Listeners tab, click ![Add](https://resources.jetbrains.com.cn/help/img/idea/2026.2/app.expui.general.add.svg), and add the `EmailableReporter` option. This will generate an HTML report.

7. Apply the changes and close the dialog.

![Adding a listener in run configuration](https://resources.jetbrains.com.cn/help/img/idea/2026.2/testng-add-reporter-listener.png)

8. On the toolbar, make sure that the newly created run configuration is selected and click ![](https://resources.jetbrains.com.cn/help/img/idea/2026.2/app.runConfigurations.testState.run.svg) next to it. Alternatively, press `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)), `F6` (NetBeans), `Ctrl+F5` (Visual Studio), `⌃ F5` (Visual Studio (macOS)), `Alt+Shift+X` (Eclipse), `⌘ ⇧ F11` (Eclipse (macOS)).

![Running configuration that launches tests and generates reports](https://resources.jetbrains.com.cn/help/img/idea/2026.2/run-testng-with-reports.png)

> **Tip:**
> For a detailed description of each option in TestNG run configurations, refer to [Run/Debug Configuration: TestNG](run-debug-configuration-testng.html).

The IDE creates an `.html` report with results of the test run in the folder that you have specified in the run configuration. If you haven't specified a folder, the `test-output` folder with the report will be created.

To quickly view this file in your browser, open the file in the editor. Then, go to `View | Open in Browser` and select a browser. You can also use the built-in preview to view the file. For more information, refer to [Preview output of HTML files](editing-html-files.html#ws_html_preview_output).

![Viewing TestNG HTML report](https://resources.jetbrains.com.cn/help/img/idea/2026.2/view-testng-html-report.png)

## Test suites

In TestNG, a test suite is a collection of tests that are designed to test a specific functionality or feature of an application. It is a container that holds a group of related test cases. Test suites can be created by grouping multiple test cases or by combining multiple test suites.

TestNG does not allow defining the test suites inside the test code or the main testing source code. That is why a test suite can be defined in an XML or YAML file that contains information about the test cases, the classes or packages that contain the test cases, and the test parameters.

Procedure: Create test suites in XML

You can specify test suites in `.xml` or `[.yaml](#yaml)` files. If you are going to use YAML, make sure to [add the corresponding dependency](#yaml) to the build file.

1. Right-click the project root folder in the   Project tool window  (`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+1` (NetBeans), `Ctrl+Alt+L` (Visual Studio), `⌘ ⌥ L` (Visual Studio (macOS)), `Alt+1` (Eclipse), `Alt+1` (Eclipse (macOS))) , select `New | File` and enter the name of the file, for example `testng.xml`.

2. Fill in the file with information about your tests. For more information, refer to [testng.xml](https://testng.org/#_testng_xml).

Procedure: Use YAML for test suites

TestNG [supports defining a test suite using YAML](https://testng.org/#_yaml), however, it doesn't contain the YAML parser by default. If you want to use a YAML file, add the `snakeyaml` dependency. The version should correspond with the TestNG version that you use.

1. Open the build file (`pom.xml` or `build.gradle`), press `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` (NetBeans), `Alt+Insert` (Visual Studio), `⌘ ⌃ N` (Visual Studio (macOS)), `Alt+Insert` (Eclipse), `⌘ N` (Eclipse (macOS)), and select Add dependency.

2. In the tool window that opens, type `snakeyaml` in the search field.

Locate the necessary dependency, select its version in the search results, and click Add next to it.

3. Press `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` (NetBeans), `Ctrl+Shift+O` (Visual Studio), `Ctrl+Shift+O` (Visual Studio (macOS)), `Ctrl+Shift+O` (Eclipse), `Ctrl+Shift+O` (Eclipse (macOS)) or click the notification that appears in the top-right corner of the editor to load the changes.

4. Right-click the project root folder in the   Project tool window  (`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+1` (NetBeans), `Ctrl+Alt+L` (Visual Studio), `⌘ ⌥ L` (Visual Studio (macOS)), `Alt+1` (Eclipse), `Alt+1` (Eclipse (macOS))) , select `New | File` and enter the name of the file, for example `testing.yaml`.

Procedure: Run a test suite

To be able to run a TestNG test suite, create a run configuration for this suite:

1. In the main menu, go to `Run | Edit Configurations`.

2. In the left-hand pane, click ![Add New Configuration](https://resources.jetbrains.com.cn/help/img/idea/2026.2/app.expui.general.add.svg) and from the list that opens, select TestNG. Name the new configuration.

3. From the Test kind list, select Suite.

The Suite field  becomes available.

4. In the Suite field, click ![Browse](https://resources.jetbrains.com.cn/help/img/idea/2026.2/app.actions.menu-open.svg) and specify the path to the XML or YAML file in which the test suite is configured.

5. In the Output directory field, you can specify a folder in which the IDE will place output files, such as test reports.

By default, the IDE creates a new `test-output` folder in the project root directory, but you can use another folder or use different folders for different outputs.

6. Apply the changes and close the dialog.

![Creating configuration to run TestNG suite](https://resources.jetbrains.com.cn/help/img/idea/2026.2/testng-run-configuration-suite.png)

7. On the toolbar, make sure that the newly created run configuration is selected and click ![Run](https://resources.jetbrains.com.cn/help/img/idea/2026.2/app.runConfigurations.testState.run.svg) next to it. Alternatively, press `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)), `F6` (NetBeans), `Ctrl+F5` (Visual Studio), `⌃ F5` (Visual Studio (macOS)), `Alt+Shift+X` (Eclipse), `⌘ ⇧ F11` (Eclipse (macOS)).

![Running test suite](https://resources.jetbrains.com.cn/help/img/idea/2026.2/run-testng-suite.png)

> **Tip:**
> For a detailed description of each option in TestNG run configurations, refer to [Run/Debug Configuration: TestNG](run-debug-configuration-testng.html).

## See also

### External Links

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

