Till now we have created our executable test scripts inside main methods in Java classes. However this approach has many limitations as they always require human intervention with the execution. To overcome these shortcomings, there are tools like TestNG, JUnit that not only help us with organizing and grouping of our tests but also help us with reporting of test results. In simple words, both these tools has many advantages that will make our tests more easier to develop and maintain. In this post we will be focusing on TestNG as it is an extension of JUnit.
TestNG Introduction:
TestNG is an open source automated testing tool available in market. It is similar to JUnit in design but it has more advantages over JUnit. One of the main reason of using TestNG or JUnit is that we don’t have to use static main methods in our test scripts. Some other advantages are:
- Selenium WebDriver has no support for test reports. However with TestNG/JUnit we can generate reports in readable formats.
- TestNG/JUnit has different annotation like ‘@BeforeClass’, ‘@AfterClass’, ‘@Test’ etc. These annotations help us in configuring our test cases. Also we can pass additional parameters using annotations.
- Any uncaught exceptions can be handled by TestNG/JUnit without terminating the test prematurely. These exceptions are reported as failed steps in the report.
In TestNG, we can use the following annotations for configuring our test case:
@BeforeSuite: The annotated method will be run before all tests in this suite have run.
@AfterSuite: The annotated method will be run after all tests in this suite have run.
@BeforeTest: The annotated method will be run before any test method belonging to the classes inside the <test> tag is run.
@AfterTest: The annotated method will be run after all the test methods belonging to the classes inside the <test> tag have run.
@BeforeGroups: The list of groups that this configuration method will run before. This method is guaranteed to run shortly before the first test method that belongs to any of these groups is invoked.
@AfterGroups: The list of groups that this configuration method will run after. This method is guaranteed to run shortly after the last test method that belongs to any of these groups is invoked.
@BeforeClass: The annotated method will be run before the first test method in the current class is invoked.
@AfterClass: The annotated method will be run after all the test methods in the current class have been run.
@BeforeMethod: The annotated method will be run before each test method.
@AfterMethod: The annotated method will be run after each test method.
@DataProvider: Marks a method as supplying data for a test method. The annotated method must return an Object[][] where each Object[] can be assigned the parameter list of the test method. The @Test method that wants to receive data from this DataProvider needs to use a dataProvider name equals to the name of this annotation.
@Test: Marks a class or a method as part of the test.
The complete list of annotations and their descriptions are mentioned in below link:
TestNG Annotations
TestNG Installation:
Now that we had some introduction on the TestNG, lets install in our Eclipse.
- Launch the Eclipse IDE and click Help -> Install New Software option.
- Click on Add button to Add Repository.
- Enter following details: Name: ‘TestNG’, Location: ‘http://beust.com/eclipse’. Click OK to close the window.
- Select ‘TestNG’ option and click Next.
- Click on Next and accept the license agreement and hit Finish to complete the installation process. Once the installation is complete, we need to restart the Eclipse.
- To verify if the installation was successful or not, right click on a project to view the below options.
With this we completed the TestNG installation in Eclipse. In our next post we will focus on creation and execution of TestNG tests and then view the test results in TestNG reports.