The TestNG Eclipse plug-in allows that we run TestNG tests from inside Eclipse in a convenient way. once we code the first draft test case on hand, we don’t need to wait or waste any time, just simply run them from inside Eclipse and easily monitor their execution and output. This tutorial will demonstrate how to install TestNG eclipse plugin, write and run test case with this plugin.

Prerequisites

Eclipse and JDK 5 or higher.

How to install testNG eclipse plugin?

1. Open eclipse, click Help –> Install New Software, in the ‘select a site’ window, click add to open ‘add repository’ dialog.

The TestNG team maintains an latest update in Eclipse update site. we suggest using the following update site address in textbox location to get latest release:

  • For Eclipse 3.4 and higher than, enter http://beust.com/eclipse.
  • For Eclipse 3.3 and below, enter http://beust.com/eclipse1.

2. Once enter name and location, click OK,  then in the ‘Name’ column, select ‘TestNG’ and click ‘Next’ button until appearing the following screenshot.

3. Choose the radio button “I accept the terms of the license agreement” and click finish, it will lead to complete the installation automatically, during this process, if it have security warning, it says that you are installing software that contains unsigned content, please click OK.

4. Click Yes to restart Eclipse for the changes to take effect.

How to Write a TestNG case in eclipse?

1. Create a new project in Eclipse, and add a test class SimpleTestCase.

import java.util.ArrayList;
import java.util.List;

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

public class SimpleTestCase {
	private List list;

	@Test
	public void testEmptyCollection() {
		list = new ArrayList();

		Assert.assertTrue(list.isEmpty());
		System.out.println("list is empty");
	}
}

Currently, the TestNG library has not yet added to the project, so it has red error icons, click them to choose Add TestNG library.

Once done, right-click Run as -> TestNG to run this test case, you will be able to see:

This was the console output:

[TestNG] Running:
C:\Users\asjava\AppData\Local\Temp\testng-eclipse–494421159\testng-customsuite.xmllist is empty
PASSED: testEmptyCollection===============================================
Default test
Tests run: 1, Failures: 0, Skips: 0
==============================================================================================
Default suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================[TestNG] Time taken by org.testng.reporters.XMLReporter@13ded59: 379 ms
[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@d98b00: 730 ms
[TestNG] Time taken by org.testng.reporters.EmailableReporter2@aed3a5: 157 ms
[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 0 ms
[TestNG] Time taken by org.testng.reporters.jq.Main@faa824: 161 ms
[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@4be8b8: 77 ms