Open In App

How to run single test in a TestNG class from IntelliJ IDE?

Last Updated : 07 Aug, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

TestNG is a widely used testing framework in the Java programming language. Often, when developing or debugging your code, you may need to run a specific test method rather than executing the entire test suite. Fortunately, IntelliJ IDEA makes this task straightforward with its integrated support for TestNG.

To run a single test in a TestNG class from IntelliJ IDEA with a Maven setup, follow these steps:

Step 1: Create a project in IntelliJ IDE

Start by opening IntelliJ IDEA and creating a new project. Give your project a name and make sure to include the necessary tools or plugins that will help you generate test reports later on.

Project-creation
Project creation

Step 2: Choose Maven or Gradle as the build system to generate the report.

Select Maven or Gradle as your build system. These tools will manage your project dependencies and help generate reports. If you choose Maven, IntelliJ IDEA will automatically create a `pom.xml` file for you to handle configurations. You must add the testNG dependency in pom.xml.

XML
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.geeks</groupId>
  <artifactId>MavenTestNG</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>MavenTestNG</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.testng</groupId>
      <artifactId>testng</artifactId>
      <version>7.7.0</version> <!-- Use the latest version -->
      <scope>test</scope>
    </dependency>

  </dependencies>
</project>

Step 3: Create TestNG class for the sample testcase.

Java
package TestNGTestCases;

import org.testng.Assert;
import org.testng.annotations.Test;

public class LoginTest {

    @Test
    public void testLoginWithValidCredentials() {
        System.out.println("Running test: testLoginWithValidCredentials");
        Assert.assertTrue(true, "Login should succeed with valid credentials.");
    }

    @Test
    public void testLoginWithInvalidCredentials() {
        System.out.println("Running test: testLoginWithInvalidCredentials");
        Assert.assertFalse(false, "Login should fail with invalid credentials.");
    }
}

Step 4: Right-click on the specific test method you want to run.

Output:

output-of-TestNG-with-Intellij
Output of TestNG with Intellij

By following these steps, you can easily run a single test in a TestNG class with Maven setup in IntelliJ IDEA.


Article Tags :

Similar Reads