TestNG Eclipse plug-in

The TestNG Eclipse plug-in allows you to run your TestNG tests from Eclipse and easily monitor their execution and their output. It has its own separate project on code.google.com called testng-eclipse.

Table of Contents

  1 - Installation
  2 - Creating a TestNG class
  3 - Launch configurations
  3.1 - From a class file
  3.2 - From groups
  3.3 - From an XML file
  3.4 - From a method
  3.5 - Specifying listeners and other settings
  4 - Viewing the results
  5 - Converting JUnit tests
  

1 - Installation

Once you have installed the plug-in, restart Eclipse and select the menu Window / Show View / Other...  and you should see the TestNG view listed in the Java category.

2 -Creating a TestNG class

To create a new TestNG class, select the menu File / New / TestNG:

  

Select which annotations you want in this new class along with the class name, package and where the file should be created.

Additionally, you can specify the name of an XML file and the plug-in will generate a suite file which, if run, will invoke the class you just created.

3 - Launch configuration

Once you have created classes that contain TestNG annotations and/or one or more testng.xml files, you can create a TestNG Launch Configuration.  Select the Run / Run... (or Run / Debug...) menu and create a new TestNG configuration:

You should change the name of this configuration and pick a project, which can be selected by clicking on the Browse... button at the top of the window.

Then you choose to launch your TestNG tests in the following ways:

4.2.1 - From a class file

Make sure the box near Class is checked and then pick a class from your project.  You can click on the Browse... button and pick it directly from a list.  This list only contains classes that contain TestNG annotations:

3.2 - From groups

If you only want to launch one or several groups, you can type them in the text field or pick them from a list by clicking on the Browse... button 

3.3 - From a definition file

Finally, you can select a suite definition from your project. It doesn't have to be named testng.xml, the plug-in will automatically identify all the applicable TestNG XML files in your project:

3.4 - From a method

This launch isn't accomplished from the Launch dialog but directly from your Outline view:

You can right-click on any test methods and select Run as... / TestNG test and only the selected method will be run (not shown on the above screenshot because I couldn't find a way to capture a contextual menu).

Method launching is also available from the Package Explorer view and from the Java Browser perspective.

Once you have selected one of these launches, you can also choose the logging of level and also whether TestNG should run in 1.4 or 1.5 mode. Then you can launch the tests by pressing the Debug (or Run) button, which will switch you to the Debug perspective and will open the main TestNG view.

3.5 -Specifying listeners and other settings

As you saw above, the plug-in will let you start tests in many different ways: from an XML file, from a method, a class, etc... When you are running an XML file, you can specify all the settings you want for this run in the XML file, but what if you want to run a package in parallel mode with specific listeners? How can you configure the settings for all the launches that are not done from an XML file?

In order to give you access to the most flexibility, TestNG lets you specify an XML suite file for all these launches, which you can find in the Preferences menu:

If you specify a valid suite file as "XML template file", TestNG will reuse all the settings found in this XML file, such as parallel, name, listeners, thread pool size, etc... Only the <test> tags in this file will be ignored since the plug-in will replace these by a generated <test> tag that represents the launch you chose.

4 - Viewing the test results

The above view shows a successful run of the tests:  the bar is green and no failed tests are reported.  The All tests tab shows you a list of all the classes and methods that were run.

If your test run contains failures, the view will look like this:

You can use the Failed tests tab to display only these tests that failed, and when you select such a test, the stack trace will be shown on the right-hand pane.  You can double click on the offending line to be taken directly to the failure in your code.

5 - Converting JUnit tests

When you are editing a JUnit class, press Ctrl-1 (Quick Fix) and the plug-in will give you the option to convert it to TestNG, either with JDK5 annotations:

or JavaDoc annotations: