TestNG Eclipse Plugin

You have learned how to download and run TestNG with the command line in the article TestNG First Example. But it is so inconvenient. There is an Eclipse plugin that can help you to write and run the TestNG test method in java class easily and quickly. Below is the tasks list which you can learn in this article.

  1. UnInstall and Install TestNG Eclipse plugin.
  2. Create TestNG java project.
  3. Add libraries to the java project.
  4. Write and Run TestNG java class with Eclipse.
  5. Debug test methods.
  6. Generate Html test result reports.
  7. Get testng.xml file content.
  8. Conclusion.

1. UnInstall TestNG Eclipse Plugin.

Because my eclipse has installed the plugin before, so before demonstrate how to install it in Eclipse, I had to uninstall it first. My eclipse version is Neon.3 Release (4.6.3).

  1. Click “Help —> Installation Details” menu item in eclipse top menubar. It will open the Eclipse Installation Details popup window.
  2. Click the tab Installed Software in the Eclipse Installation Details dialog window, and input the search keyword testng in the search text box, then it will list related plugins in the bottom panel.
  3. Select the plugin you want to uninstall ( in this example, I want to uninstall the TestNG M2E Integration plugin ), click the Uninstall button at the bottom.
  4. Wait for some time and click the “Finish” button, there will pop up a dialog that lets you restart eclipse, click the “Yes” button to restart it.
  5. After restart, the eclipse plugin has been removed successfully.

2. Install TestNG Eclipse Plugin.

  1. Click eclipse “Help —> Eclipse Marketplace” menu item at the top menubar.
  2. Input search keyword testng in the Find search box, click Enter key or Go button to search the eclipse plugin in the marketplace.
  3. Click the Install button after the TestNG for Eclipse plugin. Wait for some time, check all checkboxes ( TestNG for Eclipse, TestNG, TestNG M2E Integration ) in the Confirm Selected Features dialog. Click the Confirm button at the bottom.
  4. In the Review Licenses wizard dialog, accept the apache software license and click the Finish button.
  5. Click OK  to continue to install the plugin if you meet the Security Warning dialog which said your installed software is unsigned.
  6. After this plugin installation, click “Yes” to restart Eclipse.
  7. After restart, click eclipse Window —> Preferences menu item.
  8. If you can find TestNG in the eclipse Preferences window left panel that means the plugin has been installed successfully. When you click the TestNG item in the eclipse Preferences window, it will show the TestNG detail settings information ( such as Output directory, Template XML file, etc ) on the right panel.

3. Create Java Project.

After successfully installed the TestNG eclipse plugin, we can use it to write and run automation test cases. But before that, we should create a java project and add TestNG libraries in the project as below.

  1. Click eclipse File —> New —> Others menu item.
  2. Input java project in the Select a wizard dialog input text box and select Java Project under the search text box.
  3. Click the Next button in the above Select a wizard dialog, it will show the Create a Java Project wizard. Input the Project name and Location ( java project saved folder ) in the Create a Java Project wizard and then click Finish.

4. Add TestNG Library In Eclipse Java Project.

  1. Right-click the java project name in eclipse, click the Build Path —> Add Libraries menu item.
  2. It will display the Add Library wizard dialog, select TestNG in the libraries list, click the Next button.
  3. Click the Next then Finish button in the above dialogs. Now you can see TestNG libraries have been successfully added to this java project. You can find the jar files in the eclipse project-name / TestNG library folder.

5. Write And Run TestNG Java Class.

  1. Right-click the java project in eclipse and click the New —> Other menu item in the popup menu.
  2. Input the keyword TestNG in the Select a wizard search box, select TestNG/TestNG class below the search box, click the Next button.
  3. It will open the New TestNG class window, input detailed test class information ( for example Source folder, Package name, Class name) in this dialog. Check the @BeforeClass and @AfterClass checkboxes, then click the Finish button.
  4. Now you can see the wizard-generated TestNG java source file and source code in the eclipse java project. In my example, the java class file is com.dev2qa.example.testng.TestNGSelenium.java, and the TestNG plugin generated source code is as below.
    package com.dev2qa.example.testng;
    
    import org.testng.Assert;
    import org.testng.SkipException;
    import org.testng.annotations.AfterClass;
    import org.testng.annotations.BeforeClass;
    import org.testng.annotations.Test;
    
    
    public class TestNGSelenium {
        
      @Test(enabled = false) 
      /* This method will pass the test without error. */
      public void testStringPass() {
          
      }
      
      @BeforeClass
      public void beforeClass() {
         
      }
    
      @AfterClass
      public void afterClass() {
         
      }
    }
    
  5. Input below java code in the file. There are three @Test methods, they will be executed one by one. Please see the java code comments for more detail.
      @Test
      /* This method will pass the test without error. */
      public void testStringPass() {
    	  
    	  String actualStr = "Welcome to dev2qa.com";
    	  
    	  String expectStr = "Welcome to dev2qa.com";
    	  
    	  Assert.assertEquals(expectStr, actualStr);
    	 
      }
      
      @Test(enabled = true) // If not specify then enabled default value is true.
      /* This method will be failed because the compared two string is not equal. */
      public void testStringFail() {
    	  
    	  String actualStr = "Welcome to dev2qa.com";
    	  
    	  String expectStr = "Welcome to java.com";
    	  
    	  Assert.assertEquals(expectStr, actualStr);
    	 
      }
      
      @Test
      /* This method will be skipped because it throw a SkipException object. */
      public void testStringSkipped() {
    	  
    	  String actualStr = "Welcome to dev2qa.com";
    	  
    	  String expectStr = "Welcome to google.com";
    	  
    	  throw new SkipException("This method is skipped.");
      }
  6. Right-click the java code editor area, click Run As —> TestNG Test menu in the popup menu list.
  7. After execution, you can see the TestNG methods execution result is printed on the eclipse Results tab the Console tab.
  8. In this example, the three TestNG @Test methods result are:  testStringPass() pass, testStringFail() fail and testStringSkip() skip.

6. Debug TestNG Test Method In Eclipse.

  1. Right-click the left first column in the eclipse java source code editor, click the Toggle Breakpoint menu item in the popup menu list to enable or disable the breakpoint.
  2. Right-click the java source code edit area, click the Debug As —> TestNG Test menu item in the popup menu list.
  3. When the code execution stopped at the breakpoint, you can use the top debug tool to execute debug actions such as step-into, step-over, etc.

7. Generate TestNG Html Test Result Reports In Eclipse.

  1. After executing the test class, right-click the eclipse java project name, click Refresh in the popup menu list.
  2. You can see a test-output folder under the eclipse java project name, the folder is used to save the generated test report files ( index.html, emailable-report.html, testng-results.xml, etc).
  3. Right-click index.html and open it in a web browser by clicking the Open With —> Web Browser menu item in the popup menu list.
  4. It will open the Test results information page in the web browser, it will list all the test suite test class and test methods and the methods execution results.

8. Get testng.xml File Content.

  1. Click the testng-eclipse-55655887-testng-customsuite.xml link in the test result html report page left panel Info section. You can see the content for the TestNG project testng.xml in the right panel.

9. Conclusion.

  1. Do not need java main() method to run test cases.
  2. Exception will be handled by framework, so if one test method throw exception, other test methods will not be influenced.
  3. Test method do not need to be static.
  4. Can have multiple test methods, each for one specific test case.
  5. Test method should has @Test annotation.

3 thoughts on “TestNG Eclipse Plugin”

  1. When I install the TestNG Eclipse plugin in the eclipse Help —> Marketplace, it prompts a confirm dialog with the message The following solutions are not available: TestNG for eclipse Proceed with the installation anyway?. I click the Yes button to confirm, but it returns the error message The request can not complete. Please see the error log for details. After this, the TestNG plugin is not installed and I go to Help —> Install New Software to search the TestNG plugin to install it, but I can not find it in the panel. How can I install the TestNG plugin for the eclipse? Thanks.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.