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 is an advanced framework that can be integrated with Selenium or any other automation tool. It is a widely used unit testing and reporting framework with Selenium WebDriver.
It provides multiple rich features for testing like assertions, reporting, parameterization, parallel test execution, etc.
TestNG provides a DataProvider feature to perform data-driven testing. Creating data-driven tests in TestNG is very easy as compared with JUnit.
Who developed TestNG?
TestNG was developed by Cedric Beust to overcome the deficiency in JUnit by introducing some more new functionalities and features.
It mainly uses Java annotations to configure and write test methods. It is distributed under the Apache Software License and is readily available for download.
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 WebDriver. It can be used for unit, integration, functional and end-to-end testing.
Why do we use TestNG Framework with Selenium?
There are the following reasons or purposes for using TestNG with Selenium. They are as follows:
1. Selenium WebDriver does not generate detailed test or summary reports. If Selenium test cases are created by integrating TestNG with Selenium WebDriver, no external task is required to generate detailed test reports.
We can easily generate detailed test reports or test result in a proper readable format (or in an organized folder) to know about how many test cases are passed, failed, and skipped.
2. We can execute selective test cases by integrating TestNG with Selenium WebDriver.
For example, suppose, we have 25 test cases and we want to execute only 5 test cases. So, we can easily execute 5 test cases using TestNG framework.
3. Using TestNG, we can also execute failed test cases separately.
For example, suppose, we have 5 test cases, and out of five test cases, the first three test cases are successfully executed but the fourth test case is failed due to some error.
In this situation, we can run the fourth test case using the testng-failed.xml file option provided by TestNG in the output folder. We will have to run this XML file to execute the only failed test case.
4. We can easily integrate the TestNG framework with various tools and plugins like ANT, Maven, Jenkins, Integrated Development Environment (Eclipse), 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, we have been familiarized a little with the most commonly used TestNG framework. So, let’s move forward and understand in more detail the features provided by TestNG.
The most important features of TestNG are as follows:
1. Object-oriented: Since TestNG is written in Java, 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 instructs how to run.
3. Priority: It is one of the major features provided by TestNG where we can instruct TestNG to execute the 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 that 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. Multi-threaded execution: This feature is mainly used to execute test cases in a multi-threaded 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.
11. Inbuilt Exception handling: TestNG has an inbuilt exception handling mechanism that allows us to run the program without terminating unexpectedly.
12. Thread-safe: TestNG framework supports a thread-safe environment when executing more than one thread.
We will discuss these features in more detail in the further tutorials.
Advantage of TestNG Framework over JUnit
Both TestNG and JUnit belong to the same family of the unit testing framework. But TestNG framework provides several advantages over JUnit that are as follows:
1. TestNG generates XML, HTML test reports in a readable format. It provides a very good reporting structure.
2. Test method can be grouped and prioritized more easily which is not possible in JUnit. We can easily execution patterns.
3. Annotations of TestNG are easy to understand for creating test cases. It provides superior and advanced annotations over JUnit.
4. We can set the dependencies of Test cases.
5. Parallel testing is also possible in TestNG.
6. TestNG does not need to extend any class.
Disadvantage of TestNG Framework
The main disadvantage of TestNG framework is that it does not allow us to define our own reporting format.
How to Download and Install TestNG Framework in Eclipse?
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 understand the second way to install TestNG into Eclipse and the remaining ways in the further tutorials.
Follow the step by step TestNG download and installation plugin on Eclipse:
Before going to install the TestNG plugin into eclipse, please make sure that you have Eclipse installed in your system or not. If not, I recommend that you download the latest version of Eclipse.
Now, let’s start the installation process of TestNG in Eclipse.
a. Open your Eclipse application and click on the Help option on the menu bar.
b. Choose the option “Eclipse Marketplace…” option from the dropdown.
Step 2: In Eclipse Marketplace dialog box, type TestNG in the search box as shown in the below screenshot and click the Go button or press enter key.
Step 3: Now click on the install button to install TestNG.
Step 4: As soon as you click on the Install button, a new window will be open for feature selection but you do not have to change anything. Just click on the “Confirm” button.
Step 5: In the next step, click on “I accept the terms of the license agreement” and then click on the “Finish” button.
Step 6: If you get a Security Warning dialog box, just click OK button.
Now, the installation is initiated, and wait for the installation to finish. The progress can be seen as follows.
Step 7: After installation, Eclipse will prompt you for a restart, just click on the Yes button.
Step 8: After the restart, you can verify that TestNG has been indeed successfully installed or not. For this, click on the Window option in Eclipse > Preferences and see if TestNG is included on the Preferences list.
That’s the end of TestNG download and installation.
Another way to download and Install TestNG
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 the OK button.
5. On clicking OK, TestNG update site will get connected to Eclipse. A software window will be opened to download the TestNG plugin under the TestNG site.
6. Select TestNG and click on the Next button.
7. The eclipse will calculate the software requirement to download TestNG and will display installation details screen. Click on Next on the 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 the Yes option on the window prompt.
11. After the restart, verify that TestNG plugin has been successfully installed or not. For this, click the Window option in Eclipse > Preferences and see if TestNG is included on the Preferences list.
That’s the end of TestNG Installation.
Hope that this tutorial has covered almost all the important points related to the features and applications of TestNG framework. I hope that you will have successfully downloaded and installed TestNG plugin in your Eclipse and enjoyed this tutorial.
In the next tutorial, we will learn how to write test cases in TestNG.
Thanks for reading!!!