Home > Automation Testing > TestNG: Use of Before and After annotations with sample code

TestNG: Use of Before and After annotations with sample code

This post was most recently updated on July 31st, 2024

Exmaples for @BeforeMethod and @AfterMethod in TestNG.

@BeforeMethod allows the method to execute before the execution of each Test methods, whereas @afterMethod is executed after the execution of each Test methods. 

Output:

First Method:
Before Method starts!
Test with Default priority
After Method starts

In the above output, we can observe for every Test first @BeforeMethod is executing and then eventually @AfterMethod will execute after each Test method. 

Second Method:
Before Method starts!
Test with priority
After Method starts

2. @BeforeClass and @AfterClass

The annotated method @BeforeClass will execute only once before the first test method is invoked in that class. The @AfterClass annotation will be executed only once after all the test methods are invoked in that class. In the below output, we can see that @BeforeClass and @AfterClass are executed at the very beginning and very end.

Output:

Before Class Method!
Test method with Default priority
Test method with priority=1
After Class Method!

When we run the above code, the output will be BeforeStart() method. It is executed before executing Test() and Test1() method. During the final step, AfterTest () method is executed. Both the methods executed only once.

3. @BeforeTest and @AfterTest

The @BeforeTest methods run after each Test methods. @BeforeTest can be used for creating an initial data set up and prior to running other test methods, whereas @AfterTest annotation will run after the other tests are complete.

When we run the above code, First BeforeMethod will execute in which we declaring Webdriver driver, driver path. Next Test method will execute where we are entering “TestNG” text in the search bar and then click on the search button.
At last AfterTest method will execute which will check the current opened page title and will print the page title eventually will close the page.

4. @BeforeSuite and @AfterSuite

@BeforeSuite method will run before all tests present in this suite and eventually @AfterSuite will run after all the tests. 

In this tutorial, we’ve discussed some important annotations and attributes. Some other annotations that are also used include @AfterGroups and @BeforeGroups which we covered with @Test annotation. 

This Article is TAGGED in , , , , , . BOOKMARK THE permalink.

Devanand Tripathi
Software Test Engineer Skills : Selenium Webdriver, Manual Testing, Database Testing, STLC Java, UI / UX Testing

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">