What is TestNG?

TestNG, where NG stands for "next generation" is the next step towards writing test cases. It is an open source automation testing framework based on JUnit (in Java) and NUnit (in C#). TestNG was developed by Cedric Beust to overcome the deficiency in JUnit by introducing some more functionalities and features.

It mainly uses Java annotations to configure and write test methods. Therefore, TestNG has gained a lot of popularity in a very short time which is widely used nowadays as an automation testing framework with selenium web driver. It can be used for unit, integration, functional and end-to-end testing. 

Why Use TestNG Framework with Selenium?

There is the following purpose of using TestNG in selenium. They are as follows:
1. Selenium WebDriver does not generate test reports but by integrating TestNG with selenium web driver, you can easily generate test reports or test results in a proper readable format to know about how many test cases are passed, failed, and skipped.
2. You can execute selective test cases. For example, suppose, you have 25 test cases and you want to execute only 5 test cases, so you can easily execute 5 test cases using TestNG framework. 

3. Using TestNG, we can also execute failed test cases separately. For example, suppose, you have 5 test cases and out five test cases, The first three test cases are successfully executed but the fourth test case is failed due to some error. You can run the fourth test case using testng-failed.xml file option provided by TestNG in the output folder. You will have to run this XML file to execute the only failed test case. 

4. We can easily integrate the TestNG framework with tools like Maven, Jenkins, etc.
5. Verification can be achieved with the help of only TestNG.
6. We can organize test cases easily in a proper way.

Features of TestNG Framework

Now that you are familiarized a little with TestNG, So let's move forward and see more about the features provided by TestNG. The most important features of TestNG are as follows.
1. Object-oriented: TestNG is written in Java so it uses more object-oriented and Java features.

2. Multiple Before/After annotations: TestNG offers multiple types of Before/After annotations to create test cases. Annotations are a kind of instructions for the defined set of code that instruct how to run.

3. Priority: It is one of the major features provided by TestNG where we can instruct TestNG to execute dependent test method after running the main test method by setting priority. Using priority, we can execute test methods in TestNG in a different order. We can also set priority whether the dependent test method has to be executed or not in case the earlier test method fails.

4. Groups/group of groups: This feature allows us to assign certain test methods into particular named groups. Using this feature, we can execute multiple test cases into a particular group.

5. XML file: An XML file is a file which is used to configure test suites in TestNG using classes, test methods, and packages. This file is also used to run the whole test from a single place by passing the parameters to the test methods or classes. 

6. Parameterization of test methods: This feature allows users to pass the parameter values directly through XML file to the test methods used in tests. 

7. Data-driven testing: This feature is widely used in test suites that allow users to execute the same test method multiple times based on different sets of data.

8. Multithreaded execution: This feature is mainly used to execute test cases in a multithreaded environment. Using this feature, we can execute multiple test cases in parallel to reduce the execution time.

9. Test report: TestNG internally generates an XML and HTML test report in a readable format by default for its test execution. Generation of test report includes a number of test cases run, passed, failed, and skipped. You can also add custom reports if required. 

10. Main method: There is no need for the static main method in the TestNG. The method need not be static. 
We will discuss these features in more detail in the further tutorial.

Advantages of TestNG over JUnit

1. TestNG generates HTML test reports in a readable format.
2. Test method can be grouped and prioritized more easily which is not possible in JUnit.
3. Annotations of TestNG are easy to understand for creating test cases.
4. Parallel testing is also possible in TestNG.
5. TestNG does not need to extend any class.

How to Download & Install TestNG Framework?

There are the following ways to download, install, and run TestNG. They are as follows.
1. Using command line
2. By an Eclipse plugin.
3. By an IntelliJ IDEA plugin.
4. Using ANT.
5. Using Maven.

In this tutorial, we will see the second way to install TestNG into Eclipse and the remaining ways in the further tutorial. 

Step by step guide to Install TestNG plugin in Eclipse

Before going to install TestNG plugin into eclipse, please make sure that you have Eclipse installed in your system. You can get the latest version of Eclipse.
Let's start the installation process of TestNG in Eclipse.
Step 1: a. Open your Eclipse application and click to help option on the menu bar.
             b. Choose the option "Eclipse Market place...".
Install TestNG plugin
Step 2: In Eclipse Marketplace dialog box, type TestNG in the search box as shown in the below screenshot and click the search button or press enter key.
Install TestNG plugin
Step 3: Now click on the install button.
Install TestNG plugin
Step 4: A new window will be open for feature selection but you do not have to change anything, Just click on the confirm button.
Install TestNG plugin
Step 5: Click on "I accept the terms of the license agreement" and then click Finish.
Install TestNG plugin
Step 6: If you get a Security warning dialog box, just click OK.
Install TestNG plugin
Now, wait for the installation to finish.
Install TestNG plugin
Step 7: After installation, Eclipse will prompt you for a restart, just click Yes.
Install TestNG plugin
Step 8: After the restart, you can verify that TestNG has been indeed successfully installed or not. For this, click Window option in Eclipse > Preferences and see if TestNG is included on the Preferences list.
That's end TestNG Installation.

There is another way to install the TestNG into Eclipse. Just follows all the below steps. 
1. Open your Eclipse application.
2. Go to Help and click on "Install New Software".
3. Click on add button. You will get a text dialog box. 
4. Enter "TestNG site" into the Name box and enter the URL http://beust.com/eclipse into the Location box. Once done, click on OK button.

5. On clicking OK, TestNG update site will get connected to Eclipse. A software window will be opened to download TestNG plugin under the TestNG site. 
6. Select TestNG and click on Next button.
7. The eclipse will calculate software requirement to download TestNG and will display installation details screen. Click on Next on details screen.

8. Accept the license agreement and click on finish. The download and installation of the TestNG plugin into Eclipse will start. 
9. In case you get any warning window then click on the OK button. 
10. Once the installation is complete, Eclipse will prompt you to restart it. Click on Yes option on the window prompt.
11. After the restart, verify that TestNG plugin has been indeed successfully installed or not. For this, click Window option in Eclipse > Preferences and see if TestNG is included on the Preferences list.
That's end TestNG Installation.

Final words 
Hope that this tutorial has covered almost all the important points in the TestNG framework tutorial. I hope that you will have enjoyed this tutorial.
Thanks for reading!