TestNG
   HOME

TheInfoList



OR:

TestNG is a testing
framework A framework is a generic term commonly referring to an essential supporting structure which other things are built on top of. Framework may refer to: Computing * Application framework, used to implement the structure of an application for an op ...
for the
Java programming language Java is a high-level, class-based, object-oriented programming language that is designed to have as few implementation dependencies as possible. It is a general-purpose programming language intended to let programmers ''write once, run anywh ...
created by Cédric Beust and inspired by
JUnit JUnit is a unit testing framework for the Java programming language. JUnit has been important in the development of test-driven development, and is one of a family of unit testing frameworks which is collectively known as xUnit that originated ...
and NUnit. The design goal of TestNG is to cover a wider range of test categories: unit, functional, end-to-end, integration, etc., with more powerful and easy-to-use functionalities.


Features

TestNG's main features include: # Annotation support. # Support for data-driven/parameterized testing (with @DataProvider and/or XML configuration). # Support for multiple instances of the same test class (with @Factory) # Flexible execution model. TestNG can be run either by
Ant Ants are eusocial insects of the family Formicidae and, along with the related wasps and bees, belong to the order Hymenoptera. Ants evolved from vespoid wasp ancestors in the Cretaceous period. More than 13,800 of an estimated total of 22 ...
via build.xml (with or without a test suite defined), or by an IDE plugin with visual results. There isn't a TestSuite class, while test suites, groups and tests selected to run are defined and configured by XML files. # Concurrent testing: run tests in arbitrarily big thread pools with various policies available (all methods in their own thread, one thread per test class, etc.), and test whether the code is multithread safe. # Embeds
BeanShell BeanShell is a small, free, embeddable Java source interpreter with object scripting language features, written in Java. It runs in the Java Runtime Environment (JRE), dynamically executes standard Java syntax and extends it with common scripting c ...
for further flexibility. # Default
JDK The Java Development Kit (JDK) is a distribution of Java Technology by Oracle Corporation. It implements the Java Language Specification (JLS) and the Java Virtual Machine Specification (JVMS) and provides the Standard Edition (SE) of the Java ...
functions for runtime and logging (no dependencies). # Dependent methods for application server testing. # Distributed testing: allows distribution of tests on slave machines.


Data provider

A data provider in TestNG is a method in a test class, which provides an array of varied actual values to dependent test methods. Example: //This method will provide data to any test method that declares that its Data Provider is named "provider1". @DataProvider(name = "provider1") public Object[][] createData1() // This test method declares that its data should be supplied by the Data Provider named "provider1". @Test(dataProvider = "provider1") public void verifyData1(String n1, Integer n2) // A data provider which returns an iterator of parameter arrays. @DataProvider(name = "provider2") public Iterator createData() // A data provider with an argument of the type java.lang.reflect.Method. // It is particularly useful when several test methods use the same // provider and you want it to return different values depending on // which test method it is serving. @DataProvider(name = "provider3") public Object[][] createData(Method m) The returned type of a data provider can be one of the following two types: *An array of array of objects (Object[][]) where the first dimension's size is the number of times the test method will be invoked and the second dimension size contains an array of objects that must be compatible with the parameter types of the test method. *An Iterator. The only difference with Object[][] is that an Iterator lets you create your test data lazily. TestNG will invoke the iterator and then the test method with the parameters returned by this iterator one by one. This is particularly useful if you have a lot of parameter sets to pass to the method and you don't want to create all of them upfront.


Tool support

TestNG is supported, out-of-the-box or via plug-ins, by each of the three major Java IDEs -
Eclipse An eclipse is an astronomical event that occurs when an astronomical object or spacecraft is temporarily obscured, by passing into the shadow of another body or by having another body pass between it and the viewer. This alignment of three ce ...
,
IntelliJ IDEA IntelliJ IDEA is an integrated development environment (IDE) written in Java (programming language), Java for developing computer software written in Java, Kotlin (programming language), Kotlin, Groovy (programming language), Groovy, and other ...
, and
NetBeans NetBeans is an integrated development environment (IDE) for Java (programming language), Java. NetBeans allows applications to be developed from a set of modular software components called ''modules''. NetBeans runs on Microsoft Windows, Windows, ...
. It also comes with a custom task for
Apache Ant Apache Ant is a software tool for automating software build processes which originated from the Apache Tomcat project in early 2000 as a replacement for the Make build tool of Unix. It is similar to Make, but is implemented using the Java languag ...
and is supported by the
Maven MAVEN is an American spacecraft orbiting Mars to study the loss of its atmospheric gases to space, providing insight into the history of the planet's climate and water. The spacecraft name is an acronym for "Mars Atmosphere and Volatile Evolu ...
build system. The
Hudson Hudson may refer to: People * Hudson (given name) * Hudson (surname) * Henry Hudson, English explorer * Hudson (footballer, born 1986), Hudson Fernando Tobias de Carvalho, Brazilian football right-back * Hudson (footballer, born 1988), Hudso ...
continuous integration server has built-in support for TestNG and is able to track and chart test results over time. Most Java
code coverage In computer science, test coverage is a percentage measure of the degree to which the source code of a program is executed when a particular test suite is run. A program with high test coverage has more of its source code executed during testing, ...
tools, such as Cobertura, work seamlessly with TestNG. Note: TestNG support for Eclipse is only embedded in the Eclipse Marketplace for Eclipse versions up to 2018-09 (4.9). For later versions of Eclipse, TestNG must be manually installed as per instructions in the TestNG site.


Reporting

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, PDFngreport and TestNG-XSLT, to be used.


Comparison with JUnit

TestNG has a longstanding rivalry with another testing tool
JUnit JUnit is a unit testing framework for the Java programming language. JUnit has been important in the development of test-driven development, and is one of a family of unit testing frameworks which is collectively known as xUnit that originated ...
. Each framework has differences and respective advantages.
Stack Overflow In software, a stack overflow occurs if the call stack pointer exceeds the stack bound. The call stack may consist of a limited amount of address space, often determined at the start of the program. The size of the call stack depends on many fac ...
discussions reflect this controversy.


Annotations

In JUnit 5, the @BeforeAll and @AfterAll methods have to be declared as static in most circumstances. TestNG does not have this constraint. TestNG includes four additional setup/teardown annotation pairs for the test suite and groups: @BeforeSuite, @AfterSuite, @BeforeTest, @AfterTest, @BeforeGroup and @AfterGroup, @BeforeMethod and @AfterMethod. TestNG also provides support to automate testing an application using selenium.


Parameterized testing

Parameterized testing is implemented in both tools, but in quite different ways. TestNG has two ways for providing varying parameter values to a test method: by setting the ''testng.xml'', and by defining a ''@DataProvider'' method. In JUnit 5, the ''@ParameterizedTest'' annotation allows parameterized testing. This annotation is combined with another annotation declaring the source of parameterized arguments, such as ''@ValueSource'' or ''@EnumSource''. Using ''@ArgumentsSource'' allows the user to implement a more dynamic ArgumentsProvider. In JUnit 4, ''@RunWith'' and ''@Parameters'' are used to facilitate parameterized tests, where the ''@Parameters'' method has to return a List[] with the parameterized values, which will be fed into the test class constructor.


Conclusion

Different users often prefer certain features of one framework or another. JUnit is more widely popular and often shipped with mainstream IDEs by default. TestNG is noted for extra configuration options and capability for different kinds of testing. Which one more suitable depends on the use context and requirements.


See also

*
List of unit testing frameworks This article is a list of tables of code-driven unit testing frameworks for various programming languages. Some, but not all, of these are based on xUnit. Columns (classification) * Name: This column contains the name of the framework and wil ...
*
JUnit JUnit is a unit testing framework for the Java programming language. JUnit has been important in the development of test-driven development, and is one of a family of unit testing frameworks which is collectively known as xUnit that originated ...
*
xUnit xUnit is the collective name for several unit testing frameworks that derive their structure and functionality from Smalltalk's SUnit. ''SUnit'', designed by Kent Beck in 1998, was written in a highly structured object-oriented style, which lent ...


References


External links


TestNG Home page
{{DEFAULTSORT:Testng Unit testing frameworks Java platform Software using the Apache license