So far we had been doing Selenium tests without generating a proper format for the test results. From this point on, we shall tackle how to make these reports using a test framework called TestNG.
TestNG is a testing framework that overcomes the limitations of another popular testing framework called JUnit. The "NG" means "Next Generation". Most Selenium users use this more than JUnit because of its advantages. There are so many features of TestNG, but we will only focus on the most important ones that we can use in Selenium.
There are three major advantages of TestNG over JUnit:
- Annotations are easier to understand
- Test cases can be grouped more easily
- Parallel testing is possible
Annotations in TestNG are lines of code that can control how the method below them will be executed. They are always preceded by the @ symbol. A very early and quick example is the one shown below.
Annotations will be discussed later in the section named "Annotations used in TestNG", so it is perfectly ok if you do not understand the above example just yet. It is just important to note for now that annotations in TestNG are easier to code and understand than in JUnit.
The ability to run tests in parallel is available in TestNG but not in JUnit, so it is the more preferred framework of testers using Selenium Grid.
TestNG can generate reports based on our Selenium test results.
- WebDriver has no native mechanism for generating reports.
- TestNG can generate the report in a readable format like the one shown below.
TestNG simplifies the way the tests are coded
- There is no more need for a static main method in our tests. The sequence of actions is regulated by easy-to-understand annotations that do not require methods to be static.
- Uncaught exceptions are automatically handled by TestNG without terminating the test prematurely. These exceptions are reported as failed steps in the report.
Installing TestNG in Eclipse
- Launch Eclipse.
- On the menu bar, click Help.
- Choose the "Install New Software..." option.
In the Install dialog box, click the Add button
- In "Name", type TestNG.
- In "Location", type http://beust.com/eclipse.
- Click OK
- Notice that "TestNG - http://beust.com/eclipse" was populated onto the "Work with:" textbox.
- Check the "TestNG" check box as shown below, then click Next.
- Note: In the latest Eclipse (Kepler) you don't have a checkbox for TestNG, instead you click on question mark (help) icon which will open up the form, and you can select all and installation will continue as for the remaining instructions. Thanks Jana for the tip!
- Click Next again on the succeeding dialog box until you reach the License Agreement dialog.
- Click "I accept the terms of the license agreement" then click Finish.
Wait for the installation to finish
If you encounter a Security warning, just click OK
When Eclipse prompts you for a restart, just click Yes.
After the restart, verify if TestNG was indeed successfully installed. Click Window > Preferences and see if TestNG is included on the Preferences list.