Jump to content
 







Main menu
   


Navigation  



Main page
Contents
Current events
Random article
About Wikipedia
Contact us
Donate
 




Contribute  



Help
Learn to edit
Community portal
Recent changes
Upload file
 








Search  

































Create account

Log in
 









Create account
 Log in
 




Pages for logged out editors learn more  



Contributions
Talk
 



















Contents

   



(Top)
 


1 Features  



1.1  Data provider  





1.2  Tool support  





1.3  Reporting  







2 Comparison with JUnit  



2.1  Annotations  





2.2  Parameterized testing  





2.3  Conclusion  







3 See also  





4 References  





5 External links  














TestNG






Deutsch
Español
Français
Magyar

Polski

 

Edit links
 









Article
Talk
 

















Read
Edit
View history
 








Tools
   


Actions  



Read
Edit
View history
 




General  



What links here
Related changes
Upload file
Special pages
Permanent link
Page information
Cite this page
Get shortened URL
Download QR code
Wikidata item
 




Print/export  



Download as PDF
Printable version
 
















Appearance
   

 






From Wikipedia, the free encyclopedia
 


TestNG
Developer(s)Cédric Beust, the TestNG team
Stable release

7.6.1 / July 4, 2022; 2 years ago (2022-07-04)[1]

Repository
Written inJava
Operating systemCross-platform
TypeUnit testing tool
LicenseApache License 2.0[2]
Websitetestng.org

TestNG is a testing framework for the Java programming language created by Cedric_Beust and inspired by JUnit 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

[edit]

TestNG's main features include:

  1. Annotation support.
  2. Support for data-driven/parameterized testing (with @DataProvider and/or XML configuration).
  3. Support for multiple instances of the same test class (with @Factory)
  4. Flexible execution model. TestNG can be run either by Ant 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.
  5. 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.
  6. Embeds BeanShell for further flexibility.
  7. Default JDK functions for runtime and logging (no dependencies).
  8. Dependent methods for application server testing.[clarification needed]
  9. Distributed testing: allows distribution of tests on slave machines.


Data provider

[edit]

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() {
  return new Object[][] { 
   { "Cedric", new Integer(36) },
   { "Anne", new Integer(37) }
  };
 }

 // 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) {
  System.out.println(n1 + " " + n2);
 }

 // A data provider which returns an iterator of parameter arrays.
 @DataProvider(name = "provider2")
 public Iterator<Object[]> createData() {
  return new MyIterator(...);
 } 

 // 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) {
  System.out.println(m.getName()); 
  return new Object[][] { new Object[] { "Cedric" } };
 }

The returned type of a data provider can be one of the following two types:

Tool support

[edit]

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.

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.[3]

Reporting

[edit]

TestNG generates test reports in HTML and XML formats. The XML output can be transformed by the Ant JUnitReport task[4] to generate reports similar to those obtained when using JUnit. Since version 4.6, TestNG also provides a reporter API[5] that permits third-party report generators, such as ReportNG,[6] PDFngreport[7] and TestNG-XSLT,[8] to be used.

Comparison with JUnit

[edit]

TestNG has a longstanding rivalry with another testing tool JUnit. Each framework has differences and respective advantages. Stack Overflow discussions reflect this controversy.[9][10][11]

Annotations

[edit]

In JUnit 5, the @BeforeAll and @AfterAll methods have to be declared as static in most circumstances.[12] 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

[edit]

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.[13]

In JUnit 5, the @ParameterizedTest annotation allows parameterized testing. This annotation is combined with another annotation declaring the source of parameterized arguments, such as @ValueSourceor@EnumSource. Using @ArgumentsSource allows the user to implement a more dynamic ArgumentsProvider.[14] 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

[edit]

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

[edit]

References

[edit]
  • ^ "Apache License Version 2.0". January 2004. Archived from the original on 2013-12-21.
  • ^ "TestNG for Eclipse".
  • ^ JUnitReport Archived 2010-02-24 at the Wayback Machine
  • ^ "Announcing TestNG 4.6". Archived from the original on 2010-12-12. Retrieved 2010-03-04.
  • ^ ReportNG 1.0 Final Released
  • ^ PDFngreport 1.0.0
  • ^ TestNG XSL Reports
  • ^ "Junit vs TestNG". stackoverflow.com.
  • ^ "JUnit vs TestNG". stackoverflow.com. Archived from the original on 2014-02-02.
  • ^ "Which UnitTest framework to learn for Java now?". stackoverflow.com.
  • ^ "Writing Tests - Annotations". junit.org.
  • ^ "What is TestNG?". qacreators.com.
  • ^ "JUnit 5 User Guide". junit.org.

  • [edit]
    Retrieved from "https://en.wikipedia.org/w/index.php?title=TestNG&oldid=1206057792"

    Categories: 
    Unit testing frameworks
    Java platform
    Software using the Apache license
    Hidden categories: 
    Webarchive template wayback links
    Wikipedia articles needing clarification from May 2012
     



    This page was last edited on 11 February 2024, at 03:20 (UTC).

    Text is available under the Creative Commons Attribution-ShareAlike License 4.0; additional terms may apply. By using this site, you agree to the Terms of Use and Privacy Policy. Wikipedia® is a registered trademark of the Wikimedia Foundation, Inc., a non-profit organization.



    Privacy policy

    About Wikipedia

    Disclaimers

    Contact Wikipedia

    Code of Conduct

    Developers

    Statistics

    Cookie statement

    Mobile view



    Wikimedia Foundation
    Powered by MediaWiki