top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

What are TestNG Annotations?


An annotation is a tag that provides additional information about the class or method. It is represented by ‘@’ prefix. TestNG use these annotations to help in making a robust framework.


  • In this Blog, we will look what are types of annotations and order of TestNG annotations.




Types of annotation in TestNG: -


@BeforeSuite

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. @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

Example:



@BeforeTest

This annotated method will be run before your first @Test annotated method in your class. You can use this annotation to launch the browser, setting up your own customized profile for your browser etc. This annotated method is executed once for every test defined in the <test> tag in testNG.xml file.

Example:



@BeforeClass

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.

Example:



@BeforeMethod

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

Example:



@Test

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.

Example:



@AfterMethod

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

Example:



@AfterClass

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 to close the browser

Example:



@AfterTest

This TestNG Annotations runs after all your test methods belonging to your class have run. This is a useful annotation which comes handy in terms of reporting your automation results to your stakeholders.

Example:



@AfterSuite

This TestNG Annotations runs post all the test methods of all the classes have run. This annotation can be used to clean up the processes before completing off your tests when you have multiple classes in functioning, for example closing the drivers etc.

Example:

Happy Learning!

67 views0 comments

Recent Posts

See All
bottom of page