TestNG Basics And TestNG Attributes

In this article, we will discuss what is TestNG and also its features, advantages, and TestNG concepts as well.


  • Java JDK needs to be installed
  • Eclipse IDE needs to be installed
  • Java Test Automation project (should be a Maven project) needs to be setup
  • TestNG dependency should be added to that maven java project.
  • TestNG Plugin needs to be Installed inside the Eclipse IDE so that we can run the tests using that Plugin.

What is TestNG

  • TestNG is an automation testing framework in which NG stands for "Next Generation".
  • It was founded by Cedric Buest.
  • TestNG is inspired from JUnit and NUnit.
  • TestNG is easier to use.
  • TestNG covers all types of test automation like Unit testing, Functional testing, End-to-End, and integration testing.
  • TestNG provides a XML file in which the tester can define the test cases and test suite.

Advantages of using TestNG

  • It is open source.
  • Design test cases in a systematic way.
  • It will generate HTML reports
  • TestNG provides features like dependency annotations grouping priority.
  • Using TestNG we can perform parallel execution of tests
  • Using TestNG we can make use of data providers and perform data driven testing.
  • We can also parameterize the tests.

Test-NG is divided into three sections,

  • Pre-condition
  • Tests
  • Post-condition

TestNG makes use of annotation to write the tests,

@Beforesuite - The annotated method will be run before all tests in this suite have run. It executes only one time.

@BeforeTest - The annotated method will be run before any test method belonging to the classes inside the tag is run. It executes only one time.

@Beforeclass - The annotated method will be run before the first test method in the current class is invoked. It executes only one time.

@Beforemethod - The annotated method will be run before each test method

@Test - This method contains the logic of the test written.

@Aftermethod - The annotated method will be run after each test method

@Afterclass - The annotated method will be run after all the test methods in the current class have been run. It executes only one time.

@AfterTest - The annotated method will be run after all the test methods belonging to the classes inside the tag have run. It executes only one time.

@Aftersuite - The annotated method will be run after all tests in this suite have run. It executes only one time.

Execution order of testNG Annotations is,

  • BeforeSuite
  • BeforeTest
  • BeforeClass
  • BeforeMethod
  • Test
  • AfterMethod
  • AfterClass
  • AfterTest
  • AfterSuite

TestNG Groups

TestNG Groups allow you to perform groupings of different test methods. Grouping of test methods is required when you want to access the test methods of different classes.

Groups are specified inside the testng.xml file using <groups> tag.

We can include/exclude the group while running the test suite using testng.xml file

@Test(groups = “regression test ")
        public void verifyurlofpage() {
            //automation code goes here

TestNG Priority

In TestNG "Priority" is used to schedule the test cases. Default priority is 0.

Note: If we don’t give priority how will test-ng execute our test cases in alphabetical order.

@Test(priority = 1)
public void sampletest() {
    System.out.println("this is a test");

TestNG Dependency

If one test case is dependent on another test case then we use the concept of dependency.

we can also use dependency on group as well. if one group of test methods are dependent on another group

Types of attributes used are,

  • dependsOnMethods
  • dependsOnGroups
public class DependsOnTest {
    @Test(dependsOnMethods = {
    public void SignIn() {
        System.out.println("User has signed in successfully");
    public void OpenBrowser() {
        System.out.println("The browser is opened");
    @Test(dependsOnMethods = {
    public void LogOut() {
        System.out.println("The user logged out successfully");

Invocation count

We can make use of invocation count annotation to run a specific test case n times.

Thread pool size-It means that 2 threads will be created to run the test in parallel.

@Test(invocationCount=2, threadPoolSize = 2)
public void login() {
    //Code goes here

Invocation Timeout

If we want a particular test should execute within a particular period of time then we can use the concept of invocationtimeout.


If we knew that the particular test case will throw an exception then we can use the concept of expectedExceptions

public void titleofloginpage() {
public void login() {

TestNG parallel execution

If we want to execute the test cases in parallel then we need to click the test cases which are going to be executed in parallel and then right click it and then select convert to testng suite.

Select the parallel mode option to methods and click finish.

Thread count is the number of threads allocated to each test case to run.

We can run the methods, classes, tests in parallel.

TestNG Retry failed test case

If we want to run the failed test again then we need to write a separate class which implements the IRetryAnalyzer interface and we need to write the implementation of the retry method.

And then we need to refer the class in the respective test case that you are going to use.

TestNG Parameters

It is a way to pass values to the test methods as arguments.

Need to define the parameters in the testng.xml file,

<parameter name="hrmusername" value="admin">
<parameter name="hrmpassword" value="admin123">

And we need to refer in the test method in which you are passing


public void login(String unn, String pwd) {
    System.out.println("user logged in the hrm app");

TestNG listeners

In TestNG. @Listeners annotation which listens to every event that occurs in a selenium code. Listeners are activated either before the test or after the test case.

We need to create a class that implements the Itestlister interface and then we need to provide the method definition for the overridden method.

TestNG Reports

Test-NG comes with HTML reports and reports with images also. We can obtain reports by running the test cases at suite level.

  • After running the tests.
  • Refresh your project.
  • The reports will be under the test-output folder in your project.

Soft Assertion

Soft Assertions are the type of assertions that do not throw an exception immediately when an assertion fails.

Therefore, all steps and soft assertions in the automated test will execute before failing the test.

Hard Assertion

A Hard Assertion is a type of assertion that throws an exception immediately when an assert statement fails.

Test steps after hard assertion will not be executed and the next test in the test suite will start.


In this article, I have discussed the testNG basics and attributes that we can make use while writing test cases.

Thank you

Happy Learning...........

Similar Articles