To Be Efficient with TestNG Eclipse Plugin

TestNG is a powerful test framework that enables developers to write flexible tests with groupingsequencing, and data-driven features; it even makes writing parallel tests easy. And the most powerful feature, in my view, is a set of Listeners that hooks into TestNG, which allows to better control the test cases, or augment the reporting, etc.

The TestNG Eclipse plugin gives you the ability to run and debug TestNG test cases within Eclipse IDE.

This article shares some tips about how to be efficient when authoring and running tests with TestNG Eclipse Plugin.

Installation

TestNG for Eclipse Plugin is available on Eclipse Marketplace, please follow the installation guide.

Use TestNG Eclipse Plugin

Authoring Tests

When you're authoring the TestNG test cases, the predefined templates can definitely help you be more efficient. To create a test method, in the editor, type test, then hit the Content Assist hot-key ('Alt+/' in my case), select the template as below:

 

Note: There will be more templates available in TestNG Eclipse Plugin 6.10.1, e.g. 'setup', 'teardown', etc.

Run/Debug Tests

Lauching Tests

There are several ways to launch tests, which is very handy. Here are the different ways it can be done:
  • Right clicking on the test class or the source editor, to run that test.
  • Right clicking on the package, to run all the tests under the package.
  • Right clicking on the TestNG suite xml file, to run the custom test suite.
  • Or, go to Run Configuration for better control on the launch configuration, for example, the test group to run; pass system properties to the runtime test process, etc.

Re-run Failed Tests

If you have multiple failed tests, after you've fix your test code, you can rerun the failed tests altogether to save time:

 

Maven Integration

If your project is Maven managed, you might want to configure maven-surefire-plugin or maven-failsafe-plugin to run the test case. Let's say you pass the following JVM arguments and system properties to the runtime test process:

<artifactId>maven-surefire-plugin</artifactId>
<configuration>
    <suiteXmlFiles>
    <suiteXmlFile>test-suite/testng.xml</suiteXmlFile>
    </suiteXmlFiles>
    <argLine>-javaagent:${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar
                -Xmx1024m -XX:MaxPermSize=512m -Xms256m -Xmx1024m -XX:PermSize=128m 
                -Dversion=${project.version}
    </argLine>
    <systemPropertyVariables>
    <foo>${foo.value}</foo>
    </systemPropertyVariables>
    <environmentVariables>
    <bar>${bar.value}</bar>
    </environmentVariables>
</configuration>

The tests run well with Maven cli, mvn -e test, but could fail with any TestNG Eclipse Plugin version prior to 6.9.10. In that case, you would have to manually copy the system properties to Launch Configuration for each test.

Fortunately, today, the TestNG Eclipse Plugin can parse the configuration of maven-surefire/failsafe-plugin, auto append them to the Launch Configuration. This way, it also makes the behavior as close as possible to running on the command line. For more details, please refer to the official guide.

Note: The equivalent feature for Gradle integration with Eclipse Buildship is under planning, you can track the ticket here.

TestNG Eclipse Plugin Internal

It's helpful to understand the core of the TestNG Eclipse plugin, especially when you're having trouble on using it.

When a test running, the plugin needs to get the test results from runtime test process and display them on the view at real time.

The communication between plugin and the runtime test process is made via socket. The plugin first start the socket server. While it's on the runtime test process there are a set of Listeners that hook into TestNG runtime and send the test results back to plugin via socket.

The socket communication functionalities are maintained in the project 'testng-remote' on GitHub.

Final Words

We keep improving the experience of the TestNG Eclipse Plugin, please send your feedback and ideas on the GitHub project here.

About the Author