First Test case with TestNG

Steps to follow:

1) Press Ctrl+N , select “TestNG Class” under TestNG category and click Next.


Right-click on Test Case folder, go to TestNG and select “TestNG Class“.


2) If your project is set up and you have selected the Test Case folder before creating a TestNG class then the source folder and the package name will be prepopulated on the form. Set class name as ‘TestNG‘.

Under Annotations, check “@BeforeMethod”, “@AfterMethod” and click Finish. That’s it.


3) Now it will display the newly created TestNg class under the Test Case package(folder). TestNG class will look like the image below with displaying three empty methods. One method f() by default and before & after method, as selected during the creation of the class.


4) Project explorer will look like this with the TestNG class.



Now it is the time to write the first TestNG test case.

5) Let’s take an example of First Test Case and divide the test case into three parts.

@BeforeMethod : Launch Firefox and direct it to the Base URL

@Test : Enter Username & Password to Login, Print console message and Log out

@AfterMethod : Close Firefox browser

6) Run the test by right click on the test case script and select Run As > TestNG Test.



Results of running the TestNG Test Case

7) Give it a few minutes to complete the execution, once it is finished the results will look like this in the TestNg Result window.


It displayed ‘passed : 1’. This means the test is successful and  Passed.

There are 3 sub-tabs. “All Tests”, “Failed Tests” and “Summary”. Just click “All Tests” to see what is there.


As you see, there is the information about which test cases are executed and their duration. Take look to other tabs. Better than Junit right?

8) TestNG also produce HTML reports. To access those reports go to the Project directory and open a test-output folder.


9) Open ‘emailable-report.html‘, as this is an HTML report open it with browser.


10) TestNG also produce ‘index.html‘ report and it resides in the same test-output folder. This report gives the link to all the different components of the TestNG reports like Groups & Reporter Output. On clicking these will display detailed descriptions of execution. In the advance chapter of TestNG, we will go through each of the TestNG topics.