Open In App

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

Last Updated : 10 Sep, 2024
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. In this article, we'll guide you step by step on how to run a single test method within a TestNG class using IntelliJ IDEA.

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

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.

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
<?xml version="1.0" encoding="UTF-8"?>
<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>org.example</groupId>
    <artifactId>singletest</artifactId>
    <version>1.0-SNAPSHOT</version>

Step 3.Create an XML file in the test report directory for the test method

In your project directory, create a TestNG XML file (e.g., testng.xml). This file allows you to configure how your tests are executed, letting you specify which classes, methods, or groups of tests to run. However, this XML file is optional if you prefer to run individual tests directly from IntelliJ IDEA, as the IDE can handle test execution without requiring an XML configuration.

XML
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
    <test name="Test">
        <classes>
            <class name="XYZTest"/> <!-- Adjust the package name as needed -->
        </classes>
    </test>
</suite>

Step 4. Creating a class

Create a test class where you will write your test methods. This class will have separate test methods that you can run directly from IntelliJ IDEA.

Java
import org.testng.annotations.Test;

public class XYZTest {

    @Test
    public void testAddition() {
        // Test logic for addition
        System.out.println("Addition test executed.");
    }

    @Test
    public void testSubtraction() {
        // Test logic for subtraction
        System.out.println("Subtraction test executed.");
    }
}
  • This test class has two methods: `testAddition()` and `testSubtraction()`.
  • You can run these methods one by one by right-clicking on the method in IntelliJ IDEA and selecting "Run."

5. Choose the Test Method to Run

To run a single test method (e.g., testAddition()), you right-click directly on the method name in the code editor and select “Run 'testAddition()'” from the context menu. IntelliJ will automatically handle the execution using its internal TestNG runner.

  • In this case, you want to run the `testAddition()` method.

Output

file
testAddition() Run
  • In this case, you want to run the `testSubtraction()` method.

Output:

Screenshot-2024-08-31-174521
testSubtraction() Run

Conclusion

Running a single test method in TestNG using IntelliJ IDEA enables you to focus on specific parts of your code without executing the entire test suite. This targeted approach allows you to quickly identify and resolve issues within individual methods, making debugging and development more efficient. Ultimately, it saves time and improves the overall quality of your code.


Next Article
Article Tags :

Similar Reads