TestNG, where NG stands for “Next Generation” is a testing framework inspired by JUnit (in Java) and NUnit (in C#). It can be used for unit, functional, integration, and end-to-end testing. It mainly uses Java annotations to configure and write test methods.
TestNG was developed by Cedric Beust. He developed it to overcome a deficiency in JUnit.
A few of the features that TestNG has over JUnit4 are:
- Extra Before and After annotations such as Before/After Suite and Before/After Group
- Dependency test
- Grouping of test methods
- Multithreaded execution
- In-built reporting framework
TestNG is written in Java and can be used with Java as well as with Java-related languages such as Groovy. In TestNG, suites and tests are configured or described mainly through XML files. By default, the name of the file is testng.xml, and it is not mandatory to put the same name, we can change the name if we want to.
TestNG allows users to do test configuration through XML files and allows them to include or exclude respective packages, classes, and methods in their test suite. It also allows users to group test methods into particular named groups and to include or exclude them as part of the test execution.
Parameterization of test methods is very easy using TestNG and it also provides an easy method of creating data-driven tests.
The following are a few of the most important features:
Multiple Before and After annotation options:
TestNG provides multiple kinds of Before/After annotations for support of different setup and cleanup options.
XML-based test configuration and test suite definition:
Test suites in TestNG are configured mainly using XML files. An XML file can be used to create suites using classes, test methods, and packages, as well as by using TestNG groups. This file is also used to pass parameters to test methods or classes.
This is one of the major features of TestNG where you can tell TestNG to execute a dependent test method to run after a given test method. You can also configure whether the dependent test method has to be executed or not in case the earlier test method fails.
Groups/group of groups:
Using this feature you can assign certain test methods into particular named groups (ex: Regression tests, Smoke tests and Sanity tests etc…) and tell TestNG to include or exclude a particular group in a test. Means, you can execute only Regression tests if you want to…
Like dependent methods, this feature allows test methods belonging to one group being dependent upon another group.
Parameterization of test methods:
This feature helps users to pass parameter values through an XML configuration file to the test methods, which can then be used inside the tests.
TestNG allows users to do data-driven testing of test methods using this feature. The same test method gets executed multiple times based on the data.
This allows execution of test cases in a multithreaded environment. This feature can be used for parallel test execution to reduce execution time or to test a multithreaded test scenario.
TestNG internally generates an XML and HTML report by default for its test execution. You can also add custom reports to the framework if required.
TestNG provides easy extension of API, this helps in adding custom extensions or plugins to the framework depending upon the requirement.
You have 2 ways to use the TestNg in your project:
- You can download the testng.jar file and use in your project.
- You can add as a plug-in to your IDE (i.e. Java or IntelliJ IDEA)
TestNG can be downloaded, installed, and run in multiple ways as follows:
- Using command line
- As an Eclipse plugin
- As an IntelliJ IDEA plugin
- Using ANT
- Using Maven (http://mvnrepository.com/artifact/org.testng/testng – from here you can download latest testng.jar file)
- Java JDK5 or above is installed on your system.
- JDK is set in the system path.
Please watch youtube video for better understanding.