TestNG
≈ 4 min read
Names in other languages
No names in other languages are held for this entry yet.
Article bodies are English and are not machine-translated. A name in another language is not a translation of the article.
TestNG is a testing framework for the Java programming language created by Cédric 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.
TestNG is a testing framework for the Java programming language created by Cédric 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. ===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() { 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 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: 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. ==Comparison with JUnit== TestNG has a longstanding rivalry with the JUnit test automation framework. Each of them has differences and respective advantages. Stack Overflow 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 JUnit xUnit ==References== ==External links==
Text derived from https://en.wikipedia.org/w/index.php?oldid=1356814503 under CC BY-SA 4.0; modifications have been made.