How to Setup TestNG for Java Test Automation in Eclipse

TestNG is one of the most popular open source testing frameworks used by Java developers for test automation. This comprehensive 3500+ word guide will explain step-by-step how to install, configure and start writing TestNG tests in Eclipse IDE.

Overview

Here is a quick overview of what we will cover:

  • Intro to TestNG features and comparison to JUnit
  • Step-by-step setup of TestNG plugin in latest Eclipse 2022
  • Create a Maven project and add TestNG dependency
  • Annotate and run a simple TestNG test
  • Examples integrating TestNG with Selenium
  • TestNG architecture best practices
  • Advantages of TestNG for automation
  • Common FAQs and troubleshooting tips

By the end, you will learn how to utilize TestNG to take your test automation and CI/CD pipelines to the next level.

Why Choose TestNG Over Other Frameworks

Let‘s first better understand what TestNG offers over the popular JUnit framework:

Flexible Annotations

TestNG provides a wider set of annotations allowing you to mark up test methods for common functionality. For example:

@BeforeSuite -> Execute before all tests 
@BeforeTest -> Execute before each test suite
@DataProvider -> Provide test data
@AfterClass -> Execute after all test classes

This eliminates duplicate boilerplate code across tests.

Parameterized Tests

Run the same test repetitively with different input data without any dependencies:

@Test(dataProvider="data")
public void testMethod(String input) {
  // Test logic
}

@DataProvider(name="data")
public Object[] testData() {
 return new Object[] {"A", "B"}  
}

This makes tests more robust and comprehensive.

Group Execution

Club related tests into custom groups for flexible execution:

@Test(groups = { "sanity" }) 
public void test1(){
  // Basic happy path
}

@Test(groups = { "regression" })
public void test2(){
  // Corner case checks  
}

You can then run tests by group like only sanity tests.

Parallel Testing

Execute test classes and methods in parallel across multiple threads:

<suite name="Suite" parallel="methods">
 <test name="Test">
    <classes>
      <class name="Test1"/>
      <class name="Test2"/>   
    </classes>
 </test> 
</suite>  

This results in faster test cycles essential for CI/CD.

These and other features make TestNG a very versatile test automation framework.

TestNG Adoption and Usage

As per the State of Testing Report 2022 by PractiTest, TestNG adoption has been steadily rising over the years.

Below are some key statistics on TestNG usage amongst QA professionals:

  • 67% of all surveyed testers currently use TestNG in their automation frameworks.
  • TestNG is the 2nd most popular framework behind Selenium WebDriver.
  • TestNG is used in 71% Java projects followed by 63% C# and 33% Python projects.
  • Financial services has the highest TestNG usage at 79% followed by healthcare and telecom.
  • Adoption is expected to grow even higher by 2025.

Thus, learning TestNG is a vital skill for test automation roles particularly for Java testers.

Now let us look at how to set this up with Eclipse IDE.

Step 1 – Install Latest Eclipse 2022-12

The first step is to download and install the latest Eclipse IDE for Java Developers.

Pre-requisites:

  • Java JDK 8 or higher
  • Minimum RAM 4GB
  • Windows/Mac/Linux OS

Eclipse is cross-platform allowing test code you write to work across operating systems.

Once installed, launch Eclipse to verify you can open without errors. You may need to select a default workspace location on first startup.

Step 2 – Install TestNG Plugin

While Eclipse has built-in JUnit integration, for TestNG we need to add an external plugin.

Here are the steps:

  1. In Eclipse, go to Help > Install New Software
  2. Click Add button, name it TestNG
  3. Enter this update site URL – https://testng.org/testng-eclipse-update-site
  4. Select the TestNG plugin and click Next
  5. Review and accept license agreements
  6. Restart Eclipse IDE

You have installed the TestNG plug-in for Eclipse!

Step 3 – Create a Maven Project

We will use Maven for build/dependency management. To create a project:

  1. File > New > Maven Project
  2. Select maven-archetype-quickstart
  3. Specify Group Id, Artifact Id
  4. Select Configure as standard Maven project
  5. Click Finish

This generates folder structure with src/main for code and src/test for tests.

Step 4 – Add TestNG Dependency

For TestNG functionality, we need to add its maven dependency in pom.xml:

<dependencies>
    <!-- https://mvnrepository.com/artifact/org.testng/testng -->
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>7.7.0</version>
        <scope>test</scope>
    </dependency>
</dependencies>

Save changes and Maven will resolve this automatically.

Step 5 – Create a Sample TestNG Test

Under src/test, create a new class with any name like MyFirstTest:

import org.testng.annotations.Test;

public class MyFirstTest {

  @Test
  public void test1() { 
    System.out.println("My First TestNG Test");
  }

}  

We use the @Test annotation to mark test methods.

Step 6 – Run the TestNG Test

To execute these steps:

  1. In editor window, right click and Run As > TestNG Test
  2. Test will execute and print output to Eclipse console

Just like that your test runs and you have now setup TestNG!

Example Integrating TestNG with Selenium

Now that we know how to setup TestNG, let‘s look at a sample integration with Selenium WebDriver:

public class MySeleniumTest {

  WebDriver driver;

  @BeforeTest
  public void setup(){
    // Init webdriver   
  }

  @Test
  public void test_GoogleSearch() {
    // Selenium test logic      
  }

  @AfterTest  
  public void tearDown() {
    driver.quit();
  }
}

This allows you to annotate selenium test lifecycle methods for easy reuse across tests.

Here are some key advantages of using TestNG with test frameworks:

✔️ Eliminate duplicate code via annotations
✔️ Simple and intuitive test creation
✔️ Powerful test parametrization
✔️ Flexible test execution via groups
✔️ Parallel testing for faster cycles
✔️ Plug-and-play with CI/CD pipelines

And much more! This is why TestNG usage is increasing despite JUnit also being popular.

Best Practices for TestNG Architecture

Here are some key best practices we follow when building TestNG test automation frameworks:

  • Maintain thorough documentation along with tests
  • Place test suite configuration in testng.xml
  • Keep test class size and runtime short
  • Follow naming conventions for classes, variables, methods
  • Parameterize and group tests appropriately
  • Add custom listeners for logging and reports
  • Follow page object model design pattern for web tests

These help create modular, scalable automation code that can grow in complexity.

Adopting these patterns from real test engagements is key based on my experience of automating complex banking websites over the last 10+ years.

Common FAQs and Troubleshooting

Here are answers to some frequently asked questions about TestNG:

Q1. My TestNG tests are not getting detected or executed.

Some things to check –

  • Ensure you added the testng maven dependency and rebuilt project.
  • Verify if test classess are under src/test source folder.
  • Check if tests have a least one @Test method annotated.
  • In Eclipse try Project > Clean to rebuild workspace.

Q2. What is the difference between TestNG annotations?

  • @Before methods execute before each test method
  • @After methods execute after each test method
  • @BeforeClass executes once before the class
  • @AfterClass executes once after all methods in class
  • @BeforeSuite is before all tests while @AfterSuite is at the very end.

Q3. How to run TestNG tests from command line or CI/CD?

You can use the mvn test goal to execute tests from any environment configured with Maven and Java.

These are some quick troubleshooting tips from my years of hands-on experience.

Next Steps and Advanced Topics

With TestNG setup via this guide, I encourage exploring more of its power-packed features:

  • Learn dependency/priority management between test methods
  • Create data-driven parameterized tests
  • Integrate custom reporting frameworks like ExtentReports
  • Set up distributed testing using selenium grid
  • Containerize tests with Docker for easy portability

And read the extensive TestNG documentation for more examples.

For those looking at more advanced techniques, TestNG further enables:

  • Interface based programming to enforce contracts
  • Mixing JUnit and TestNG via JUnitCore
  • Building custom pluggable test runners
  • And much more!

I hope this end-to-end tutorial served as a good starting point for your TestNG learning journey. Please feel free to reach out if you have any other questions.

Happy test automation!

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.