As mentioned in earlier section, TestNG can be integrated with Simple Java project or Maven Integrated Java Project. First, we will see with a simple Java project.

  1. Go to the Eclipse IDE, create a new Java project. Right click on the Package Explorer panel, Select New option and then click on Project. You will see a New Project Wizard, select Java Project listed under Java Section, click on Next button, now you will be asked to provide the project name. Write the project name in Project Name input box and click on Finish Button.
  2. You will see that project is created in the Package Explorer along with default “src” folder in it.
  3. At this stage, we need to Integrate TestNG plug-in to the current project. So right click on the Project Name, go to Properties-> Java Build path-> Click on Add Library option, select TestNG and click on “Next” button and “Finish”. Finally click on “Apply and Close” button to close the Properties window.
  4. Create a new package under src folder. Then create a new Java class without the Main method.
  5. Write the below code in it.
TestNG Annotations

After writing the codes, you will notice that TestNG annotations are underlined with red, it means that the Eclipse IDE doesn’t know which library to refer for, just place the cursor over it, and select import from “org.testng.annotations” as show in the below screenshot to import.

How to use TestNG Annotations

An annotation is a tag that provides additional information about the class or method. It is represented by ‘@’ prefix. TestNG uses these annotations to help create a framework. Below listed are some of the most widely used annotations in test framework.


The test scripts (test methods) where the main business logic resides, should be tagged with @Test annotation. It has various attributes based on which the method can be reformed and executed which we will see later. By default, these @Test methods are executed in alphabetical order.


A Test suite consists of multiple classes, this annotated method will be run before all the tests methods of all the classes implemented in the test suite. This annotation marks the entry point of execution. @BeforeSuite annotation in TestNG can be used to perform the needed generic functions like setting properties in configuration files and starting Selenium driver or initializing the logging procedure which are necessary. It is executed only once in the test execution hierarchy



This annotated method will be run before your first @Test annotated method in your class (and after @BeforeSuite if used  in the code) You can use this annotation to launch the browser, setting up your own customized profile for your browser etc. that are must to run the test scripts. This annotated method is executed once for every tests defined in the <test> tag in testng.xml file. (We will see what Testng.xml file in further section)



This annotated method will be run before any @Test method of that class is executed but after @BeforeTest method. We can use this to go to the application website or anything that you would like perform/ set before executing the test scripts.



This annotated method will be run before every @test annotated method. You can use it to login to the application before executing your tests.


This annotated method will be run after every @test annotated method. You can use it to logout from the application after executing your tests. It can also be used to take screenshot of every method execution



This annotated method will be run after all @Test methods of the class is executed but before @BeforeTest annotated method is run. This annotation can be used to delete cookies or some that you want to do at class level



This annotated method will be run after all test method belonging to the classes inside the <test> tag in testNG.xml file is run. It can be used to close the browser or anything that you want to do after the execution of all test methods.


This annotated method will be run after all tests in this suite have run. This annotation can be used to clean up the processes before completing off your tests when you have multiple classes in functioning.


You can customize all the above annotated methods as per you testing needs. To summarize these, @Before annotated method behave as pre-test conditions, @After annotated methods behave as post-test conditions and @Test annotated method is where the business logic resides and it is executed in the alphabetical order by default

When you run the above code, the output will be as shown in the below screenshot


As explained in the above section, first all pre-test conditions are executed.

@BeforeSuite annotated method is executed only once before all other annotated methods’ execution.  

@BeforeTest:  It is executed only once since we have created only one   <test > tag defined. It is explained in the further section.

 @BeforeClass:  It is executed before any @Test annotated method execution of the current class. In the above example there is only one class so only once it is executed, next

@BeforeMethod, before every @Test annotated method. In our example we have two @Test annotated methods so twice it is executed.

Next, @Test annotated methods are executed.

Finally, post-test conditions are executed in the below order

@AfterMethod: It is executed after every @Test annotated method. In our example there are two @Test annotated methods so twice it is executed after each testcases

@AfterClass: After all the @Test annotated methods of the current classes are executed. In the above example only one class is defined so only once it is executed

@AfterTest: It is executed after all @Test annotated method from all classes have been executed

@AfterSuite is executed in the end.

Let’s see how  to execute this project in the next section.

Please follow and like us: