The Next Generation Testing Framework – TestNG
TestNG is a testing framework for the Java Programming language. TestNG is designed to cover all categories of tests: unit, functional, end-to-end, integration, etc. TestNG is an annotation-based testing framework, which draws on some of the shortcomings of JUnit by adding features such as flexible fixtures, test categorization, parametric testing, and dependent methods.
This framework, is used to organize different tests, and to report results. TestNG is a testing framework inspired from JUnit and Nunit, but introduces some new functionalities that make it more powerful and easier to use, such as annotations and parameters that give you much more flexibility in your testing.
An example is “@Test” .This annotation is used to define a method that will be run as a test and it is not necessary follow any naming convention.
With TestNG, it is also possible to specify other particular methods in a class, called configuration methods, using the dedicated annotation @Configuration. There are four types of configuration methods: beforeTestClass, afterTestClass, beforeTestMethod and afterTestMethod and are executed after the instantiation of the class, but before any test method has been run.
Declare “@Parameters” annotation in method which needs parameter testing, the parametric data will be provided by TestNG’s XML configuration files. By doing this, you can easily reuse a single test case with different data sets.
Advanced parametric testing
TestNG handles the scenario requires complex type values with its @DataProvider annotation, which facilitates the mapping of complex parameter types to a test method.
TestNG welcomes test dependencies through the dependsOnMethods attribute of the Test annotation. With this handy feature, you can easily specify dependent method.If the dependent method fails, then all subsequent tests will be skipped, not marked as failed.
Fail and rerun
Anytime there is a failure in TestNG, it creates an XML configuration file that delineates the failed tests. Running a TestNG runner with this file causes TestNG to only run the failed tests.
Defining test groups
Every test method can be associated with one or more groups. Once you’ve defined these groups, you can choose to run only certain group of tests. To add a test to a group of tests, simply specify the group as a parameter of the @Test annotation.
With TestNG, you can check the occurrence of an exception very quickly and easily. Using the @ExpectedExceptions annotation with TestNG, makes the code of the test easy &straightforward to write. The@ExpectedExceptions annotation specifies that the raising of a NumberFormatException is tolerated by the framework and therefore should not be considered a failure.
TestNG is supported, out-of-the-box or via plug-ins, by each of the three major Java IDEs – Eclipse, IntelliJ IDEA, and NetBeans. It also comes with a custom task for Apache Ant and is supported by the Maven build system. The Hudson continuous integration server has built-in support for TestNG and is able to track and chart test results over time. Most Java code coverage tools, such as Cobertura, work seamlessly with TestNG.
TestNG generates test reports in HTML and XML formats. The XML output can be transformed by the Ant JUnitReport task  to generate reports similar to those obtained when using JUnit. Since version 4.6, TestNG also provides a reporter API  that permits third-party report generators, such as ReportNG  and TestNG-XSLT  , to be used.
Advantages of TestNG over JUnit
* Tests parametrization
* Parallel execution of tests
* Dynamic tests grouping and lists combinations
* Test Case inheritance isn’t needed, e.g. more flexibility for tests developers
* Elaborate listeners APIs
* Data providers API is available for even more complex data and parameters preparation for test cases
About the Author :
Anusree has been working for Bobcares as Sr. Software Engineer, since April 2010. Her experience and expertise lies in Manual testing, and she is equally knowledgeable in automation testing. She bags an experience of 2.2 years in the industry. Her hobbies include listening to music and travelling. She is passionate about automation testing tools, Python and Java.
Co-Authored by Sankar.H