TestNG Eclipse Plugin

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

  1. UnInstall and Install TestNG Eclipse plugin.
  2. Create TestNG java project.
  3. Add library in 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.

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.
    eclipse help installation details menu
  2. Input below search keyword in search text box, then bottom panel will list related plugins.
    search testng eclipse plugin
  3. Select the plugin you want to uninstall, click Uninstall button at bottom.
    uninstall eclipse testng plugin
  4. Wait for sometime and click “Finish” button, there will popup a dialog that let you to restart eclipse, click “Yes” button to restart it.
  5. After restart, the eclipse plugin has been removed successfully.

Install TestNG Eclipse Plugin

  1. Click “Help —> Eclipse Marketplace” menu item.
    help eclipse marketplace menu item
  2. Input search keyword in below picture in search box, click Enter to search the eclipse plugin in the marketplace.
    search testng eclipse plugin in eclipse marketplace
  3. Click Install button in above picture ” TestNG for Eclipse ” area. Wait for sometime, check all checkbox in the next dialog. Click Confirm button in bottom.
    confirm install testng eclipse plugin
  4. Accept Apache software license and click Finish button.
    apache software licence
  5. Click OK if you meet security warning dialog.
    security warning dialog
  6. After this plugin installation, click “Yes” to restart Eclipse now.
  7. After restart, click “Window —> Preferences”
    click window preferences menu
  8. If you can find TestNG in left panel that means the plugin has been installed successfully.
    verify testng eclipse plugin installation successful
READ :   How To Solve Eclipse Building Workspace Too Slow Problem

Create Java Project

After successfully installed 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 related library in the project as below.

  1. Click ” File —> New —> Others ” menu item.
    eclipse file new others menu item
  2. Search and select java project.
    search and choose java project
  3. Click Next and input Project name and store folder and then click Finish.
    input java project name and location

Add TestNG Library 

  1. Right click the java project name, click ” Build Path —> Add Libraries ” menu item.
    java project build path add libraries menu item
  2. Select TestNG in library list, click Next.
    select TestNG java library
  3. Click “Next” —> “Finish” button. Now you can see TestNG libraris has been successfully added in this java project.
    testng library has been added in this java project

Write And Run TestNG Java Class

  1. Click the java project and click ” New —> Others ” in the popup menu.
    java project new others menu
  2. Input TestNG in search box, click enter.
    choose TestNG class
  3. Click Next, input detail test class information in the next dialog as below picture. Select @BeforeClass and @AfterClass checkbox.
    test class detail information
  4. Click Finish button, Now you can see below java code generated by the wizard.
    testng wizard generated java code
  5. Input below java code in the file. There are three @Test method, 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 edit area, click ” Run As —> TestNG Test” popup menu.
    run testng class test methods
  7. After execute, you can see below graphic and console execution result for above code.
    The three @Test method result are:  testStringPass() pass, testStringFail() fail and testStringSkip skip.
    graphic testng test method execution result
    Test result console output.
    testng test result console output
READ :   Manage Maven Project Using Eclipse

Debug Test Method

  1. Right click the left vertical bar in java code editor, toggle break point in the popup menu.
    set break point in java test method
  2. Right click java code edit area, click ” Debug As —> TestNG Test ” in popup menu.
    debug test method
  3. When code execution stopped at the break point, you can use top debug tool to execute multiple actions such as step into, step over etc.
    java debug tool

Generate Html Test Result Reports

  1. After execute the test class, right click the project name, click Refresh in the popup menu list.
    refresh java project
  2. There will have a test-output folder for test report.
    output folder for testng test report
  3. Right click index.html and open it in a web browser.
    open index.html in web browser
  4. You can see below test execution result report page.
    testng test results report

Get testng.xml File Content 

  1. Click the .xml link in test result html report page left panel. You can see the content for testng.xml in right panel as below.
    testng.xml content

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.

Download “TestNGExampleProject.zip” TestNGExampleProject.zip – Downloaded 63 times – 61 KB

(Visited 530 times, 1 visits today)

Leave a Reply

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.