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.
- UnInstall and Install TestNG Eclipse plugin.
- Create TestNG java project.
- Add libraries to the java project.
- Write and Run TestNG java class with Eclipse.
- Debug test methods.
- Generate Html test result reports.
- Get testng.xml file content.
- 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).
- Click “Help —> Installation Details” menu item in eclipse top menubar. It will open the Eclipse Installation Details popup window.
- 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.
- 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.
- 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.
- After restart, the eclipse plugin has been removed successfully.
2. Install TestNG Eclipse Plugin.
- Click eclipse “Help —> Eclipse Marketplace” menu item at the top menubar.
- Input search keyword testng in the Find search box, click Enter key or Go button to search the eclipse plugin in the marketplace.
- 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.
- In the Review Licenses wizard dialog, accept the apache software license and click the Finish button.
- Click OK to continue to install the plugin if you meet the Security Warning dialog which said your installed software is unsigned.
- After this plugin installation, click “Yes” to restart Eclipse.
- After restart, click eclipse Window —> Preferences menu item.
- 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.
- Click eclipse File —> New —> Others menu item.
- Input java project in the Select a wizard dialog input text box and select Java Project under the search text box.
- 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.
- Right-click the java project name in eclipse, click the Build Path —> Add Libraries menu item.
- It will display the Add Library wizard dialog, select TestNG in the libraries list, click the Next button.
- 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.
- Right-click the java project in eclipse and click the New —> Other menu item in the popup menu.
- Input the keyword TestNG in the Select a wizard search box, select TestNG/TestNG class below the search box, click the Next button.
- 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.
- 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() { } }
- 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."); }
- Right-click the java code editor area, click Run As —> TestNG Test menu in the popup menu list.
- After execution, you can see the TestNG methods execution result is printed on the eclipse Results tab the Console tab.
- In this example, the three TestNG @Test methods result are: testStringPass() pass, testStringFail() fail and testStringSkip() skip.
6. Debug TestNG Test Method In Eclipse.
- 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.
- Right-click the java source code edit area, click the Debug As —> TestNG Test menu item in the popup menu list.
- 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.
- After executing the test class, right-click the eclipse java project name, click Refresh in the popup menu list.
- 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).
- 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.
- 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.
- 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.
- Do not need java main() method to run test cases.
- Exception will be handled by framework, so if one test method throw exception, other test methods will not be influenced.
- Test method do not need to be static.
- Can have multiple test methods, each for one specific test case.
- Test method should has @Test annotation.
Thank you very much for this document
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.
Please provides your Eclipse version and TestNG plugin version, we can not give you advice without that information. Maybe the link https://github.com/cbeust/testng-eclipse is helpful to you.