In the previous tutorial we learn about What is Test Next Generation (TestNG) framework, Now it’s time for some hands-on experience of writing our first TestNG Test case.
How to Create TestNG Class in Eclipse
Follow below steps to create TestNG Class.
Step 1: Navigate to src from the project folder and right-click the same. You will see TestNG as an option in the drop-down towards the bottom.
Click on that and now see two sub-options to either create a TestNG class or convert the class to TestNG.
As we are creating a new TestNG class, you need to select the first option.

Step 2: Generally, the source folder name is auto-filled but if it is not, you can simply browse through the same. Next, you can give any name to your class for example,” TestNGFirst”, For now, we will keep the basic annotations selected @BeforeMethod and @AfterMethod. However, Annotations can be configured at a later stage as well depending upon your test scenario.



Step 3: You will now see a class in your project directory with default methods, viz f(), as well as beforeMethod() and afterMethod() you can see screenshot below.



You are now all set to write code in your first TestNG class, but before doing so let me quickly brief you about the TestNG annotations that we can see in the class we just created.
- @Test annotation implies that the method is a test method and any code written under it constitutes to be a test case.
- @BeforeMethod implies that the method beneath should be running before the test method.
- @AfterMethod, just as the name suggests implies that the method should run after the test method.
Writing Our First Test Case Using TestNG
Test Scenario: Write a simple test script wherein we will just open up a browser, navigate to Facebook, verify the title, and then close the browser session. You will see the utility of the @BeforeMethod and @AfterMethod annotations as well.
Program Source Code



Key points:
Now notice the following key points in the above TestNG test case example program.
1. TestNG does not require a main() method.
2. Methods need not be static.
3. A class named TestNGFirst has a test method, denoted by the TestNG annotation @Test mentioned before the FirstTest() function.
4. When the test method will be executed, the following actions will be performed by code.
- Chrome browser will be launch.
- The Login Page of Facebook will open.
- We will get the output as the title of the Login Page .
- At last, the browser will be closed.
5. Since we used annotations in the above test case, we needed to import the package org.testng.annotations.*.
Running TestNG Test Case
To run the test, simply select test class “TestNGFirst.java” in the package of src and then right-click on it, selecting Run as from the menu, and choosing TestNG test.
Eclipse will provide two results for your test execution, one in the Console window and the other on the TestNG Results window.
Output on Console Window:



TestNG Result Window:



I Hope this tutorial has covered all the important steps related to how to write TestNG test case with example.
Stay tuned for the next part of TestNG
Reference Link:
https://www.lambdatest.com/blog/complete-guide-on-testng-annotations-for-selenium-webdriver/
https://www.javatpoint.com/testng-tutorial
https://www.guru99.com/all-about-testng-and-selenium.html


