How to use TestNG for unit testing

TestNG is a Java based testing framework used for writing and executing unit tests. It provides a wide range of functions and flexibility, can easily create, configure and run Test suite, and provides powerful reporting and logging functions. TestNG also supports advanced functions such as concurrent testing, parameterized testing, dependency testing, and skip testing. The most commonly used key methods in TestNG are: 1. @ Test annotation: Use @ Test annotation to mark a testing method. By using the @ Test annotation, you can specify properties such as priority, timeout, and whether to enable the testing method. 2. @ BeforeMethod and @ AfterMethod annotations: Use the @ BeforeMethod annotation to execute once before each test method is executed, and use the @ AfterMethod annotation to execute once after each test method is executed. These methods can be used to set up initialization operations before testing and cleaning operations after testing. 3. @ BeforeClass and @ AfterClass annotations: Use the @ BeforeClass annotation to execute once before all test methods in the entire test class are executed, and use the @ AfterClass annotation to execute once after all test methods are executed. These methods can be used to perform class level initialization and cleanup operations. 4. @ DataProvider annotation: Use @ DataProvider annotation to provide test data. By using @ DataProvider annotations, test data can be separated from test methods to achieve data-driven testing. The following is a simple Java sample code for unit testing using TestNG: import org.testng.Assert; import org.testng.annotations.*; public class TestClass { @BeforeClass public void setUpClass() { //Initialize operation, execute once } @AfterClass public void tearDownClass() { //Clean up operation, execute once } @BeforeMethod public void setUp() { //Initialization operation, each test method will be executed once before execution } @AfterMethod public void tearDown() { //Cleanup operation, each test method will be executed once after execution } @Test(priority = 1) public void testMethod1() { //Test Method 1 Assert.assertTrue(true); } @Test(priority = 2) public void testMethod2() { //Test Method 2 Assert.assertEquals(1, 1); } @DataProvider(name = "testData") public Object[][] testData() { return new Object[][] { { "data1" }, { "data2" } }; } @Test(dataProvider = "testData") public void testMethodWithDataProvider(String data) { //Using test data for testing Assert.assertNotNull(data); } } The above code uses TestNG annotations to mark the testing method and set up the testing environment, and demonstrates how to use @ DataProvider annotations to provide testing data. The methods in the test class are executed according to the set priority, and the results are verified using assertions. If Maven is used to build a project, the following dependencies can be added to the 'pom. xml' file to introduce TestNG: <dependencies> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>7.4.0</version> <scope>test</scope> </dependency> </dependencies> By adding the above dependencies, Maven can be used to download and manage the TestNG framework.