TestNG Test cases & Test suite creation

Below are the steps through which we can understand how to create test cases and suites through TestNG.

Test Case creation
Step 1: Open Eclipse IDE -> select the project -> right click -> select TestNG and click on create TestNG class.

Step 2: Select the source folder location and enter the package and class name. We have option for selecting multiple annotations here, after that click on Finish button.

Step 3: Eclipse provide the basic schema of this class.

Step 4: Let us take an example here and divide our test scenario into three parts.
@BeforeTest: Launch the firefox and navigate to the given URL.
@Test: Get the page title value.
@AfterMethod: Close Firefox browser.

Step 5: Run or execute this program by right click on the test case and select Run As -> TestNG Test.

Step 6: Once execution is done, we can see the result on TestNG result window.

Step 7: TestNG provides HTML report and we can see this report under test-output folder.

Test Suite creation

Step 1: Open Eclipse IDE -> select the project -> right click -> select New and click on File option.

Step 2: In New File section, add the file name for example ‘testng.xml’ and click on Finish button.

Step 3: In testng.xml file add the below code. First we need to enter the suite tag and provide any meaningful name for the suite. Under the suite tag we mention test tag and then the class tag tag.

Step 4: Execute the test by right click on testng.xml file and select Run As > TestNG Suite.

Step 5: Once the execution is done, we can see the execution result under TestNG console.
Note: We can add multiple test cases in our testng.xml test suite and execute it altogether.