Open In App

How to run TestNG from IntelliJ IDEA?

Last Updated : 17 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Running TestNG from IntelliJ IDEA is a straightforward process that allows developers to test their Java applications efficiently. IntelliJ IDEA, one of the most popular IDEs, provides seamless integration with TestNG for running unit tests, integration tests, and more. Whether working on small or large-scale projects, configuring TestNG in IntelliJ IDEA enhances your development workflow by simplifying test execution and result analysis.

Running the TestNG tests in IntelliJ IDEA is not overly difficult to do whether or not it is a small project or big enterprise application. Here's how you set up and run your tests.

Steps to Install and Run TestNG in IntelliJ

Step 1: Install the TestNG Plugin

  • Open your IntelliJ Idea: Launch IntelliJ IDEA on your Mac.
Interface-of-InteliJ


  • Go to Plugin Settings: In the top menu, click on IntelliJ IDEA > Preferences (or press ⌘,). In the Preferences window, select Plugins from the left sidebar.
imresizer-1726091628633
Go to Plugin Settings:


  • Search for TestNG Plugin: Open Your Plugins Section and Search in the Search Bar TestNG and You See TestNG install or not in Your Intellij Ide. if not then click to install.
imresizer-1725154054991
Search for TestNG Plugin


Step 2: Configuring Your Project with TestNG Dependency

  • Add TestNG Dependency: in case you’re the use of Maven on your project, you need to add TestNG as a dependency on your pom.xml report. this will can help you write and run TestNG tests.
  • Add the following snippet to your pom.xml:

pom.xml

XML
<dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>7.7.1</version>
    <scope>test</scope>
</dependency>


  • Open Your pom.xml file and add TestNG Dependency on it here you see in the image.
imresizer-1725154497937
add TestNG Dependency


  • After adding the dependency, refresh your Maven project to click the refresh sign in IntelliJ IDEA to ensure the dependency is recognized.
imresizer-1726092299797
refresh your Maven project


Step 3: Create a TestNG Test Class

  • Writing a TestNG Class: Navigate to the src/test/java directory and click on it. pick out New > Java magnificence, and supply your elegance name. inside the magnificence, write check techniques annotated with @test.
  • Create a class and name it MyTest open MyTest class copy the code which given and paste it in MyTest file save it and run through TestNG.

MyTest.java

Java
package com.example.tests;

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

public class MyTest {

    @Test
    public void testAddition() {
        int a = 5;
        int b = 10;
        int result = a + b;
        Assert.assertEquals(result, 15, "Addition result is incorrect!");
    }

    @Test
    public void testSubtraction() {
        int a = 10;
        int b = 5;
        int result = a - b;
        Assert.assertEquals(result, 5, "Subtraction result is incorrect!");
    }
}


Step 4: Run TestNG Tests

  • Running Tests Directly
  • Run from Context Menu: right-click on on on the take a look at elegance or unique test technique. Select Run 'ClassName' or Run 'MyTest'.
  • you have two options to run code option one run test directly which shown images with output.
imresizer-1725156242943
Run TestNG Tests
  • Create a Run Configuration: go to Run > Edit Configurations. Configure the class, technique, or institution of exams you need to run. Click Apply, then Run.
  • Option two is to edit run configuration and make some changes which shown in images and click to apply and run.
imresizer-1725155922971

Output: Once you run your tests, results will appear in the Run tool window, showing passed, failed, or skipped tests, along with any relevant stack traces.

imresizer-1725156052226
Output

Conclusion

In conclusion, running TestNG tests in IntelliJ IDEA is an easy and effective way to ensure your Java applications are tested thoroughly. By following the steps to install and configure TestNG within the IDE, you can quickly set up, run, and analyze your tests. This seamless integration boosts productivity and helps maintain high-quality code through frequent and automated testing.


Next Article

Similar Reads