Selenium testNG annotations

For the fundamentals of testNG framework and the testng.xml content, please refer to my prev blog post.

Let’s jump into a sample code to get more understanding on the annotations-

These below 2 classes contains all the details of testNG annotations and the output shows sequence of execution of each annotation.

Below examples also shows difference between @AfterTest and @AfterMethod

1st Class: testcase1

Another class: testcase2 with @Test, @AfterMethod and @BeforeMethod

As you can see in above 2 classes, in only one class, I have mentioned @Before and @AfterSuite, as this will be execute only once, so no need to mention in both the classes.Let’s see the xml for the above classes – Name it as testng_annotation.xml

Let’s run the above testng_annotation.xml and watch the output

In testcase1, @BeforeTest and @AfterTest willl be executed only once before and after all the methods run (as mentioned under the tag in testng_annotation.xml)

Whereas in testcase2, @BeforeMethod & @AfterMethod will be executed each time a @Test method runs, we have 2 methods tc02 and tc03, so @BeforeMethod & @AfterMethod executed twice.

Let’s get into bit more depth about testNG, refer testNG parameterization

Content posted is based on learning or working experience, please leave comments if anything needs to be added or updated, discuss your queries on our facebook:qavalidation.com, Thanks!

Leave a Reply

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

%d bloggers like this: